Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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})
Ejemplo n.º 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}
Ejemplo n.º 5
0
 def test_load(self):
     m = Metadatable()
     self.assertEqual(m.metadata, {})
     m.load_metadata({1: 2, 3: 4})
     self.assertEqual(m.metadata, {1: 2, 3: 4})
     m.load_metadata({1: 5})
     self.assertEqual(m.metadata, {1: 5, 3: 4})
Ejemplo n.º 6
0
def test_load():
    m = Metadatable()
    assert m.metadata == {}
    m.load_metadata({1: 2, 3: 4})
    assert m.metadata == {1: 2, 3: 4}
    m.load_metadata({1: 5})
    assert m.metadata == {1: 5, 3: 4}
Ejemplo n.º 7
0
    def test_init(self):
        with self.assertRaises(TypeError):
            Metadatable(metadata={2: 3}, not_metadata={4: 5})

        m = Metadatable(metadata={2: 3})
        self.assertEqual(m.metadata, {2: 3})
Ejemplo n.º 8
0
def test_init():
    with pytest.raises(TypeError):
        Metadatable(metadata={2: 3}, not_metadata={4: 5})

    m = Metadatable(metadata={2: 3})
    assert m.metadata == {2: 3}