-
Notifications
You must be signed in to change notification settings - Fork 0
/
randgen.py
executable file
·187 lines (143 loc) · 4.7 KB
/
randgen.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
#!/usr/bin/python
import sys, random
# Attended in the catch block
generator = None
try:
# Available values
formats = [ "plain", "csv", "json" ]
col_types = [ "name", "firstname", "surname", "email" ]
emails = [ "@gmail.com", "@outlook.com", "@hotmail.com", "@mail.com", "@yahoo.com" ]
stderr = sys.stderr
output = sys.stdout
# Infinite source of names
# __next__ returns a dictionary of values to be formatted by this class' implementations
class RandomTupleGenerator:
def __init__(self, names, surnames, cols, iterations):
self.names = names # collection of firstnames to pick from
self.surnames = surnames # collection of surnames to pick from
self.cols = cols # column labels
self.iterations = iterations # maximum iterations. -1 for infinity
self.idx = 0 # iteration counter
# Prevent collision of unique values
self.emails = []
def __iter__(self):
return self
def __next__(self):
if self.iterations == 0:
raise StopIteration()
if self.iterations > 0:
self.iterations -= 1
self.idx += 1
attributes = {
'firstname' : random.choice(self.names),
'surname' : random.choice(self.surnames)
}
attributes.update({'name': "%s %s" % (attributes["firstname"], attributes["surname"])})
# Generate pseudo-random email and check if it already exists
while True:
email = "%s%d%s" % (attributes["name"].lower().replace(" ", random.choice([ "", "-", "_" ])), random.randint(0, 999), random.choice(emails))
if self.emails.count(email) == 0:
self.emails.append(email)
attributes.update({'email' : email})
break
return attributes
class RandomJsonGenerator(RandomTupleGenerator):
def __next__(self):
attributes = RandomTupleGenerator.__next__(self)
elem = "{"
if self.idx == 1:
elem = "[\n{"
for c in self.cols:
elem += ' "%s": "%s",' % (c, attributes[c])
elem = elem[:-1] # trim last ","
elem += " },"
if self.iterations == 0:
elem = elem[:-1] + "\n]"
return elem
class RandomCsvGenerator(RandomTupleGenerator):
def __next__(self):
attributes = RandomTupleGenerator.__next__(self)
elem = ""
if self.idx == 1:
elem += ",".join(cols) + "\n"
for c in self.cols:
elem += '"' + attributes[c] + "\","
return elem[:-1] # trim last ","
class RandomPlainGenerator(RandomTupleGenerator):
def __next__(self):
attributes = RandomTupleGenerator.__next__(self)
elem = ""
for c in self.cols:
elem += attributes[c] + " "
return elem[:-1] # trim last " "
def pick_generator(firstnames, surnames, cols, iterations, outformat):
if outformat == "json":
return RandomJsonGenerator(firstnames, surnames, cols, iterations)
if outformat == "csv":
return RandomCsvGenerator(firstnames, surnames, cols, iterations)
return RandomPlainGenerator(firstnames, surnames, cols, iterations)
# Save file contents in a list and close handle ASAP
def file_to_list(filename):
f = open(filename, "r", encoding="utf-8")
arr = []
for l in f:
arr.append(l.strip('\n'))
f.close()
return arr
# Properties and arguments' interpretation
## Defaults
lang = "us"
filename = None # default = stdout
iterations = -1 # default = infinity
outformat = formats[0]
cols = [ col_types[0] ]
for arg in sys.argv:
pair = arg.split('=')
if len(pair) != 2:
continue
prop = pair[0]
value = pair[1]
if prop == "lang":
lang = value
elif prop == "file":
filename = value
elif prop == "iter":
iterations = int(value)
elif prop == "format":
if formats.count(value) == 0:
print("'%s' is not an available format. Using 'plain', instead." % value, file=stderr)
else:
outformat = value
elif prop == "cols":
mycols = value.split(',')
ret = []
for c in mycols:
if col_types.count(c) == 0:
print("'%s' is not an available column type. Skipping." % c, file=stderr)
continue
ret.append(c)
if len(ret) > 0:
cols = ret
del sys
# Store all names in auxiliar collections
try:
firstnames = file_to_list("names/%s/boynames" % lang) + file_to_list("names/%s/girlnames" % lang)
surnames = file_to_list("names/%s/surnames" % lang)
except FileNotFoundError:
print("Language %s is not supported" % lang, file=stderr)
quit(128)
# Printing output is stdout (default), or a specified file
if filename != None:
output = open(filename, "a", encoding="utf-8")
# Iterate through a random tuple source
generator = pick_generator(firstnames, surnames, cols, iterations, outformat)
for l in generator:
print(l, file=output)
output.close()
except KeyboardInterrupt:
if generator is not None:
generator.iterations = 1
for l in generator:
print(l, file=output)
output.close()
quit(1)