-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
193 lines (157 loc) · 5.83 KB
/
builder.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#coding: utf-8
from utils import expand, real_len, TAB_WIDTH
class Desc(object):
'''Column description.
:param name: the name of the column.
:param random_generator: A random geneartor, which returns somethings
randomly basic on given dataset and previous
generated data.
:param dataset: dataset for generating random data.
:param children: declares and gives the descriptions for the sub dataset.
:param unique: declares whether the column is unique.
'''
def __init__(self, name, random_generator, dataset=None, children=None,
unique=None):
self.name = name
self.random_generator = random_generator
self.dataset = dataset
self.children = children
self.unique = True if unique else False
def __str__(self):
if self.unique:
return '<Desc(%s unique)>' % (self.name)
else:
return '<Desc(%s)>' % (self.name)
def __repr__(self):
return self.__str__()
class Data(dict):
'''A data row in the dataset. Just like a dict object, but the key order is
fixed rather than sort by hash.'''
def __init__(self, header):
self.header = header
for key in header:
self[key] = None
def __str__(self):
return self.table
def __repr__(self):
return self.__str__()
def keys(self):
#: Fix key's order rather than base on hash
return self.header
def items(self):
return [(key, self[key]) for key in self.keys()]
@property
def table(self):
'''Generate a mysql like result table'''
# more pythonic approach for assigning value to multi vars?
header, body, sep = [''], [''], ['']
for key, value in self.items():
if isinstance(value, Dataset):
value = value.summarize()
value = str(value)
width = real_len(max(len(key), len(value)))
header.append(expand(key, width))
body.append(expand(value, width))
sep.append('-' * (width + TAB_WIDTH))
for i in (header, body, sep):
i.append('')
header = '|'.join(header)
body = '|'.join(body)
sep = '+'.join(sep)
return '\n'.join((sep, header, sep, body, sep))
class Dataset(object):
'''A dataset.'''
def __init__(self, header, body):
self.header = header
self.body = body
def __len__(self):
return len(self.body)
def __getitem__(self, idx):
return self.body[idx]
def __setitem__(self, idx, value):
self.body[idx] = value
def __delitem__(self, idx):
del self.body[idx]
def __str__(self):
return self.table
def __repr__(self):
return self.__str__()
@property
def table(self):
'''Generate a mysql like result table'''
header, body, sep = [''], [], ['']
#: find out the max length of word of each column
max_len = {}
for key in self.header:
width = len(str(key))
for line in self.body:
if isinstance(line[key], Dataset):
value = line[key].summarize()
else:
value = str(line[key])
width = max(width, len(value))
max_len[key] = real_len(width)
#: build the header and sep in the same time
header.append(expand(key, max_len[key]))
sep.append('-' * (max_len[key] + TAB_WIDTH))
for i in (header, sep):
i.append('')
header = '|'.join(header)
sep = '+'.join(sep)
#: build body
for line in self.body:
b = ['']
for key, value in line.items():
if isinstance(value, Dataset):
value = value.summarize()
else:
value = str(value)
width = max_len[key]
b.append(expand(value, width))
b.append('')
body.append('|'.join(b))
#: build table
table = [sep, header, sep]
for line in body:
table.append(line)
table.append(sep)
return '\n'.join(table)
def append(self, item):
self.body.append(item)
@property
def count(self):
return len(self)
@staticmethod
def fromheader(header):
return Dataset(header, [])
@staticmethod
def fromdescs(descs, count):
'''Build dataset from given descriptions'''
header = [desc.name for desc in descs]
dataset = Dataset.fromheader(header)
for i in xrange(0, count):
d = Data(header)
for desc in descs:
if desc.children:
#: build sub dataset
d[desc.name] = Dataset.fromdescs(
desc.children, desc.random_generator())
else:
data = desc.random_generator(desc.dataset, d)
if desc.unique:
while not dataset.is_unique(desc.name, data):
data = desc.random_generator(desc.dataset, d)
d[desc.name] = data
dataset.append(d)
return dataset
def get_column(self, row_name):
'''Get specific column'''
return [i[row_name] for i in self]
def is_unique(self, name, data):
'''Check is the data in the row'''
return not (data in self.get_column(name))
def show(self):
print 'count: %d\n%s' % (self.count, self.table)
def summarize(self):
'''Generate summary'''
return '[%d]' % len(self)