-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
366 lines (298 loc) · 11.2 KB
/
main.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Generates dataset of random data into CSV file"""
from enum import Enum
import generators
# available column types for generation
class ColumnType(Enum):
NAME = 1
PHONE_NUMBER = 2
EMAIL = 3
RANDOM_NUMBER = 4
CITY = 5
COUNTRY = 6
PASSWORD = 7
generator_functions = [
generators.get_random_name,
generators.get_random_phone_number,
generators.get_random_email,
generators.get_random_number,
generators.get_random_city,
generators.get_random_country,
generators.get_random_password,
]
# print all column types
def list_column_types():
print("Available column types:")
for col_type in ColumnType:
print("\t{0}) {1}".format(col_type.value, col_type.name))
class Column:
def __init__(self, name, col_type):
self.name = name
self.col_type = col_type
# borders for RANDOM_NUMBER
# lo also used for specifying country for CITY type
self.lo = 0
self.hi = 0
def __str__(self):
return "{0}, ".format(self.name)
class Header:
columns = [] # list of Column objects
def __init__(self):
pass
def add_column(self, col: Column):
self.columns.append(col)
def remove_column(self, index: int):
del self.columns[index]
# print all values in columns list
def show_header(self):
print("+--------+--------------+--------+")
print("| id | type | name |")
print("+--------+--------------+--------+")
for i in range(len(self.columns)):
# printing with index in list
if self.columns[i].col_type == ColumnType.RANDOM_NUMBER.name:
print(
"[{0}]\t{1} ({3}-{4})\t{2}".format(
i,
self.columns[i].col_type,
self.columns[i].name,
self.columns[i].lo,
self.columns[i].hi,
)
)
elif self.columns[i].col_type == ColumnType.CITY.name:
print(
"[{0}]\t{1} ({3})\t{2}".format(
i,
self.columns[i].col_type,
self.columns[i].name,
self.columns[i].lo,
)
)
else:
print(
"[{0}]\t{1}\t{2}".format(
i, self.columns[i].col_type, self.columns[i].name
)
)
print()
def change_column_name(self, index: int, new_name: str):
self.columns[index].name = new_name
def change_column_type(self, index: int, new_type: ColumnType):
# if new column type is CITY
if new_type == ColumnType.CITY.name:
print("enter 'countries' to see list of all countries.")
lo = input("(specify country/leave empty if any)~> ").strip().title()
while lo.lower() == "countries":
print(generators.get_all_countries())
lo = input("(specify country/leave empty if any)~> ")
# if country not found in list of countries write warning and exit
if lo.strip() != "" and lo.strip() not in generators.get_all_countries():
print("Country not found.")
return
# if no input then any country
if lo == "":
lo = "any"
self.columns[index].col_type = new_type
self.columns[index].lo = lo
# if new column type is RANDOM_NUMBER
elif new_type == ColumnType.RANDOM_NUMBER.name:
try:
lo = int(input("(lowest number)~> "))
hi = int(input("(highest number)~> "))
if lo > hi:
print("Incorrect input. First number must be lower than second.")
else:
self.columns[index].col_type = new_type
self.columns[index].lo = lo
self.columns[index].hi = hi
except:
print("Incorrect number.")
class DataRow:
cells = [] # row of data
def __init__(self):
pass
def __str__(self):
str_row = ""
for cell in self.cells:
str_row += "{0}, ".format(cell)
str_row += "\n"
return str_row
def generate(self):
self.cells.clear()
for col in Header.columns:
self.cells.append(
generator_functions[ColumnType[col.col_type].value - 1](col.lo, col.hi)
)
running = True
header = Header()
def clear_table():
header.columns.clear()
# introduction, waiting for commands
print("====== Dataset Generator ======\n")
print('Type "help" for information.')
while running == True:
user_input = input(">>> ")
# list of available commands
if user_input.lower().strip() == "help":
print(
"List of commands:\n"
"\th - show created table\n"
"\tn - add new column\n"
"\tr - remove column\n"
"\tc - change column\n"
"\tg - generate dataset\n"
"\treset - clear table template\n"
"\texit - terminate app\n"
)
# show header
elif user_input.lower().strip() == "h":
header.show_header()
# add new column
elif user_input.lower().strip() == "n":
input_name = input("(column name)~> ").strip()
# column name can't be empty
if input_name.strip() == "":
print("Column name can't be empty.")
continue
# specifying type of column
try:
list_column_types()
input_type = input("(column type)(1-7)~> ")
input_type = ColumnType(int(input_type)).name
except:
print("Incorrect data type. Enter type id from list of available options.")
continue
created_column = Column(input_name, input_type)
# if type is CITY ask user to enter country
# if country not found generates city from random country
if input_type == ColumnType.CITY.name:
print("enter 'countries' to see list of all countries.")
created_column.lo = (
input("(specify country/leave empty if any)~> ").strip().title()
)
while created_column.lo.lower() == "countries":
print(generators.get_all_countries())
created_column.lo = input("(specify country/leave empty if any)~> ")
# if country not found in list of countries write warning and exit
if (
created_column.lo.strip() != ""
and created_column.lo.strip() not in generators.get_all_countries()
):
print("Country not found.")
continue
# if no input then any country
if created_column.lo == "":
created_column.lo = "any"
# if type is RANDOM_NUMBER ask user to enter borders
if input_type == ColumnType.RANDOM_NUMBER.name:
try:
created_column.lo = int(input("(lowest number)~> "))
created_column.hi = int(input("(highest number)~> "))
if created_column.lo > created_column.hi:
print("Incorrect input. First number must be lower than second.")
continue
except:
print("Incorrect number.")
continue
header.add_column(created_column)
# remove column
elif user_input.lower().strip() == "r":
# displaying all columns
header.show_header()
# getting id of column to delete
user_input = input("(column id to delete)~> ")
try:
header.remove_column(int(user_input))
except:
print("Column with id {0} not found.".format(user_input))
# change column
elif user_input.lower().strip() == "c":
# displaying all columns
header.show_header()
# getting id of column to change
try:
id_to_change = int(input("(column id to change)~> "))
except:
print("Invalid id.")
what_to_change = input(
"What'd you like to change?\n" "\t1) Name\n" "\t2) Type\n" "\t3) Both\n~> "
)
# changing only name
if what_to_change.strip() == "1":
new_name = input("(enter new name)~> ")
header.change_column_name(id_to_change, new_name)
print("Column name changed.")
# changing only type
elif what_to_change.strip() == "2":
list_column_types()
new_type = input("(column type)(1-7)~> ")
try:
new_type = ColumnType(int(new_type)).name
header.change_column_type(id_to_change, new_type)
except:
print(
"Incorrect data type. Enter type id from list of available options."
)
continue
# changing name and type
elif what_to_change.strip() == "3":
# changing name
new_name = input("(enter new name)~> ")
header.change_column_name(id_to_change, new_name)
# changing type
list_column_types()
new_type = input("(column type)(1-7)~> ")
try:
new_type = ColumnType(int(new_type)).name
header.change_column_type(id_to_change, new_type)
except:
print(
"Incorrect data type. Enter type id from list of available options."
)
continue
else:
print("Unknown option.")
# generate dataset
elif user_input.lower().strip() == "g":
file_name = input("(enter name of CSV file)~> ")
try:
amount_of_rows = int(input("(how many rows to generate?)~> "))
except:
print("Invalid value.")
print("Generating data...")
# writing header to file
f = open("{0}.csv".format(file_name), "a")
for column in header.columns:
f.write("{0}, ".format(column.name))
f.write("\n")
f.close()
# generating rows of data, writing data to csv file
f = open("{0}.csv".format(file_name), "a")
for row_index in range(amount_of_rows):
data_row = DataRow()
data_row.generate()
f.write(str(data_row))
f.close()
print("Generation complete.")
answer = input("Do you want to continue working? (y/n): ").strip()
if answer.lower() == "y":
answer = input("Clean last table template? (y/n): ")
if answer.lower() == "y":
clear_table()
continue
else:
continue
elif answer.lower() == "n":
running = False
# clearing table template
elif user_input.lower().strip() == "reset":
clear_table()
print("Template cleared.")
# terminating on exit command
elif user_input.lower().strip() == "exit":
running = False
elif user_input.strip() == "":
pass
else:
print("Unknown command. Type 'help' for information.")
print("Program complete.")