do some minor refactoring on main and enterer; some major refactoring is in the works!

This commit is contained in:
stupidcomputer 2024-06-24 04:57:36 -05:00
parent cf68cf4223
commit e3870aee62
3 changed files with 109 additions and 74 deletions

40
common.py Normal file
View File

@ -0,0 +1,40 @@
attrtable = {
# attr, sel[i]
"a": 0,
"c": 0,
"m": 0,
"f": 0,
"u/o": 0,
"h": 0,
"nh": 0,
"ai/an": 1,
"as": 1,
"b/aa": 1,
"nh/opi": 3,
"wh": 0,
"1+": 0,
"unk": 2,
}
age = ["a", "c"]
gender = ["m", "f", "u/o"]
ethnicity = ["h", "nh"]
race = ["ai/an", "as", "b/aa", "nh/opi", "wh", "1+", "unk"]
mapper_data = {
"age": "Age",
"gender": "Gender",
"ethnicity": "Ethnicity",
"race": "Race",
"a": "Adult",
"c": "Child",
"wh": "White",
"as": "Asian",
"f": "Female",
"m": "Male",
"1+": "More than one race",
"nh": "Not Hispanic",
"h": "Hispanic",
"b/aa": "Black/African American",
}

View File

@ -2,32 +2,10 @@ import curses
import logging import logging
import json import json
import sys import sys
import common
# logging.basicConfig(filename="enterer.log", level=logging.DEBUG) # logging.basicConfig(filename="enterer.log", level=logging.DEBUG)
attrtable = {
# attr, sel[i]
"a": 0,
"c": 0,
"m": 0,
"f": 0,
"u/o": 0,
"h": 0,
"nh": 0,
"ai/an": 1,
"as": 1,
"b/aa": 1,
"nh/opi": 3,
"wh": 0,
"1+": 0,
"unk": 2,
}
age = ["a", "c"]
gender = ["m", "f", "u/o"]
ethnicity = ["h", "nh"]
race = ["ai/an", "as", "b/aa", "nh/opi", "wh", "1+", "unk"]
def serialize_finalsels_to_json(finalsels, filename): def serialize_finalsels_to_json(finalsels, filename):
json_wrapper = {"payload": []} json_wrapper = {"payload": []}
json_objs = [] json_objs = []
@ -35,16 +13,16 @@ def serialize_finalsels_to_json(finalsels, filename):
for i in finalsels: for i in finalsels:
new_json_obj = {"age": None, "gender": None, "ethnicity": None, "race": None} new_json_obj = {"age": None, "gender": None, "ethnicity": None, "race": None}
for j in i[1]: for j in i[1]:
if j in age: if j in common.age:
new_json_obj["age"] = j new_json_obj["age"] = j
if j in gender: if j in common.gender:
new_json_obj["gender"] = j new_json_obj["gender"] = j
if j in ethnicity: if j in common.ethnicity:
new_json_obj["ethnicity"] = j new_json_obj["ethnicity"] = j
if j in race: if j in common.race:
new_json_obj["race"] = j new_json_obj["race"] = j
for _ in range(i[0]): for _ in range(i[0]):
@ -68,7 +46,7 @@ def pad_out_rest_of_line(window):
def check_if_attr_selected(attr, sel): def check_if_attr_selected(attr, sel):
# logging.debug(attrtable[attr]) # logging.debug(attrtable[attr])
# logging.debug((attrtable[attr], attr[attrtable[attr]], sel)) # logging.debug((attrtable[attr], attr[attrtable[attr]], sel))
if attr[attrtable[attr]] == sel: if attr[common.attrtable[attr]] == sel:
return True return True
return False return False
@ -77,7 +55,7 @@ def equality_sel_check(attr, sel):
def generate_outstring_format_values(sels): def generate_outstring_format_values(sels):
output = [] output = []
for i in attrtable.keys(): # will always be sorted, in order for i in common.attrtable.keys(): # will always be sorted, in order
for j in sels: for j in sels:
if check_if_attr_selected(i, j): if check_if_attr_selected(i, j):
output.append("[{}]".format(i)) output.append("[{}]".format(i))
@ -89,7 +67,7 @@ def generate_outstring_format_values(sels):
def convert_sels_arr_to_attrs(sels): def convert_sels_arr_to_attrs(sels):
output = [] output = []
for i in attrtable.keys(): # will always be sorted, in order for i in common.attrtable.keys(): # will always be sorted, in order
for j in sels: for j in sels:
# logging.debug(str((i, j))) # logging.debug(str((i, j)))
if check_if_attr_selected(i, j): if check_if_attr_selected(i, j):
@ -103,13 +81,13 @@ def get_attr_cata_counts(sels):
ethnicities = 0 ethnicities = 0
races = 0 races = 0
for i in sels: for i in sels:
if i in age: if i in common.age:
ages += 1 ages += 1
if i in gender: if i in common.gender:
genders += 1 genders += 1
if i in ethnicity: if i in common.ethnicity:
ethnicities += 1 ethnicities += 1
if i in race: if i in common.race:
races += 1 races += 1
return (ages, genders, ethnicities, races) return (ages, genders, ethnicities, races)
@ -123,27 +101,27 @@ def resolve_sel_conflict(sels):
if ages > 1: if ages > 1:
for i in final: for i in final:
if i in age: if i in common.age:
final.remove(i) final.remove(i)
if genders > 1: if genders > 1:
for i in final: for i in final:
if i in gender: if i in common.gender:
final.remove(i) final.remove(i)
if ethnicities > 1: if ethnicities > 1:
for i in final: for i in final:
if i in ethnicity: if i in common.ethnicity:
final.remove(i) final.remove(i)
if races > 1: if races > 1:
for i in final: for i in final:
if i in race: if i in common.race:
final.remove(i) final.remove(i)
finalfinal = [] finalfinal = []
for i in final: for i in final:
finalfinal.append(i[attrtable[i]]) finalfinal.append(i[common.attrtable[i]])
return finalfinal return finalfinal

