def execute(self, startDate, endDate, today=None): data = Data() # Allow modifying today for unit tests to work consistently if self.today: today = self.today else: today = datetime.datetime.today().date() # Ensure we're looking into the future if startDate < today: raise RuntimeError("Engine only supports calculating future dates.") # Record the dates the data is for data.startDate = startDate data.endDate = endDate # Calculate our daily balance currentBalance = sum([account.getTotalBalance() for account in self.accounts]) for currentDate in daterange(today, data.endDate + datetime.timedelta(1)): # Apply our balance modifiers for modifier in self.balanceModifiers: if modifier.recurrence.isOnDate(currentDate): currentBalance += modifier.amount # Save the new balance for the current date if (currentDate >= startDate): data.balances[currentDate] = currentBalance return data
def outputData(data, outputPath): dates = list() balances = list() for currentDate in daterange(data.startDate, data.endDate + datetime.timedelta(1)): dates.append(currentDate.strftime(DATE_FORMAT)) balances.append(str(data.balances[currentDate])) file = open(outputPath, "w") file.write(",".join(dates)) file.write("\n") file.write(",".join(balances)) file.write("\n") file.close()