class EconomicModel: def __init__(self, wage, fixedCost, unitCost, unitPrice): self.wage = wage self.fixedCost = fixedCost self.unitPrice = unitPrice self.hours = 8 # length of worker's shift self.unitProfit = unitPrice - unitCost def format(value): value_ = "$" + value #(Math.abs(value)).toFixed(2) if value < 0: return "(" + value_ + ")" else: return value_ def getWage(self): return format(self.wage) """ Calculate daily profit/loss """ def internalExecute(self, units): income = units * self.unitProfit expense = self.fixedCost + self.hours * self.wage result = income - expense return result """ Calculate and format daily profit/loss """ def execute(self, units): result = self.internalExecute(units) return format(result) class TestA: def makeTable(self, data): def buildLine(elem): result1 = " ".join(elem) return result1 result = map(buildLine, data) return result """ Calculate table row data """ def calcData(self, helper): # Hack to have header generation near actual data generation if helper is None: return ["Wage", "100 Units", "200 Units", "500 Units","1000 Units" ] result = []; result.append(helper.getWage()) result.append(helper.execute(100)) result.append(helper.execute(200)) result.append(helper.execute(500)) result.append(helper.execute(1000)) return result def execute(self): fixedCost = 1000 unitCost = 3.00 unitPrice = 5.00 wageOld = 7.00 wageNew = 15.00 helperOld = EconomicModel(wageOld, fixedCost, unitCost, unitPrice) helperNew = EconomicModel(wageNew, fixedCost, unitCost, unitPrice) data = [ self.calcData(None), # header self.calcData(helperOld), self.calcData(helperNew) ] table = self.makeTable(data) print "\n".join(table) test = TestA() test.execute()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question