示例#1
0
def serialize_data(data):
    """
    Serialize a value or collection that may potentially contain AiiDA nodes, which
    will be serialized to their UUID. Keys encountered in any mappings, such as a dictionary,
    will also be encoded if necessary. An example is where tuples are used as keys in the
    pseudo potential input dictionaries. These operations will ensure that the returned data is
    JSON serializable.

    :param data: a single value or collection
    :return: the serialized data with the same internal structure
    """
    if isinstance(data, Node):
        return '{}{}'.format(_PREFIX_VALUE_NODE, data.uuid)
    elif isinstance(data, Group):
        return '{}{}'.format(_PREFIX_VALUE_GROUP, data.uuid)
    elif isinstance(data, AttributeDict):
        return AttributeDict({encode_key(key): serialize_data(value) for key, value in data.iteritems()})
    elif isinstance(data, AttributesFrozendict):
        return AttributesFrozendict({encode_key(key): serialize_data(value) for key, value in data.iteritems()})
    elif isinstance(data, collections.Mapping):
        return {encode_key(key): serialize_data(value) for key, value in data.iteritems()}
    elif isinstance(data, collections.Sequence) and not isinstance(data, (str, unicode)):
        return [serialize_data(value) for value in data]
    else:
        return data
示例#2
0
def deserialize_data(data):
    """
    Deserialize a single value or a collection that may contain serialized AiiDA nodes. This is
    essentially the inverse operation of serialize_data which will reload node instances from
    the serialized UUID data. Encoded tuples that are used as dictionary keys will be decoded.

    :param data: serialized data
    :return: the deserialized data with keys decoded and node instances loaded from UUID's
    """
    if isinstance(data, AttributeDict):
        return AttributeDict({
            decode_key(key): deserialize_data(value)
            for key, value in data.iteritems()
        })
    elif isinstance(data, AttributesFrozendict):
        return AttributesFrozendict({
            decode_key(key): deserialize_data(value)
            for key, value in data.iteritems()
        })
    elif isinstance(data, collections.Mapping):
        return {
            decode_key(key): deserialize_data(value)
            for key, value in data.iteritems()
        }
    elif isinstance(data, collections.Sequence) and not isinstance(
            data, (str, unicode)):
        return [deserialize_data(value) for value in data]
    elif isinstance(data,
                    (str, unicode)) and data.startswith(_PREFIX_VALUE_NODE):
        return load_node(uuid=data[len(_PREFIX_VALUE_NODE):])
    elif isinstance(data,
                    (str, unicode)) and data.startswith(_PREFIX_VALUE_GROUP):
        return load_group(uuid=data[len(_PREFIX_VALUE_GROUP):])
    else:
        return data
示例#3
0
    def test_getitem(self):
        d = AttributesFrozendict({'a': 5})
        self.assertEqual(d['a'], 5)

        with self.assertRaises(KeyError):
            d['b']
示例#4
0
 def test_setitem(self):
     d = AttributesFrozendict()
     with self.assertRaises(TypeError):
         d['a'] = 5
示例#5
0
    def test_getattr(self):
        d = AttributesFrozendict({'a': 5})
        self.assertEqual(d.a, 5)

        with self.assertRaises(AttributeError):
            d.b