def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     repetition_count = self.__repetition_count
     if isinstance(repetition_count, ParameterDeclaration):
         repetition_count = serializer.dictify(repetition_count)
     return dict(type=serializer.get_type_identifier(self),
                 body=serializer.dictify(self.__body),
                 repetition_count=repetition_count)
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     subtemplates = []
     for subtemplate, channel_mapping in self.__subtemplates:
         mapping_functions = self.__parameter_mapping.get_template_map(subtemplate)
         mapping_function_strings = \
             {k: serializer.dictify(m) for k, m in mapping_functions.items()}
         subtemplate = serializer.dictify(subtemplate)
         subtemplates.append(dict(template=subtemplate,
                                  parameter_mappings=mapping_function_strings,
                                  channel_mappings=channel_mapping))
     return dict(subtemplates=subtemplates,
                 external_parameters=sorted(list(self.parameter_names)))
Esempio n. 3
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     data = dict(
         type=serializer.get_type_identifier(self),
         condition=self.__condition,
         body=serializer.dictify(self.__body)
     )
     return data
Esempio n. 4
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     parameter_mapping_dict = dict(
         (key, str(expression))
         for key, expression in self.__parameter_mapping.items())
     return dict(template=serializer.dictify(self.template),
                 parameter_mapping=parameter_mapping_dict,
                 measurement_mapping=self.__measurement_mapping,
                 channel_mapping=self.__channel_mapping)
Esempio n. 5
0
    def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
        data = dict()
        data['external_parameters'] = sorted(list(self.parameter_names))
        data['is_interruptable'] = self.is_interruptable

        subtemplates = []
        for subtemplate in self.__subtemplates:
            mapping_functions = self.__parameter_mapping.get_template_map(
                subtemplate)
            mapping_functions_strings = \
                {k: serializer.dictify(m) for k, m in mapping_functions.items()}
            subtemplate = serializer.dictify(subtemplate)
            subtemplates.append(
                dict(template=subtemplate, mappings=mapping_functions_strings))
        data['subtemplates'] = subtemplates

        data['type'] = serializer.get_type_identifier(self)
        return data
Esempio n. 6
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     data = dict(body=serializer.dictify(self.body),
                 repetition_count=self.repetition_count.original_expression)
     if self.parameter_constraints:
         data['parameter_constraints'] = [
             str(c) for c in self.parameter_constraints
         ]
     if self.measurement_declarations:
         data['measurements'] = self.measurement_declarations
     return data
Esempio n. 7
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     data = dict()
     data['is_measurement_pulse'] = self.__is_measurement_pulse
     data['time_parameter_declarations'] = \
         [serializer.dictify(self.__time_parameter_declarations[key])
          for key in sorted(self.__time_parameter_declarations.keys())]
     data['voltage_parameter_declarations'] = \
         [serializer.dictify(self.__voltage_parameter_declarations[key])
          for key in sorted(self.__voltage_parameter_declarations.keys())]
     serialized_entries = []
     for channel in self.__entries:
         entries = []
         for (time, voltage, interpolation) in channel:
             if isinstance(time, ParameterDeclaration):
                 time = time.name
             if isinstance(voltage, ParameterDeclaration):
                 voltage = voltage.name
             entries.append((time, voltage, str(interpolation)))
         serialized_entries.append(entries)
     data['entries'] = serialized_entries
     data['type'] = serializer.get_type_identifier(self)
     return data
Esempio n. 8
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     data = dict(
         body=serializer.dictify(self.body),
         loop_range=self._loop_range.to_tuple(),
         loop_index=self._loop_index,
     )
     if self.parameter_constraints:
         data['parameter_constraints'] = [
             str(c) for c in self.parameter_constraints
         ]
     if self.measurement_declarations:
         data['measurements'] = self.measurement_declarations
     return data
    def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
        data = dict(subtemplates=[
            serializer.dictify(subtemplate)
            for subtemplate in self.subtemplates
        ])

        if self.parameter_constraints:
            data['parameter_constraints'] = [
                str(constraint) for constraint in self.parameter_constraints
            ]

        if self.measurement_declarations:
            data['measurements'] = self.measurement_declarations
        return data
