This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
198 lines (184 loc) · 5.84 KB
/
forms.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
"""
This file will take and compare the data in coredb.txt
"""
import encrypt
import math
from enum import Enum
class InternalMethods:
"""
all the functions needed for internal processing of forms
"""
@staticmethod
def bubble_sort(arr):
"""
bubble sort
:param arr: list
:return arr: list
"""
for i in range(0, len(arr)):
for j in range(0, len(arr)-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
@staticmethod
def quicksort(arr):
"""
quicksort algorithm
:param arr:
:return:
"""
if len(arr) == 1 or len(arr) == 0:
return arr
else:
pivot = arr[0]
i = 0
for j in range(len(arr) - 1):
if arr[j + 1] < pivot:
arr[j + 1], arr[i + 1] = arr[i + 1], arr[j + 1]
i += 1
arr[0], arr[i] = arr[i], arr[0]
first_part = quicksort(arr[:i])
second_part = quicksort(arr[i + 1:])
first_part.append(arr[i])
return first_part + second_part
@staticmethod
def split(input_str):
"""
this function is haunted
this function has been exorcised, carry on citizen
splits data from DB into strings for processing
:return:
"""
final_array = []
mid_str = ''
for i in range(0, len(input_str)):
# takes encrypted or unencrypted strings
if input_str[i] == "." or input_str[i] == ')':
final_array.append(mid_str)
mid_str = ''
else:
mid_str += input_str[i]
return final_array
@staticmethod
def data_decode():
"""
reads and decrypts data from DB
:return:
"""
output_data = []
with open("data" + "/coredb.txt") as f:
data = f.read().splitlines()
# print(data)
for i in range(0, len(data)):
output_data.append(InternalMethods.split(encrypt.shift_decode(data[i].lower(), 5)))
# print(output_data)
return output_data
@staticmethod
def maths(values, mean):
"""
calculates median, largest value , smallest value and standard deviation
:param values:
:param mean:
:return:
"""
std_values = []
# median calculation
med = (len(values) - 1) / 2
if med.is_integer() is False:
median = (values[int(med + 0.5)] + values[int(med - 0.5)]) / 2
else:
median = values[int(med)]
# highest value calculation
max_val = values[len(values) - 1]
# lowest value calculation
min_val = values[0]
# standard deviation calculation
for i in range(0, len(values)):
std_values.append(values[i] ** 2)
half_way = (sum(std_values)) / (len(values))
std_dev = math.sqrt((half_way - (mean ** 2)))
# print(mean, median, max_val, min_val, std_dev)
return median, max_val, min_val, std_dev
class Stats(Enum):
"""
Creates instance of Stats and compares it
"""
def __init__(self):
flag = 0
option = 0
if option == 'pony':
flag = 2
elif option == 'venue':
flag = 1
elif option == 'conditions':
flag = 4
pony = 2
name = 0
venue = 1
conditions = 4
def single_variable_stats(self, flag):
"""
gets statistics for single variable that is used in data_menu.html
:param: str
:return:
"""
error = False
values = []
sum_val, count = 0, 0
input_array = InternalMethods.data_decode()
# mean calculation
for i in range(0, len(input_array)):
if input_array[i][flag] == self:
count += 1
values.append(int(input_array[i][3]))
sum_val += int(input_array[i][3])
# checks if no values are returned
try:
mean = sum_val/count
except ZeroDivisionError:
error = True
mean, median, max_val, min_val, std_dev = 0, 0, 0, 0, 0
return mean, median, max_val, min_val, std_dev, error
# sort
InternalMethods.bubble_sort(values)
# runs maths calc
median, max_val, min_val, std_dev = InternalMethods.maths(values, mean)
# print(mean, median, max_val, min_val, std_dev, error)
return mean, median, max_val, min_val, std_dev, error
def double_variable_stats(player, variable, flag):
"""
used in comparison in data_menu.html
:param player:
:param variable:
:param flag:
:return:
"""
count, sum_val = 0, 0
error = False
values, scores = [], []
input_array = InternalMethods.data_decode()
# print(input_array)
# selects positions for scores where the player and variable are True
for i in range(0, len(input_array)):
if input_array[i][0] == player:
values.append(i)
# mean calculation
for i in range(0, len(input_array)):
if i in values and input_array[i][flag] == variable:
count += 1
scores.append(int(input_array[i][3]))
sum_val += int(input_array[i][3])
# print(scores)
# error handling
try:
mean = sum_val/count
except ZeroDivisionError:
error = True
mean, median, max_val, min_val, std_dev = 0, 0, 0, 0, 0
return mean, median, max_val, min_val, std_dev, error
# sort
InternalMethods.bubble_sort(scores)
# maths
median, max_val, min_val, std_dev = InternalMethods.maths(scores, mean)
# print(mean, median, max_val, min_val, std_dev, error)
return mean, median, max_val, min_val, std_dev, error