forked from pythonql/pythonql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPQTuple.py
More file actions
69 lines (57 loc) · 1.59 KB
/
PQTuple.py
File metadata and controls
69 lines (57 loc) · 1.59 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
64
65
66
67
68
69
# The tuples that are used within the processor, as well as
# in the return values
def str_encode(string):
res = ""
for ch in string:
if ch == '"':
res += chr(92)
res += '"'
elif ch == chr(92):
res += chr(92)
res += chr(92)
else:
res += ch
return res
class PQTuple:
def __init__(self,tuple,schema):
self.tuple = tuple
self.schema = schema
def __getattr__(self,attr):
return self.tuple[ self.schema[attr] ]
def __getitem__(self,item):
if isinstance(item,int):
return self.tuple[item]
else:
return self.tuple[ self.schema[item] ]
def __iter__(self):
return self.tuple.__iter__()
def getDict(self):
res = {}
for v in self.schema:
res[ v ] = self.tuple[ self.schema[v] ]
return res
def __setitem__(self,item,value):
self.tuple[ self.schema[item] ] = value
def copy(self):
return PQTuple(list(self.tuple), self.schema)
def __eq__(self,other):
if isinstance(other, self.__class__):
if self.schema == other.schema:
return self.tuple == other.tuple
else:
return False
else:
return False
def __ne__(self,other):
return not self.__eq__(other)
def __hash__(self):
res = 0
for item in self.tuple:
res += hash(item)
return res
def __repr__(self):
#print(self.schema)
#print(self.tuple)
itms = list(self.schema.items())
itms.sort(key=lambda x:x[1])
return "{" + ",".join([ '"%s":%s' % (str_encode(i[0].lstrip().rstrip()), repr(self.tuple[i[1]])) for i in itms]) + "}"