Esempio n. 10
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     return dict(data=serializer.dictify(self.data))
Esempio n. 11
0
class SerializerTests(unittest.TestCase):
    def setUp(self) -> None:
        self.backend = DummyStorageBackend()
        self.serializer = Serializer(self.backend)
        self.deserialization_data = dict(
            data='THIS IS DARTAA!',
            type=self.serializer.get_type_identifier(DummySerializable()))

    def test_serialize_subpulse_no_identifier(self) -> None:
        serializable = DummySerializable(data='bar')
        serialized = self.serializer.dictify(serializable)
        expected = serializable.get_serialization_data(self.serializer)
        expected['type'] = self.serializer.get_type_identifier(serializable)
        self.assertEqual(expected, serialized)

    def test_serialize_subpulse_identifier(self) -> None:
        serializable = DummySerializable(identifier='bar')
        serialized = self.serializer.dictify(serializable)
        self.assertEqual(serializable.identifier, serialized)

    def test_serialize_subpulse_duplicate_identifier(self) -> None:
        serializable = DummySerializable(identifier='bar')
        self.serializer.dictify(serializable)
        self.serializer.dictify(serializable)
        serializable = DummySerializable(data='this is other data than before',
                                         identifier='bar')
        with self.assertRaises(Exception):
            self.serializer.dictify(serializable)

    def test_collection_dictionaries_no_identifier(self) -> None:
        serializable = DummySerializable(data='bar')
        dictified = self.serializer._Serializer__collect_dictionaries(
            serializable)
        expected = {'': serializable.get_serialization_data(self.serializer)}
        expected['']['type'] = self.serializer.get_type_identifier(
            serializable)
        self.assertEqual(expected, dictified)

    def test_collection_dictionaries_identifier(self) -> None:
        serializable = DummySerializable(data='bar', identifier='foo')
        dicified = self.serializer._Serializer__collect_dictionaries(
            serializable)
        expected = {
            serializable.identifier:
            serializable.get_serialization_data(self.serializer)
        }
        expected[serializable.identifier][
            'type'] = self.serializer.get_type_identifier(serializable)
        self.assertEqual(expected, dicified)

    def test_dicitify_no_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable)
        dicitified = self.serializer._Serializer__collect_dictionaries(
            serializable)
        expected = {'': serializable.get_serialization_data(self.serializer)}
        expected['']['type'] = self.serializer.get_type_identifier(
            serializable)
        self.assertEqual(expected, dicitified)

    def test_collection_dictionaries_no_identifier_one_nesting_identifier(
            self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable)
        dicitified = self.serializer._Serializer__collect_dictionaries(
            serializable)
        expected = {
            '':
            serializable.get_serialization_data(self.serializer),
            inner_serializable.identifier:
            inner_serializable.get_serialization_data(self.serializer)
        }
        expected['']['type'] = self.serializer.get_type_identifier(
            serializable)
        expected[inner_serializable.identifier][
            'type'] = self.serializer.get_type_identifier(inner_serializable)
        self.assertEqual(expected, dicitified)

    def test_collection_dictionaries_identifier_one_nesting_no_identifier(
            self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable,
                                               identifier='outer_foo')
        dicitified = self.serializer._Serializer__collect_dictionaries(
            serializable)
        expected = {
            serializable.identifier:
            serializable.get_serialization_data(self.serializer)
        }
        expected[serializable.identifier][
            'type'] = self.serializer.get_type_identifier(serializable)
        self.assertEqual(expected, dicitified)

    def test_collection_dictionaries_identifier_one_nesting_identifier(
            self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable,
                                               identifier='outer_foo')
        dicitified = self.serializer._Serializer__collect_dictionaries(
            serializable)
        expected = {
            inner_serializable.identifier:
            inner_serializable.get_serialization_data(self.serializer),
            serializable.identifier:
            serializable.get_serialization_data(self.serializer)
        }
        expected[serializable.identifier][
            'type'] = self.serializer.get_type_identifier(serializable)
        expected[inner_serializable.identifier][
            'type'] = self.serializer.get_type_identifier(inner_serializable)
        self.assertEqual(expected, dicitified)

    def __serialization_test_helper(self, serializable: Serializable,
                                    expected: Dict[str, str]) -> None:
        self.serializer.serialize(serializable)
        expected = {
            k: json.dumps(v, indent=4, sort_keys=True)
            for k, v in expected.items()
        }
        self.assertEqual(expected, self.backend.stored_items)

    def test_serialize_no_identifier(self) -> None:
        serializable = DummySerializable(data='bar')
        expected = {
            'main': serializable.get_serialization_data(self.serializer)
        }
        expected['main']['type'] = self.serializer.get_type_identifier(
            serializable)
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_identifier(self) -> None:
        serializable = DummySerializable(data='bar', identifier='foo')
        expected = {
            serializable.identifier:
            serializable.get_serialization_data(self.serializer)
        }
        expected[serializable.identifier][
            'type'] = self.serializer.get_type_identifier(serializable)
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_no_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable)
        expected = {
            'main': serializable.get_serialization_data(self.serializer)
        }
        expected['main']['type'] = self.serializer.get_type_identifier(
            serializable)
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_no_identifier_one_nesting_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable)
        expected = {
            'main':
            serializable.get_serialization_data(self.serializer),
            inner_serializable.identifier:
            inner_serializable.get_serialization_data(self.serializer)
        }
        expected['main']['type'] = self.serializer.get_type_identifier(
            serializable)
        expected[inner_serializable.identifier][
            'type'] = self.serializer.get_type_identifier(inner_serializable)
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable,
                                               identifier='outer_foo')
        expected = {
            serializable.identifier:
            serializable.get_serialization_data(self.serializer)
        }
        expected[serializable.identifier][
            'type'] = self.serializer.get_type_identifier(serializable)
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_identifier_one_nesting_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable,
                                               identifier='outer_foo')
        expected = {
            serializable.identifier:
            serializable.get_serialization_data(self.serializer),
            inner_serializable.identifier:
            inner_serializable.get_serialization_data(self.serializer)
        }
        expected[serializable.identifier][
            'type'] = self.serializer.get_type_identifier(serializable)
        expected[inner_serializable.identifier][
            'type'] = self.serializer.get_type_identifier(inner_serializable)
        self.__serialization_test_helper(serializable, expected)

    def test_deserialize_dict(self) -> None:
        deserialized = self.serializer.deserialize(self.deserialization_data)
        self.assertIsInstance(deserialized, DummySerializable)
        self.assertEqual(self.deserialization_data['data'], deserialized.data)

    def test_deserialize_identifier(self) -> None:
        jsonized_data = json.dumps(self.deserialization_data,
                                   indent=4,
                                   sort_keys=True)
        identifier = 'foo'
        self.backend.put(identifier, jsonized_data)

        deserialized = self.serializer.deserialize(identifier)
        self.assertIsInstance(deserialized, DummySerializable)
        self.assertEqual(self.deserialization_data['data'], deserialized.data)

    def test_serialization_and_deserialization_combined(self) -> None:
        table_foo = TablePulseTemplate(
            identifier='foo',
            entries={'default': [('hugo', 2), ('albert', 'voltage')]},
            parameter_constraints=['albert<9.1'])
        table = TablePulseTemplate({'default': [('t', 0)]})

        foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage')
        sequence = SequencePulseTemplate(
            (table_foo, foo_mappings, dict()), (table, dict(t=0), dict()),
            external_parameters=['ilse', 'albert', 'voltage'],
            identifier=None)

        storage = DummyStorageBackend()
        serializer = Serializer(storage)
        serializer.serialize(sequence)

        serialized_foo = storage.stored_items['foo']
        serialized_sequence = storage.stored_items['main']

        deserialized_sequence = serializer.deserialize('main')
        storage.stored_items = dict()
        serializer.serialize(deserialized_sequence)

        self.assertEqual(serialized_foo, storage.stored_items['foo'])
        self.assertEqual(serialized_sequence, storage.stored_items['main'])
    def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
        data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates],
                    parameter_constraints=self.parameter_constraints)

        return data
 def get_serialization_data(self, serializer: Serializer) -> None:
     return dict(duration_expression=serializer.dictify(
         self.__duration_expression),
                 expression=serializer.dictify(self.__expression),
                 measurement=self.__is_measurement_pulse)
