-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.py
54 lines (44 loc) · 1.77 KB
/
serialize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''Helper module for serializing and deserializing stereonet data.'''
from math import degrees, radians
from grouping import DataGroup
from transformation import Line, Plane, Rotation
def stereonet_object_encoder(obj):
'''A JSON encoder that handles stereonet objects.
Designed to be passed as default= to the JSONEncoder constructor.
'''
tries = [
# DataGroup
lambda o: {'name': o.name, 'enabled': o.enabled, 'style': o.style,
'data': o.net_objects()},
# Line
lambda o: {'plunge': degrees(o.plunge), 'trend': degrees(o.trend)},
# Plane
lambda o: {'strike': degrees(o.strike), 'dip': degrees(o.dip)},
# Rotation
lambda o: {'rotation_axis': o.rot_axis, 'base_line': o.base_line},
# tk.*Var
lambda o: o.get(),
]
for serialize in tries:
try:
return serialize(obj)
except AttributeError:
continue
raise TypeError(f'{type(obj).__name__} {obj}')
def stereonet_object_decoder(obj):
'''A JSON decoder that correctly handles stereonet objects.
Designed to be passed as object_hook= to the JSONDecoder constructor.
'''
if 'name' in obj and 'enabled' in obj and 'style' in obj and 'data' in obj:
group = DataGroup(obj['name'], enabled=obj['enabled'], **obj['style'])
for data in obj['data']:
group.add_net_object(data)
return group
if 'plunge' in obj and 'trend' in obj:
return Line(**{k: radians(v) for k, v in obj.items()})
if 'strike' in obj and 'dip' in obj:
return Plane(**{k: radians(v) for k, v in obj.items()})
if 'rotation_axis' in obj and 'base_line' in obj:
obj['rot_axis'] = obj.pop('rotation_axis')
return Rotation(**obj)
return obj