-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuples_set.py
69 lines (58 loc) · 1.64 KB
/
tuples_set.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
import config
import struct
import tuples_read
import vertex
def set_vertex(cdir, vid, sv, name, val):
vfile = cdir + '/' + config.VERTEX_FILE
try:
vfd = open(vfile, 'rb+')
except:
return
# Search for the specified vertex id. If found, the file pointer
# is left at the beginning of the tuple data
if not vertex.find(cdir, vid, vfd):
vfd.close()
return
# Search to the specified attribute name
for attrtype, attrname in sv:
if name == attrname:
# Replace the value of the attribute
if attrtype == 'INT':
valint = int(val)
b = bytearray(struct.pack('q', valint))
vfd.write(b)
elif attrtype == 'UINT':
valint = int(val)
valuint = valint % 2**64
b = bytearray(struct.pack('Q', valuint))
vfd.write(b)
elif attrtype == 'FLOAT':
valfloat = float(val)
b = bytearray(struct.pack('d', valfloat))
vfd.write(b)
elif attrtype == 'DOUBLE':
valdouble = float(val)
b = bytearray(struct.pack('d', valdouble))
vfd.write(b)
elif attrtype == 'CHAR':
b = ord(val[0]).to_bytes(1, byteorder='little', signed=False)
vfd.write(b)
elif attrtype == 'STRING':
b = bytearray()
b.extend(map(ord, val))
# Write string length
blen = struct.pack('H', len(b))
vfd.write(blen)
# Write string padded out to max string length
s = bytearray()
s.extend(map(ord, val.ljust(config.BASE_TYPE_SIZES['STRING'])))
vfd.write(s)
elif attrtype == 'DATE':
vfd.write(bytes(val, 'utf-8'))
elif attrtype == 'TIME':
vfd.write(bytes(val, 'utf-8'))
else:
tuples_read.read_attribute(attrtype, vfd)
vfd.close()
def set_edge(cdir, vid1, vid2, se, name, val):
return