forked from lnishan/SQLGitHub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.py
169 lines (141 loc) · 4.7 KB
/
table.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""A class to store tables.
Sample Usage:
table = SgTable()
table.Append([1, 2, 3])
table.Append([2, 4, 6])
table.Append([3, 6, 9])
for row in table:
print(row)
print(table[1])
table[1] = [2, 2, 2]
print(table[1])
table.SetFields(["a", "b", "c"])
print(table.GetVals("a"))
print(table.GetVals("b"))
print(table.GetVals("c"))
print(table[1:])
print(table[:2])
print(table[0:2:2])
"""
import itertools
class EscapeHtml:
MAPPING = {u"&": u"&",
u"<": u"<",
u">": u">",
u"\"": u""",
u"\'": u"'",
u"\n": u"<br>\n"}
@classmethod
def Escape(cls, ch):
return cls.MAPPING[ch] if cls.MAPPING.has_key(ch) else ch
@classmethod
def EscapeUnicodeStr(cls, unicode_str):
ret = u""
for ch in unicode_str:
ret += cls.Escape(ch)
return ret
class SgTable:
"""A class to store tables."""
def __init__(self):
self._fields = []
self._table = []
def __len__(self):
return len(self._table)
def __iter__(self):
for row in self._table:
yield row
def __getitem__(self, key):
if isinstance(key, slice):
return self._table[key.start:key.stop:key.step]
else:
if not ((type(key) == int or type(key) == long) and key >= 0 and key < len(self._table)):
raise ValueError("Index illegal")
else:
return self._table[key]
def __setitem__(self, key, value):
if not ((type(key) == int or type(key) == long) and key >= 0 and key < len(self._table)):
raise ValueError("Index illegal")
else:
self._table[key] = value
def __str__(self):
ret = str(self._fields)
for row in self._table:
ret += "\n" + str(row)
return ret
def __HasCommaOutOfString(self, val):
in_string = False
is_escaping = False
for ch in val:
if in_string:
if is_escaping:
is_escaping = False
elif ch == u"\\":
is_escaping = True
elif ch in (u"\"", u"\'"):
in_string = False
else:
if ch == u",":
return True
elif ch in (u"\"", u"\'"):
in_string = True
return False
def _GetCsvRepr(self, val):
if isinstance(val, list):
return u",".join(itertools.imap(self._GetCsvRepr, val))
else:
if isinstance(val, unicode):
if self.__HasCommaOutOfString(val) or u"\n" in val:
return u"\"" + val + u"\""
else:
return val
else:
return unicode(str(val), "utf-8")
def InCsv(self):
ret = self._GetCsvRepr(self._fields)
for row in self._table:
ret += u"\n" + self._GetCsvRepr(row)
return ret
def InHtml(self):
ret = u"<html>\n<head><meta charset=\"utf-8\">\n<title>SQLGitHub Result</title>\n</head>\n<body>\n"
ret += u"<table border=1>"
ret += u"<tr>"
for field in self._fields:
ret += u"<td>" + EscapeHtml.EscapeUnicodeStr(field) + u"</td>"
ret += u"</tr>\n"
for row in self._table:
ret += u"<tr>"
for val in row:
unicode_str = val if isinstance(val, unicode) else unicode(str(val), "utf-8")
ret += u"<td>" + EscapeHtml.EscapeUnicodeStr(unicode_str) + u"</td>"
ret += u"</tr>\n"
ret += u"</table>\n</html>"
return ret
def GetVals(self, field):
idx = [i for i, f in enumerate(self._fields) if f == field][0]
return [row[idx] for row in self._table]
def Copy(self, table):
self.SetFields(table.GetFields())
self.SetTable(table.GetTable())
def Append(self, row):
self._table.append(row)
def GetTable(self):
return self._table
def SetTable(self, table):
self._table = table
def GetFields(self):
return self._fields
def SetFields(self, fields):
self._fields = fields
def SliceCol(self, start, end):
table = SgTable()
table.SetFields(self._fields[start:end])
for row in self._table:
table.Append(row[start:end])
return table
def Chain(self, table):
res_table = SgTable()
res_table.SetFields(self._fields + table.GetFields())
rows = min(len(self._table), len(table))
for i in range(rows):
res_table.Append(self._table[i] + table[i])
return res_table