def compute_rows(self): """ Compute the main rows of the grid corresponding to the datas retrieved by celery also fill rows of computed_totals with 0 :returns: generator yielding 2-uple (type, row) where type is a IncomeStatementMeasureType instance and row contains the datas of the grid row for the given type (15 columns). :rtype: tuple """ for category in self.categories: for type_ in self.types[category.id]: row = [] if not type_.computed_total: sum = 0 for month, grid in self.grids.items(): value = self._get_month_cell(grid, type_.id) sum += value row.append(value) self.type_totals[type_.id][month] = value self.category_totals[category.id][month] += value self.type_totals[type_.id]['total'] = sum self.category_totals[category.id]['total'] += sum row.append(sum) percent = math_utils.percent(sum, self.turnover, 0) row.append(percent) else: row.extend([0 for i in range(0, 14)]) yield type_, row
def compute_turnover_percent(index, projections, turnovers): """ Compute the percent the difference represents """ turnover = turnovers.get(index) if turnover: projection = projections.get(index) if projection: if projection.value: return percent(turnover, projection.value) return None
def compute_turnover_percent(index, projections, turnovers): """ Compute the percent the difference represents """ turnover = turnovers.get(index) if turnover is not None: projection = projections.get(index) if projection is not None: if projection.value is not None: return percent(turnover, projection.value, 0) return None
def compile_rows(self): """ Pre-compute all row datas First collect datas collected from the general ledger file Then compile totals """ result = [] result = list(self.compute_rows()) # After having collected all rows, we compile totals for type_, datas in result: if type_.computed_total: total = 0 for month in range(1, 13): values = self._collect_values_for_computation(month) month_total = type_.compute_total(values) total += month_total datas[month - 1] = month_total datas[-2] = total percent = math_utils.percent(total, self.turnover, 0) datas[-1] = percent return result
def test_percent(self): self.assertEqual(percent(30, 10), 300.0) self.assertEqual(percent(1, 3), 33.33) self.assertRaises(ZeroDivisionError, percent, 1, 0) self.assertEqual(percent(1, 0, 5), 5)