-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython2datalisp.py
More file actions
executable file
·63 lines (48 loc) · 1.43 KB
/
python2datalisp.py
File metadata and controls
executable file
·63 lines (48 loc) · 1.43 KB
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
55
56
57
58
59
60
61
62
63
import pydatalisp as dl
import numbers
# Only useful with dict!
class Importer:
def __init__(self, data):
self.data = data
def _toData(self, obj):
data = dl.Data()
if isinstance(obj, (str, unicode)):
data.string = str(obj)
elif isinstance(obj, bool):
data.bool = bool(obj)
elif isinstance(obj, (int, long)):
data.int = int(obj)
elif isinstance(obj, numbers.Real):
data.float = float(obj)
elif data:
data.group = self._toGroup(obj)
return data
def _toGroup(self, obj):
if isinstance(obj, dict):
id = '__unknown__'
if '__id__' in obj:
id = obj['__id__']
elif isinstance(obj, list):
id = ''
obj = dict(obj)
else:
id = type(obj).__name__
obj = dict(obj)
grp = dl.DataGroup(id)
for key in obj:
if key == "__id__":
continue
d = self._toData(obj[key])
d.key = str(key)
grp.addData(d)
return grp
def run(self):
self.container = dl.DataContainer()
for grp in self.data:
d = self._toGroup(self.data[grp])
d.id = str(grp)
self.container.addTopGroup(d)
return self.container
def run(obj):
imp = Importer(obj)
return imp.run()