-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.py
149 lines (115 loc) · 4.97 KB
/
Report.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
import re # regex
import pandas as pd
import os
import json
import reportGenerator
groupTypes = ("Drużyna", "Szczep", "Gromada", "Krąg") #these are the names group types
GROUP_TYPE_KEY = 'groupType'
# @staticmethod
def getDisplayableFieldName(fieldName):
if fieldName == GROUP_TYPE_KEY: # TODO (medium/low): think of better solution for dynamic group typess
return "Jednostka"
return re.sub(r"\. ?\d+", "", fieldName) # deletes . optionally spaces and digits after this
def getNumberFromFieldVaule(fieldValue):
try:
tmp = re.search(r"(\(\d+\.?,?\d+\))", fieldValue).group(0)
tmp = tmp.replace(",", ".")
to_be_added = float(tmp)
print("From val {0} there is tmp {1} = {2}".format(fieldValue, tmp, to_be_added))
except TypeError:
try:
to_be_added = float(fieldValue)
except Exception:
pass
print("From val {0} there is tmp {1}".format(fieldValue, fieldValue))
except Exception:
pass
return -1
def _get_longest_duplicate_substring(input_string: str) -> str:
substing_array = []
for i in range(input_string.__len__()):
work_string = input_string[0:i]
sub_string_count = input_string.count(work_string)
if sub_string_count > 1 and input_string.replace(work_string, "").strip() == "":
substing_array.append(work_string)
if substing_array == []:
return input_string.strip()
result_string = ""
for x in substing_array:
if result_string.__len__() < x.__len__():
result_string = x
return result_string
def remove_duplicates(input_string: str) -> str:
if type(input_string) != str:
return input_string
if len(input_string) < 5 or input_string == "":
return ""
if input_string.count(input_string[0:4]) == 1:
return input_string
final_result = _get_longest_duplicate_substring(input_string)
previous_result = ""
while final_result != previous_result:
previous_result = final_result
final_result = _get_longest_duplicate_substring(final_result)
# sometimes there is multiple duplicates so we do this recursive
return final_result.strip()
class Report():
default_global_config_path = "./config/global_config.json"
logger = reportGenerator.logger # injects logger set in reportGenerator.py
logger.debug("Want to read: {0}".format(default_global_config_path))
if os.path.exists(default_global_config_path):
with open(default_global_config_path, encoding="UTF-8") as f:
logger.info("Have read global config from: {0}".format(default_global_config_path))
config_json = json.load(f)
if "report_fields_to_ommit" in config_json.keys():
fields_to_ommit = config_json["report_fields_to_ommit"]
logger.debug("Fields to ommit: {0}".format(fields_to_ommit))
else:
logger.debug("Default global config not found")
id = -1
def __init__(self, headers, in_data):
# self.id = id
# logger.debug("Creating report with headers=\'{0}\' \nin_data=\'{1}\'".format(headers, in_data))
self.data = {}
i = 0
points_accumulated = 0
for name in headers:
if pd.isnull(in_data[i]) or pd.isna(in_data[i]):
val = ""
else :
to_be_added = 0
val = in_data[i]
# TODO ( high ) w niektórych opisach znajduje się rok i jest on przetwarzany jako liczba.
if name in groupTypes or name.startswith("Drużyna") or name.startswith("Szczep") or name.startswith("Gromada") or name.startswith("Jednostka") or name.startswith("Numer Gromady"):
# print("is >>{0}<< in {1} and val is >>{2}<<".format(name, groupTypes, val))
self.data[GROUP_TYPE_KEY] = val
else:
# print("not {0} in {1}".format(name, groupTypes))
field_name = remove_duplicates(name)
self.data[field_name] = remove_duplicates(val)
i += 1
# self.data['Punktów ogółem'] = points_accumulated
# print(self.shortDesc())
# self.data = in_data
def shortDesc(self):
return "Raport dla {0}".format(self.groupType)
def toString(self):
result = ""
for x in self.data:
result += "{0}: {1}\n".format(x, self.data[x])
return result
def __getattr__(self, attr):
return self.data[attr]
def getFields(self):
return list(self.data.keys())
def getDisplayableFieldsList(self):
displayableFields = list()
for field in list(self.data.keys()):
if not field in self.fields_to_ommit:
displayableFields.append( field )
if "" in displayableFields:
displayableFields.remove("")
# Ensure that groupType is the first key
displayableFields.remove(GROUP_TYPE_KEY)
displayableFields.insert(0, GROUP_TYPE_KEY)
return displayableFields