87
main.py
View File

@ -1,36 +1,26 @@
import json import json
import common
from openpyxl import Workbook from openpyxl import Workbook
from openpyxl.utils import get_column_letter from openpyxl.utils import get_column_letter
from openpyxl.styles import Font from openpyxl.styles import Font
mapper_data = {
"age": "Age",
"gender": "Gender",
"ethnicity": "Ethnicity",
"race": "Race",
"a": "Adult",
"c": "Child",
"wh": "White",
"as": "Asian",
"f": "Female",
"m": "Male",
"1+": "More than one race",
"nh": "Not Hispanic",
"h": "Hispanic",
"b/aa": "Black/African American",
}
def mapper(string): def mapper(string):
# map to human-readable values # map to human-readable values
try: try:
return mapper_data[string] return common.mapper_data[string]
except KeyError: except KeyError:
return "WARNING: {}".format(string) return "WARNING: {}".format(string)
def aggregate_demographic_totals(payload): def aggregate_demographic_totals(payload, adultonly, childonly):
results = {"age": {}, "gender": {}, "ethnicity": {}, "race": {}} results = {"age": {}, "gender": {}, "ethnicity": {}, "race": {}}
for i in payload: for i in payload:
if adultonly and i["age"] == "c":
continue
if childonly and i["age"] == "a":
continue
# for every entry we get # for every entry we get
for key in results.keys(): for key in results.keys():
# for every field we have # for every field we have
@ -39,7 +29,7 @@ def aggregate_demographic_totals(payload):
return results return results
def get_totals_for_specific_attribute_on_specific_site(site): def get_totals_for_specific_attribute_on_specific_site(site, adultonly=False, childonly=False):
manifest = open("data/manifest.json") manifest = open("data/manifest.json")
manifest_data = json.loads(manifest.read()) manifest_data = json.loads(manifest.read())
@ -60,7 +50,7 @@ def get_totals_for_specific_attribute_on_specific_site(site):
payload = demographic_data_json["payload"] payload = demographic_data_json["payload"]
return aggregate_demographic_totals(payload) return aggregate_demographic_totals(payload, adultonly, childonly)
def generate_table_for_attr(attr, totals): def generate_table_for_attr(attr, totals):
dataarray = [[mapper(attr), "Count"]] dataarray = [[mapper(attr), "Count"]]
@ -87,39 +77,66 @@ def generate_provider_string(providers):
return before_and + and_string return before_and + and_string
return providers[0] return providers[0]
def handle_writing_of_attrs(worksheet, totals, providers): def handle_writing_of_attrs(worksheet, totals, offset):
# each set of fields is two long
worksheet["A1"].value = "Information for {} site attendance.".format(worksheet.title)
worksheet["A2"].value = "Fields not present should be assumed 0."
worksheet["A10"].value = "Data submitted by {}.".format(generate_provider_string(providers))
worksheet.merge_cells("A1:E1")
worksheet.merge_cells("A2:E2")
worksheet.merge_cells("A10:E10")
count = 0 count = 0
maxlen = 0
for i in totals.keys(): for i in totals.keys():
tmp = generate_table_for_attr(i, totals) tmp = generate_table_for_attr(i, totals)
maxlen = max(maxlen, len(tmp))
write_dataarray_to_specific_cell(3, count * 3, tmp, worksheet) write_dataarray_to_specific_cell(offset, count * 3, tmp, worksheet)
count += 1 count += 1
return maxlen
def adjust_column_width(worksheet, times): def adjust_column_width(worksheet, times):
for i in range(times): for i in range(times):
worksheet.column_dimensions[get_column_letter((i * 3) + 1)].width = 20 worksheet.column_dimensions[get_column_letter((i * 3) + 1)].width = 20
def write_information_to_spreadsheet(totals, worksheet, providers): def write_information_to_spreadsheet(totals, worksheet, offset):
handle_writing_of_attrs(ws, totals, providers) return handle_writing_of_attrs(ws, totals, offset)
adjust_column_width(ws, 4)
def handle_spreadsheet_decoration(ws, providers, persons, commleads, onsiteleads):
ws["A1"].value = "Information for {} site attendance.".format(ws.title)
ws["A2"].value = "Fields not present should be assumed 0."
ws["A3"].value = "Data submitted by {} in person after conclusion of site operations.".format(generate_provider_string(providers))
ws["A4"].value = "Interns present: {}".format(generate_provider_string(persons))
ws["A5"].value = "Communication Lead(s): {}".format(generate_provider_string(commleads))
ws["A6"].value = "On Site Lead(s): {}".format(generate_provider_string(onsiteleads))
ws.merge_cells("A1:E1")
ws.merge_cells("A2:E2")
ws.merge_cells("A3:H3")
ws.merge_cells("A4:H4")
ws.merge_cells("A5:H5")
ws.merge_cells("A6:H6")
fd = open("data/manifest.json", "r") fd = open("data/manifest.json", "r")
json_data = json.loads(fd.read()) json_data = json.loads(fd.read())
wb = Workbook() wb = Workbook()
for site in json_data["sites"]: for site in json_data["sites"]:
ws = wb.create_sheet(site["name"])
providers = [] providers = []
for record in site["records"]: for record in site["records"]:
providers.append(record['submitter']) providers.append(record['submitter'])
write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(ws.title), ws, providers) persons = site["present"]
commleads = site["commleads"]
onsiteleads = site["onsiteleads"]
length = 8
ws = wb.create_sheet(site["name"])
ws.cell(row=length, column=1, value="Combined Totals")
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"]), ws, length) + 2
ws.cell(row=length, column=1, value="Adults")
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"], adultonly=True), ws, length) + 3
ws.cell(row=length, column=1, value="Children")
length += write_information_to_spreadsheet(get_totals_for_specific_attribute_on_specific_site(site["name"], childonly=True), ws, length) + 3
adjust_column_width(ws, 4)
handle_spreadsheet_decoration(ws, providers, persons, commleads, onsiteleads)
del wb["Sheet"] del wb["Sheet"]
wb.save("test.xlsx") wb.save("test.xlsx")