예제 #1
0
 def test_non_callable_group_keys(self):
     collect_everything = Nodes(key="everything")
     g1 = Grouping(key="The Special One",
                   filter=lambda e: "special" in e.uid)
     g2 = Nodes(key="A Subset", filter=lambda e: "subset" in e.uid)
     ES = es.EnergySystem(groupings=[g1, g2, collect_everything])
     special = Entity(uid="special")
     subset = set(Entity(uid="subset: {}".format(i)) for i in range(2))
     others = set(Entity(uid="other: {}".format(i)) for i in range(2))
     everything = subset.union(others)
     everything.add(special)
     eq_(ES.groups["The Special One"], special)
     eq_(ES.groups["A Subset"], subset)
     eq_(ES.groups["everything"], everything)
예제 #2
0
    def test_grouping_laziness(self):
        """ Energy system `groups` should be fully lazy.

        `Node`s added to an energy system should only be tested for and put
        into their respective groups right before the `groups` property of an
        energy system is accessed.
        """
        group = "Group"
        g = Nodes(key=group, filter=lambda n: getattr(n, "group", False))
        self.es = es.EnergySystem(groupings=[g])
        buses = [Bus("Grouped"), Bus("Ungrouped one"), Bus("Ungrouped two")]
        self.es.add(buses[0])
        buses[0].group = True
        self.es.add(*buses[1:])
        ok_(
            group in self.es.groups,
            "\nExpected to find\n\n  `{!r}`\n\nin `es.groups`.\nGot:\n\n  `{}`"
            .format(
                group,
                "\n   ".join(pformat(set(self.es.groups.keys())).split("\n")),
            ),
        )
        ok_(
            buses[0] in self.es.groups[group],
            "\nExpected\n\n  `{}`\n\nin `es.groups['{}']`:\n\n  `{}`".format(
                "\n   ".join(pformat(buses[0]).split("\n")), group,
                "\n   ".join(pformat(self.es.groups[group]).split("\n"))),
        )
예제 #3
0
 def test_grouping_filter_parameter(self):
     g1 = Grouping(key=lambda e: "The Special One",
                   filter=lambda e: "special" in e.uid)
     g2 = Nodes(key=lambda e: "A Subset",
                filter=lambda e: "subset" in e.uid)
     ES = es.EnergySystem(groupings=[g1, g2])
     special = Entity(uid="special")
     subset = set(Entity(uid="subset: {}".format(i)) for i in range(10))
     others = set(Entity(uid="other: {}".format(i)) for i in range(10))
     eq_(ES.groups["The Special One"], special)
     eq_(ES.groups["A Subset"], subset)
예제 #4
0
 def test_grouping_filter_parameter(self):
     g1 = Grouping(key=lambda e: "The Special One",
                   filter=lambda e: "special" in str(e))
     g2 = Nodes(key=lambda e: "A Subset",
                filter=lambda e: "subset" in str(e))
     ES = es.EnergySystem(groupings=[g1, g2])
     special = Node(label="special")
     subset = set(Node(label="subset: {}".format(i)) for i in range(10))
     others = set(Node(label="other: {}".format(i)) for i in range(10))
     ES.add(special, *subset)
     ES.add(*others)
     eq_(ES.groups["The Special One"], special)
     eq_(ES.groups["A Subset"], subset)
예제 #5
0
    def test_proper_filtering(self):
        """ `Grouping.filter` should not be "all or nothing".

        There was a bug where, if `Grouping.filter` returned `False` only for
        some elements of `Grouping.value(e)`, those elements where actually
        retained.
        This test makes sure that the bug doesn't resurface again.
        """
        g = Nodes(key="group", value=lambda _: {1, 2, 3, 4},
                  filter=lambda x: x % 2 == 0)
        ensys = es.EnergySystem(groupings=[g])
        special = Node(label="object")
        ensys.add(special)
        eq_(ensys.groups["group"], {2, 4})
예제 #6
0
    def test_constant_group_keys(self):
        """ Callable keys passed in as `constant_key` should not be called.

        The `constant_key` parameter can be used to specify callable group keys
        without having to worry about `Grouping`s trying to call them. This
        test makes sure that the parameter is handled correctly.
        """
        everything = lambda: "everything"
        collect_everything = Nodes(constant_key=everything)
        ES = es.EnergySystem(groupings=[collect_everything])
        node = Node(label="A Node")
        ok_("everything" not in ES.groups)
        ok_(everything in ES.groups)
        eq_(ES.groups[everything], set([node]))
예제 #7
0
    def __init__(self, **kwargs):
        self._first_ungrouped_node_index_ = 0
        self._groups = {}
        self._groupings = ([BY_UID] +
                           [g if isinstance(g, Grouping) else Nodes(g)
                            for g in kwargs.get('groupings', [])])
        self.entities = []

        self.results = kwargs.get('results')

        self.timeindex = kwargs.get('timeindex')

        self.temporal = kwargs.get('temporal')

        self.add(*kwargs.get('entities', ()))
예제 #8
0
    def __init__(self, **kwargs):
        for attribute in ['entities']:
            setattr(self, attribute, kwargs.get(attribute, []))

        Entity.registry = self
        Node.registry = self
        self._groups = {}
        self._groupings = ([BY_UID] + [
            g if isinstance(g, Grouping) else Nodes(g)
            for g in kwargs.get('groupings', [])
        ])
        for e in self.entities:
            for g in self._groupings:
                g(e, self.groups)
        self.results = kwargs.get('results')
        self.timeindex = kwargs.get('timeindex')
예제 #9
0
    def __init__(self, **kwargs):
        for attribute in ['entities']:
            setattr(self, attribute, kwargs.get(attribute, []))

        self._groups = {}
        self._groupings = ([BY_UID] + [
            g if isinstance(g, Grouping) else Nodes(g)
            for g in kwargs.get('groupings', [])
        ])
        for e in self.entities:
            for g in self._groupings:
                g(e, self.groups)
        self.results = kwargs.get('results')
        self.timeindex = kwargs.get(
            'timeindex',
            pd.date_range(start=pd.to_datetime('today'), periods=1, freq='H'))