This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
128 lines (105 loc) · 4.36 KB
/
utility.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
"""Utility classes the app uses"""
import random
import string
import sys
from params import Command, PLATES_NUM
class UserInputValidator:
"""Provides utility functions validating user input for numeric menus"""
def __init__(self, user_options, dbc):
self.__user_options = user_options
self.__dbc = dbc
def __process_accounts(self):
"""Secure existing account selection"""
counter = 1
accounts = []
while True:
print("Account %d.:\t" % counter, end="")
user_input = sys.stdin.readline().strip()
if user_input not in string.digits:
print("Only numbers accepted, try again...")
elif user_input == "":
print("Accounts saved...")
self.__user_options.update({"accounts": accounts})
return 0
else:
if not self.__dbc.account_exists(user_input):
print("That account does not exist, try again...")
else:
accounts.append(user_input)
counter += 1
def process(self, key, min_rng=0, max_rng=0):
"""Limit user input to digits within min and max range
and save user choices"""
if key == "accounts":
self.__process_accounts()
return 0
while True:
user_input = sys.stdin.readline().strip()
if user_input not in string.digits:
print("Only numbers accepted, try again...")
continue
if int(user_input) >= min_rng and\
int(user_input) <= max_rng:
self.__user_options.update({key : str(user_input)})
return
print("Let's try that again (numbers %d to %d):\t"\
% (int(min_rng), int(max_rng)), end="")
class Generator:
"""Generates rows to be written to file"""
def __init__(self, user_options):
self.__user_options = user_options
def __generate_command(self):
"""Generate TollCRM command"""
index = (random.choice(string.digits)for _ in range(2, 4))
return Command(index).name
def __generate_plates(self, limit):
"""Generate a random plate number with two letters and 3 numbers"""
plates = []
for i in range(1, limit):
#plate is a string starting with 2 random letters and ending in 3 random numbers
plate = ''.join(random.choice(string.ascii_lowercase)for _ in range(2))\
.join(random.choice(string.digits)for _ in range(3))
if plate in plates:
i -= 1
continue
plates.append(plate)
return plates
def __generate_group(self):
"""Generate a random group name"""
groups = ["", "Drinks", "Food", "Clothes", "Transportation", "Hardware", "Furniture"]
group = groups[random.randrange(0, len(groups)-1)]
return group
def __construct_row(self, command, accountnum, platenum, groupname):
"""Generate entry for writing to file"""
line = command + ","\
+ accountnum + ","\
+ platenum + ",,,,,,,"\
+ groupname\
+ "\n"
return line
def generate_output(self):
"""Generates list with 2500 rows"""
plates = self.__generate_plates(PLATES_NUM[self.__user_options["plates"]])
accounts = self.__user_options["accounts"]
output = []
for _ in range(1, 2500):
command = self.__generate_command() if self.__user_options["commands"]\
not in Command else Command(self.__user_options).name
accnts_last = len(accounts)-1
accountnum = accounts[random.randrange(0, accnts_last)]
plates_last = len(plates)-1
plate = plates[random.randrange(0, plates_last)]
#possible additions
#make = self.__generate_make()
#model = self.__generate_model()
#color = self.__generate_color()
groupname = self.__generate_group()
row = self.__construct_row(command, accountnum, plate, groupname)
output.append(row + "\n")
return output
class Writer:
"""Class that writes output to file"""
def write(self, output, path):
"""Write to actual file"""
with open(path + "/bulk_test.csv", "w+") as file_handle:
file_handle.write(output)