Beispiel #1
0
    def test_dump_and_load_(self):
        in_resource = Book(
            title='Consider Phlebas',
            isbn='0-333-45430-8',
            num_pages=471,
            rrp=19.50,
            fiction=True,
            genre="sci-fi",
            authors=[Author(name="Iain M. Banks")],
            publisher=Publisher(name="Macmillan"),
            published=[datetime.datetime(1987, 1, 1, tzinfo=utc)]
        )

        d = dict_codec.dump(in_resource)
        out_resource = dict_codec.load(d)

        self.assertEqual(out_resource.title, in_resource.title)
        self.assertEqual(out_resource.isbn, in_resource.isbn)
        self.assertEqual(out_resource.num_pages, in_resource.num_pages)
        self.assertEqual(out_resource.rrp, in_resource.rrp)
        self.assertEqual(out_resource.fiction, in_resource.fiction)
        self.assertEqual(out_resource.genre, in_resource.genre)
        self.assertEqual(out_resource.authors[0].name, in_resource.authors[0].name)
        self.assertEqual(out_resource.publisher.name, in_resource.publisher.name)
        self.assertEqual(out_resource.published[0], in_resource.published[0])
Beispiel #2
0
 def seasonal_tariff(self):
     seasonal_tariff = dict_codec.load(
         {
             "charges": [{
                 "rate": 1.0,
                 "season": {
                     "name": "summer",
                     "from_month": 1,
                     "from_day": 1,
                     "to_month": 3,
                     "to_day": 31
                 }
             }, {
                 "rate": 1.0,
                 "season": {
                     "name": "winter",
                     "from_month": 4,
                     "from_day": 1,
                     "to_month": 12,
                     "to_day": 31
                 }
             }],
             "service":
             "electricity",
             "consumption_unit":
             "kWh",
             "demand_unit":
             "kVA",
             "billing_period":
             "monthly"
         }, Tariff)
     return seasonal_tariff
 def scheduled_tariff(self):
     scheduled_tariff = dict_codec.load(
         {
             "charges": [
                 {
                     "rate_schedule": [
                         {
                             "datetime":"2018-01-01T00:00:00Z",
                             "rate": 0.1
                         },
                         {
                             "datetime":"2018-01-01T00:30:00Z",
                             "rate": 0.2
                         },
                         {
                             "datetime": "2018-01-01T01:00:00Z",
                             "rate": 0.3
                         }
                     ]
                 }
             ],
             "service": "electricity",
             "energy_unit": "kWh",
             "demand_unit": "kVA"
         }, Tariff
     )
     return scheduled_tariff
Beispiel #4
0
 def supply_payment_tariff(self):
     supply_payment_tariff = dict_codec.load(
         {
             "charges": [{
                 "rate": -1.0,
                 "meter": "electricity_exported"
             }],
             "service": "electricity"
         }, Tariff)
     return supply_payment_tariff
Beispiel #5
0
 def demand_tariff(self):
     demand_tariff = dict_codec.load(
         {
             "charges": [{
                 "rate": 1.0,
                 "type": "demand"
             }],
             "service": "electricity",
             "demand_window": "15min",
             "billing_period": "monthly"
         }, Tariff)
     return demand_tariff
Beispiel #6
0
 def block_tariff(self):
     block_tariff = dict_codec.load(
         {
             "charges": [{
                 "rate_bands": [{
                     "limit": 10,
                     "rate": 1.0
                 }, {
                     "rate": 1.0
                 }]
             }],
             "service":
             "electricity",
             "consumption_unit":
             "kWh",
             "demand_unit":
             "kVA",
             "billing_period":
             "monthly"
         }, Tariff)
     return block_tariff
Beispiel #7
0
 def tou_tariff(self):
     tou_tariff = dict_codec.load(
         {
             "charges": [{
                 "rate": 1.0,
                 "time": {
                     "name":
                     "peak",
                     "periods": [{
                         "from_weekday": 0,
                         "to_weekday": 4,
                         "from_hour": 14,
                         "to_hour": 19,
                     }]
                 }
             }, {
                 "rate": 1.0,
                 "time": {
                     "name":
                     "shoulder",
                     "periods": [{
                         "from_weekday": 0,
                         "to_weekday": 4,
                         "from_hour": 10,
                         "to_hour": 13,
                     }, {
                         "from_weekday": 0,
                         "to_weekday": 4,
                         "from_hour": 20,
                         "to_hour": 21,
                     }]
                 }
             }, {
                 "rate": 1.0,
                 "time": {
                     "name":
                     "off-peak",
                     "periods": [{
                         "from_weekday": 0,
                         "to_weekday": 4,
                         "from_hour": 0,
                         "from_minute": 0,
                         "to_hour": 9,
                         "to_minute": 59
                     }, {
                         "from_weekday": 0,
                         "to_weekday": 4,
                         "from_hour": 22,
                         "from_minute": 0,
                         "to_hour": 23,
                         "to_minute": 59
                     }, {
                         "from_weekday": 5,
                         "to_weekday": 6
                     }]
                 }
             }],
             "service":
             "electricity",
             "consumption_unit":
             "kWh",
             "demand_unit":
             "kVA",
             "billing_period":
             "monthly"
         }, Tariff)
     return tou_tariff
Beispiel #8
0
    def apply(self, meter_data, start=None, end=None):
        """
            Calculates the cost of energy given a tariff and load.

            :param meter_data: a three-column pandas array with datetime, imported energy (kwh), exported energy (kwh)
            :param start: an optional datetime to select the commencement of the bill calculation
            :param end: an optional datetime to select the termination of the bill calculation
            :return: a dictionary containing the charge components (e.g. off_peak, shoulder, peak, total)
        """
        meter_data.truncate(before=start, after=end)

        # Create empty dict to hold calculated cost items
        cost_items = dict()
        for charge in self.charges:
            season = charge.season.name if charge.season else None
            time = charge.time.name if charge.time else None
            cost_items[charge.name] = {
                'name': charge.name,
                'code': charge.code,
                'type': charge.type,
                'season': season,
                'time': time,
                'cost': 0.0,
            }

        if 'consumption' in self.charge_types or 'generation' in self.charge_types:
            consumption_data = meter_data
            # Resample meter data if finer resolution data not required by the specified tariff types
            if 'tou' not in self.charge_types:
                if 'seasonal' in self.charge_types:
                    consumption_data = meter_data.resample('D').sum()
                else:
                    consumption_data = meter_data.resample(
                        PERIOD_TO_TIMESTEP[self.billing_period]).sum()

            cost_items.update(
                self.apply_by_charge_type(consumption_data, cost_items,
                                          'consumption'))

        if 'demand' in self.charge_types:
            # Resample meter data to the demand window and then take the maximum for each billing period
            demand_data = meter_data.resample(
                PERIOD_TO_TIMESTEP[self.demand_window]).mean()
            peak_monthly = demand_data.resample(
                PERIOD_TO_TIMESTEP[self.billing_period]).max()
            cost_items.update(
                self.apply_by_charge_type(peak_monthly, cost_items, 'demand'))

        if 'fixed' in self.charge_types:
            billing_periods = len(
                meter_data.resample(
                    PERIOD_TO_TIMESTEP[self.billing_period]).mean())
            for charge in self.charges:
                if charge.type == 'fixed':
                    cost_items[
                        charge.name]['cost'] = billing_periods * charge.rate

        # Transform the output data into the specified output format
        cost_dict = {
            'name': self.name,
            'code': self.code,
            'items': cost_items.values()
        }

        return dict_codec.load(cost_dict, Cost)