class Snapshot(models.Model): location = models.CharField(max_length=50) age = fields.YearField() x = models.PositiveSmallIntegerField() y = models.PositiveSmallIntegerField() class Meta: ordering = 'location', 'age', 'x', 'y' def __unicode__(self): return '%s old tree in %s at (%s, %s)' % (self.age, self.location, self.x, self.y) def use(self, fuel): return self.uses.filter(fuel=fuel).aggregate(use=Sum('use'))['use'] or (0 * J)
class WallInsulation(Insulation): data = DataLinkField('ComponentBase', 'wall_insulation') new = fields.CostField() old = fields.CostField() maintenance_cost = fields.CostField() repair_cost = fields.CostField() repair_rate = fields.YearField() replacement_cost = fields.CostField() replacement_rate = fields.YearField() RateModel = BlanketWallInsulation def makewith(self, data, location, building, standard, simulation): r = BlanketWallInsulation.r(standard, building, location.climate_zone(standard)) inner = tuple( BlanketWallInsulation.sheets(location, building, standard)) r -= sum((i.r for i in inner if i.r), 0) outer = RigidWallInsulation.sheets(location, building, standard, r) size = simulation.wall_area.into('ft**2') self.takefrom(data.group, chain(inner, outer), size, building.blanket_wall_insulation, building.rigid_wall_insulation) self.save()
class CarbonFunction(models.Model): name = models.SlugField(primary_key=True, choices=( ('low', 'Low'), ('medium', 'Meidum'), ('high', 'High'))) building = models.ForeignKey('structures.Building') location = models.ForeignKey('locations.Location') standard = models.ForeignKey('standards.Standard') year = fields.YearField() emissions = metric.GramField() price = metric.CostPerGramField() @property def cost(self): return self.emissions * self.price
class Costs(models.Model): row = models.ForeignKey(Row, related_name='costs') period = fields.YearField() lifecycle_cost = fields.CostField() efficiency_cost = fields.CostField() energy_cost = fields.CostField() energy_use = metric.KiloWattHourField() gas_use = uscs.BTUperYearField() electricity_use = uscs.BTUperYearField() class Meta: unique_together = 'row', 'period' def __unicode__(self): return 'Deltas for %s' % self.row
class Row(Results): group = models.ForeignKey(Group, related_name='rows') period = fields.YearField() year = property(lambda self: int(self.period.just('years'))) years = property(lambda self: xrange(1, self.year + 1)) def __unicode__(self): return u"%s: %s" % (self.group, self.period) def pv(self, cost, year=None): return self.group.pv(cost, year or self.year) @cached_property def base(self): return self.group.base.row_for(self.period) @cached_property def options(self): for group in self.group.options: yield group.row_for(self.period)
class PriceIndex(models.Model): fuel = models.ForeignKey(Fuel) year = fields.YearField() zone = models.ForeignKey(CensusRegion) value = models.FloatField() class Meta: unique_together = 'fuel', 'year', 'zone' ordering = 'fuel', 'year', 'zone' verbose_name_plural = 'price indicies' @classmethod def upv(cls, fuel, zone, year, rate): indicies = cls.objects.filter(fuel=fuel, zone=zone, year__lte=year).order_by('year') discount = lambda i: i.value / ((1 + rate)**i.year.just('year')) return sum(map(discount, indicies)) def __unicode__(self): return '%s price index for year %s in zone %s' % (self.fuel, self.year, self.zone)