Пример #1
0
def test_delete_activity_parameters():
    db = DatabaseChooser("example")
    db.register()

    a = db.new_activity(code="A", name="An activity")
    a.save()
    b = db.new_activity(code="B", name="Another activity")
    b.save()
    a.new_exchange(
        amount=0, input=b, type="technosphere", formula="foo * bar + 4"
    ).save()

    activity_data = [
        {
            "name": "reference_me",
            "formula": "sqrt(25)",
            "database": "example",
            "code": "B",
        },
        {
            "name": "bar",
            "formula": "reference_me + 2",
            "database": "example",
            "code": "A",
        },
    ]
    parameters.new_activity_parameters(activity_data, "my group")
    parameters.add_exchanges_to_group("my group", a)

    assert ActivityParameter.select().count() == 2
    assert ParameterizedExchange.select().count() == 1

    a.delete()
    assert ActivityParameter.select().count() == 1
    assert not ParameterizedExchange.select().count()
Пример #2
0
def test_delete_parameterized_exchange():
    db = DatabaseChooser("example")
    db.register()

    a = db.new_activity(code="A", name="An activity")
    a.save()
    b = db.new_activity(code="B", name="Another activity")
    b.save()
    exc = a.new_exchange(amount=0, input=b, type="technosphere", formula="foo * bar + 4")
    exc.save()

    activity_data = [{
        'name': 'reference_me',
        'formula': 'sqrt(25)',
        'database': 'example',
        'code': "B",
    }, {
        'name': 'bar',
        'formula': 'reference_me + 2',
        'database': 'example',
        'code': "A",
    }]
    parameters.new_activity_parameters(activity_data, "my group")
    parameters.add_exchanges_to_group("my group", a)

    assert ActivityParameter.select().count() == 2
    assert ParameterizedExchange.select().count() == 1

    exc.delete()
    assert ActivityParameter.select().count() == 2
    assert not ParameterizedExchange.select().count()
Пример #3
0
    def _prepare_activity_parameters(self, data=None, delete_existing=True):
        data = self.data if data is None else data

        def supplement_activity_parameter(ds, dct):
            dct.update({'database': self.db_name, 'code': ds['code']})
            if 'group' not in dct:
                dct['group'] = "{}:{}".format(dct['database'], dct['code'])
            return dct

        activity_parameters = [
            supplement_activity_parameter(ds, dct)
            for ds in data
            for dct in ds.pop("parameters", [])
        ]
        by_group = lambda x: x['group']
        activity_parameters = sorted(activity_parameters, key=by_group)

        # Delete all parameterized exchanges because
        # all exchanges are re-written, even on
        # update, which means ids are unreliable
        # Must add exchanges again manually
        bad_groups = tuple({o[0] for o in ActivityParameter.select(
            ActivityParameter.group).where(
            ActivityParameter.database == self.db_name
        ).tuples()})
        ParameterizedExchange.delete().where(
            ParameterizedExchange.group << bad_groups
        ).execute()
        if delete_existing:
            # Delete existing parameters and p. exchanges if necessary
            ActivityParameter.delete().where(ActivityParameter.group << bad_groups
            ).execute()
        else:
            # Delete activity parameters
            # where the group name changed
            name_changed = tuple({o[0] for o in
                ActivityParameter.select(
                ActivityParameter.group).where(
                ActivityParameter.database == self.db_name,
                ActivityParameter.code << tuple(
                    [m['code'] for m in activity_parameters]
                ),
                ~(ActivityParameter.group << tuple(
                    [m['group'] for m in activity_parameters]
                ))
            ).tuples()})
            ActivityParameter.delete().where(ActivityParameter.group << name_changed
            ).execute()

        return activity_parameters
Пример #4
0
def test_database_delete_parameters():
    db = DatabaseChooser("example")
    db.register()

    a = db.new_activity(code="A", name="An activity")
    a.save()
    b = db.new_activity(code="B", name="Another activity")
    b.save()
    a.new_exchange(amount=0, input=b, type="technosphere", formula="foo * bar + 4").save()

    database_data = [{
        'name': 'red',
        'formula': '(blue ** 2) / 5',
    }, {
        'name': 'blue',
        'amount': 12
    }]
    parameters.new_database_parameters(database_data, "example")

    activity_data = [{
        'name': 'reference_me',
        'formula': 'sqrt(red - 20)',
        'database': 'example',
        'code': "B",
    }, {
        'name': 'bar',
        'formula': 'reference_me + 2',
        'database': 'example',
        'code': "A",
    }]
    parameters.new_activity_parameters(activity_data, "my group")
    parameters.add_exchanges_to_group("my group", a)

    assert ActivityParameter.select().count() == 2
    assert ParameterizedExchange.select().count() == 1
    assert DatabaseParameter.select().count() == 2
    assert len(parameters) == 4

    del databases['example']
    assert not len(parameters)
    assert not ParameterizedExchange.select().count()
Пример #5
0
 def __init__(self):
     self._project_params = ProjectParameter.load()
     self._db_params = {
         p.database: DatabaseParameter.load(p.database)
         for p in DatabaseParameter.select(DatabaseParameter.database).distinct()
     }
     self._act_params = {
         p.group: ActivityParameter.load(p.group)
         for p in ActivityParameter.select(ActivityParameter.group).distinct()
     }
     self._distinct_act_params = [
         p for p in (ActivityParameter
                     .select(ActivityParameter.group, ActivityParameter.database)
                     .distinct())
     ]
     self._exc_params = [p for p in ParameterizedExchange.select()]
Пример #6
0
    def recalculate_exchanges(group: str,
                              global_params: dict = None
                              ) -> List[Tuple[int, float]]:
        """ Constructs a list of exc.id/amount tuples for the
        ParameterizedExchanges in the given group.
        """
        if global_params is None:
            global_params = {}

        params = (ParameterizedExchange.select().where(
            ParameterizedExchange.group == group))

        if not params.exists():
            return []

        interpreter = Interpreter()
        interpreter.symtable.update(global_params)
        return [(p.exchange, interpreter(p.formula)) for p in params]
Пример #7
0
 def has_parameterized_exchanges() -> bool:
     """ Test if ParameterizedExchanges exist, no point to using this manager
     otherwise.
     """
     return ParameterizedExchange.select().exists()
Пример #8
0
 def can_build_presamples() -> bool:
     """ Test if ParameterizedExchanges exist, no point to building presamples
      otherwise
     """
     return ParameterizedExchange.select().exists()