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
96 lines (80 loc) · 2.3 KB
/
Copy pathPQTuple.py
File metadata and controls
96 lines (80 loc) · 2.3 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
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
# 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 __lt__(self,other):
if isinstance(other, self.__class__):
if self.schema == other.schema:
return self.tuple < other.tuple
else:
return False
elif isinstance(other, tuple):
return self.tuple < other
else:
return False
def __eq__(self,other):
if isinstance(other, self.__class__):
if self.schema == other.schema:
return self.tuple == other.tuple
else:
return False
elif isinstance(other, tuple):
return self.tuple == other
else:
return False
def __gt__(self,other):
if isinstance(other, self.__class__):
if self.schema == other.schema:
return self.tuple > other.tuple
else:
return False
elif isinstance(other, tuple):
return self.tuple > other
else:
return False
def __le__(self,other):
return self.__gt__(other) or self.__eq__(other)
def __ge__(self,other):
return self.__gt__(other) or self.__eq__(other)
def __ne__(self,other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.tuple)
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]) + "}"