Пример #1
0
    def add_component(self,
                      component: Metadatable,
                      name: str = None,
                      update_snapshot: bool = True) -> str:
        """
        Record one component as part of this Station.

        Args:
            component: Components to add to the Station.
            name: Name of the component.
            update_snapshot: Immediately update the snapshot
                of each component as it is added to the Station.

        Returns:
            str: The name assigned this component, which may have been changed
                to make it unique among previously added components.
        """
        try:
            component.snapshot(update=update_snapshot)
        except:
            pass
        if name is None:
            name = getattr(component, 'name',
                           'component{}'.format(len(self.components)))
        namestr = str(name)
        if namestr in self.components.keys():
            raise RuntimeError(
                f'Cannot add component "{namestr}", because a '
                'component of that name is already registered to the station')
        self.components[namestr] = component
        return namestr
Пример #2
0
    def add_component(self, component: Metadatable, name: str = None,
                      update_snapshot: bool = True) -> str:
        """
        Record one component as part of this Station.

        Args:
            component (Any): components to add to the Station.
            name (str): name of the component
            update_snapshot (bool): immediately update the snapshot
                of each component as it is added to the Station, default true

        Returns:
            str: The name assigned this component, which may have been changed
                 to make it unique among previously added components.

        """
        try:
            component.snapshot(update=update_snapshot)
        except:
            pass
        if name is None:
            name = getattr(component, 'name',
                           'component{}'.format(len(self.components)))
        namestr = make_unique(str(name), self.components)
        self.components[namestr] = component
        return namestr
Пример #3
0
    def test_snapshot(self):
        m = Metadatable(metadata={6: 7})
        self.assertEqual(m.snapshot_base(), {})
        self.assertEqual(m.snapshot(), {'metadata': {6: 7}})
        del m.metadata[6]
        self.assertEqual(m.snapshot(), {})

        sb = self.HasSnapshotBase(metadata={7: 8})
        self.assertEqual(sb.snapshot_base(), {'cheese': 'gruyere'})
        self.assertEqual(sb.snapshot(),
                         {'cheese': 'gruyere', 'metadata': {7: 8}})
        del sb.metadata[7]
        self.assertEqual(sb.snapshot(), sb.snapshot_base())

        s = self.HasSnapshot(metadata={8: 9})
        self.assertEqual(s.snapshot(), {'fruit': 'kiwi'})
        self.assertEqual(s.snapshot_base(), {})
        self.assertEqual(s.metadata, {8: 9})
Пример #4
0
def test_snapshot():
    m = Metadatable(metadata={6: 7})
    assert m.snapshot_base() == {}
    assert m.snapshot() == {'metadata': {6: 7}}
    del m.metadata[6]
    assert m.snapshot() == {}

    sb = HasSnapshotBase(metadata={7: 8})
    assert sb.snapshot_base() == {'cheese': 'gruyere'}
    assert sb.snapshot() == \
           {'cheese': 'gruyere', 'metadata': {7: 8}}
    del sb.metadata[7]
    assert sb.snapshot() == sb.snapshot_base()

    s = HasSnapshot(metadata={8: 9})
    assert s.snapshot() == {'fruit': 'kiwi'}
    assert s.snapshot_base() == {}
    assert s.metadata == {8: 9}