-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelperFunctions.py
More file actions
111 lines (76 loc) · 3.4 KB
/
HelperFunctions.py
File metadata and controls
111 lines (76 loc) · 3.4 KB
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
from tabulate import tabulate
class HelperFunctions:
#converts numbers to an int
@staticmethod
def ConvertToInt(num):
return int(str(num).replace(',', ''))
#Gets the users input for a given question, has to be between the min and max values (inclusive)
@staticmethod
def GetQuestionInput(question, min, max):
option = -1
try:
option = int(input(question))
except ValueError:
print("Not a valid input was given")
while not (min <= option <= max):
try:
option = int(input(question + " Enter a value between " + str(min) + " and " + str(max) + ": "))
except:
print("Not a valid input was given")
return option
#filters the whole vote data array for objects that match the given expression
#Format of expression MUST match: x.CountryName == 'Scotland'
@staticmethod
def FilterVoteData(data, expression):
try:
filter = [x for x in data if eval(expression)]
return filter
except:
print("Expression invalid, returning whole data set")
return data
#filters the whole vote data array for items that contain the given element
#Format of expression MUST match: "name", "x.Member.firstName"
@staticmethod
def SearchVoteData(data, phrase, obj):
try:
filter = [x for x in data if phrase in eval(obj)]
return filter
except:
print("Expression invalid, returning whole data set")
return data
# displays all of the data in an easy to read table
@staticmethod
def DisplayDataDetails(data):
tableData = []
for self in data:
tableData.append( [ self.ConstituencyName,
self.RegionName,
self.CountryName,
self.ConstituencyType,
self.Member.firstName,
self.Member.lastName,
self.Member.gender,
self.Result,
self.FirstParty,
self.SecondParty,
self.Electorate,
self.ValidVotes,
self.InvalidVotes,
self.Majority
] )
print("\n")
print(tabulate(tableData, headers=[ 'Constituency Name',
'Region name',
'Country Name',
'Constituency Type',
'First Name',
'Last Name',
'Gender',
'Result',
'First Party',
'Second Party',
'Electorate',
'Valid Votes',
'Invalid Votes',
'Majority'
], tablefmt='orgtbl'))