source: framspy/framsfiles/writer/_serializer.py @ 1178

Last change on this file since 1178 was 1104, checked in by Maciej Komosinski, 4 years ago

Added the "framsfiles" module for reading and writing Framsticks-format files (genotypes, settings, scrips, neurons, etc.)

File size: 1.1 KB
Line 
1_SERIALIZED_STRING = '@Serialized:{}'
2_DOUBLE_QUOTED = '"{}"'
3_FIELD_STRING = '"{}":{}'
4_LIST_STRING = '[{}]'
5_OBJECT_STRING = '{{{}}}'
6
7
8def _serialize_value(value):
9    return _SERIALIZED_STRING.format(_serialize(value))
10
11
12def _serialize(target):
13    if isinstance(target, list):
14        return _serialize_list(target)
15    if isinstance(target, dict):
16        return _serialize_object(target)
17    if isinstance(target, str):
18        return _serialize_string(target)
19    if target is None:
20        return 'null'
21    return str(target)
22
23
24def _serialize_list(target):
25    serialized = [_serialize(el) for el in target]
26    return _to_list_string(serialized)
27
28
29def _serialize_object(target):
30    serialized = [_to_field_string(k, v) for k, v in target.items()]
31    return _to_object_string(serialized)
32
33
34def _serialize_string(target):
35    return _DOUBLE_QUOTED.format(target)
36
37
38def _to_field_string(key, value):
39    return _FIELD_STRING.format(key, _serialize(value))
40
41
42def _to_list_string(target):
43    return _LIST_STRING.format(','.join(target))
44
45
46def _to_object_string(target):
47    return _OBJECT_STRING.format(','.join(target))
Note: See TracBrowser for help on using the repository browser.