-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuples.py
96 lines (85 loc) · 1.92 KB
/
tuples.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import config
import struct
def dump(s, fd):
if not s:
return
# Iterate through the attributes of the schema and print the values
# associated with the current tuple
first = True
print('[', end='')
for attrtype, attrname in s:
if attrtype == 'INT':
b = fd.read(config.BASE_TYPE_SIZES['INT'])
if not b:
break
if not first:
print(',', end='')
first = False
print(struct.unpack('<q', b)[0], end='')
elif attrtype == 'UINT':
b = fd.read(config.BASE_TYPE_SIZES['UINT'])
if not b:
break
if not first:
print(',', end='')
first = False
print(struct.unpack('<Q', b)[0], end='')
elif attrtype == 'FLOAT':
b = fd.read(config.BASE_TYPE_SIZES['FLOAT'])
if not b:
break
if not first:
print(',', end='')
first = False
d = struct.unpack('d', b)[0]
print(d, end='')
elif attrtype == 'DOUBLE':
b = fd.read(config.BASE_TYPE_SIZES['DOUBLE'])
if not b:
break
if not first:
print(',', end='')
first = False
d = struct.unpack('d', b)[0]
print(d, end='')
elif attrtype == 'CHAR':
b = fd.read(config.BASE_TYPE_SIZES['CHAR'])
if not b:
break
if not first:
print(',', end='')
first = False
print(str(b), end='')
elif attrtype == 'STRING':
b = fd.read(2)
len = struct.unpack('<H', b)[0]
if len > 0:
b = fd.read(config.BASE_TYPE_SIZES['STRING'])
if not b:
break
if not first:
print(',', end='')
first = False
if len == 0:
print('\'\'', end='')
else:
print('\'' + b[:len].decode('utf-8') + '\'', end='')
elif attrtype == 'DATE':
b = fd.read(config.BASE_TYPE_SIZES['DATE'])
if not b:
break
if not first:
print(',', end='')
first = False
s = str(b)
print(s, end='')
elif attrtype == 'TIME':
b = fd.read(config.BASE_TYPE_SIZES['TIME'])
if not b:
break
if not first:
print(',', end='')
first = False
s = str(b)
print(s, end='')
print(']', end='')