class SerializerTests(unittest.TestCase):

    def setUp(self) -> None:
        self.backend = DummyStorageBackend()
        self.serializer = Serializer(self.backend)
        self.deserialization_data = dict(data='THIS IS DARTAA!',
                                         type=self.serializer.get_type_identifier(DummySerializable()))

    def test_serialize_subpulse_no_identifier(self) -> None:
        serializable = DummySerializable(data='bar')
        serialized = self.serializer._serialize_subpulse(serializable)
        self.assertEqual(serializable.get_serialization_data(self.serializer), serialized)

    def test_serialize_subpulse_identifier(self) -> None:
        serializable = DummySerializable(identifier='bar')
        serialized = self.serializer._serialize_subpulse(serializable)
        self.assertEqual(serializable.identifier, serialized)

    def test_serialize_subpulse_duplicate_identifier(self) -> None:
        serializable = DummySerializable(identifier='bar')
        self.serializer._serialize_subpulse(serializable)
        self.serializer._serialize_subpulse(serializable)
        serializable = DummySerializable(data='this is other data than before', identifier='bar')
        with self.assertRaises(Exception):
            self.serializer._serialize_subpulse(serializable)

    def test_dictify_no_identifier(self) -> None:
        serializable = DummySerializable(data='bar')
        dictified = self.serializer.dictify(serializable)
        self.assertEqual({'': serializable.get_serialization_data(self.serializer)}, dictified)

    def test_dictify_identifier(self) -> None:
        serializable = DummySerializable(data='bar', identifier='foo')
        dicified = self.serializer.dictify(serializable)
        self.assertEqual({serializable.identifier: serializable.get_serialization_data(self.serializer)}, dicified)

    def test_dicitify_no_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable)
        dicitified = self.serializer.dictify(serializable)
        self.assertEqual({'': serializable.get_serialization_data(self.serializer)}, dicitified)

    def test_dictify_no_identifier_one_nesting_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable)
        dicitified = self.serializer.dictify(serializable)
        self.assertEqual({'': serializable.get_serialization_data(self.serializer),
                          inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer)},
                         dicitified)

    def test_dictify_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo')
        dicitified = self.serializer.dictify(serializable)
        self.assertEqual({serializable.identifier: serializable.get_serialization_data(self.serializer)}, dicitified)

    def test_dictify_identifier_one_nesting_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo')
        dicitified = self.serializer.dictify(serializable)
        self.assertEqual({inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer),
                          serializable.identifier: serializable.get_serialization_data(self.serializer)},
                         dicitified)

    def __serialization_test_helper(self, serializable: Serializable, expected: Dict[str, str]) -> None:
        self.serializer.serialize(serializable)
        expected = {k: json.dumps(v, indent=4, sort_keys=True) for k,v in expected.items()}
        self.assertEqual(expected, self.backend.stored_items)

    def test_serialize_no_identifier(self) -> None:
        serializable = DummySerializable(data='bar')
        expected = {'main': serializable.get_serialization_data(self.serializer)}
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_identifier(self) -> None:
        serializable = DummySerializable(data='bar', identifier='foo')
        expected = {serializable.identifier: serializable.get_serialization_data(self.serializer)}
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_no_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable)
        expected = {'main': serializable.get_serialization_data(self.serializer)}
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_no_identifier_one_nesting_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable)
        expected = {'main': serializable.get_serialization_data(self.serializer),
                    inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer)}
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_identifier_one_nesting_no_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar')
        serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo')
        expected = {serializable.identifier: serializable.get_serialization_data(self.serializer)}
        self.__serialization_test_helper(serializable, expected)

    def test_serialize_identifier_one_nesting_identifier(self) -> None:
        inner_serializable = DummySerializable(data='bar', identifier='foo')
        serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo')
        expected = {serializable.identifier: serializable.get_serialization_data(self.serializer),
                    inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer)}
        self.__serialization_test_helper(serializable, expected)

    def test_deserialize_dict(self) -> None:
        deserialized = self.serializer.deserialize(self.deserialization_data)
        self.assertIsInstance(deserialized, DummySerializable)
        self.assertEqual(self.deserialization_data['data'], deserialized.data)

    def test_deserialize_identifier(self) -> None:
        jsonized_data = json.dumps(self.deserialization_data, indent=4, sort_keys=True)
        identifier = 'foo'
        self.backend.put(identifier, jsonized_data)

        deserialized = self.serializer.deserialize(identifier)
        self.assertIsInstance(deserialized, DummySerializable)
        self.assertEqual(self.deserialization_data['data'], deserialized.data)

    def test_serialization_and_deserialization_combined(self) -> None:
        table_foo = TablePulseTemplate(identifier='foo')
        table_foo.add_entry('hugo', 2)
        table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage')
        table = TablePulseTemplate(measurement=True)
        foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage')
        sequence = SequencePulseTemplate([(table_foo, foo_mappings), (table, {})], ['ilse', 'albert', 'voltage'], identifier=None)

        storage = DummyStorageBackend()
        serializer = Serializer(storage)
        serializer.serialize(sequence)

        serialized_foo = storage.stored_items['foo']
        serialized_sequence = storage.stored_items['main']

        deserialized_sequence = serializer.deserialize('main')
        storage.stored_items = dict()
        serializer.serialize(deserialized_sequence)

        self.assertEqual(serialized_foo, storage.stored_items['foo'])
        self.assertEqual(serialized_sequence, storage.stored_items['main'])
Esempio n. 15
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     return dict(if_branch_template=serializer.dictify(self.__if_branch),
                 else_branch_template=serializer.dictify(
                     self.__else_branch),
                 condition=self.__condition)
Esempio n. 16
0
 def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]:
     return dict(type=serializer.get_type_identifier(self),
                 expression=serializer.dictify(self.__expression))