1- from election_anomaly import db_routines as dbr
1+ from election_anomaly import database as db
22from election_anomaly import user_interface as ui
33from election_anomaly import munge_routines as mr
44from sqlalchemy .orm import sessionmaker
@@ -60,13 +60,13 @@ def __init__(self):
6060 self .tracker = dict ()
6161
6262 # create db if it does not already exist
63- error = dbr .establish_connection (paramfile = self .d ['db_paramfile' ], db_name = self .d ['db_name' ])
63+ error = db .establish_connection (paramfile = self .d ['db_paramfile' ], db_name = self .d ['db_name' ])
6464 if error :
65- dbr .create_new_db (self .d ['project_root' ], self .d ['db_paramfile' ], self .d ['db_name' ])
65+ db .create_new_db (self .d ['project_root' ], self .d ['db_paramfile' ], self .d ['db_name' ])
6666
6767 # connect to db
6868 try :
69- self .engine = dbr .sql_alchemy_connect (paramfile = self .d ['db_paramfile' ], db_name = self .d ['db_name' ])
69+ self .engine = db .sql_alchemy_connect (paramfile = self .d ['db_paramfile' ], db_name = self .d ['db_name' ])
7070 Session = sessionmaker (bind = self .engine )
7171 self .session = Session ()
7272 except Exception as e :
@@ -228,8 +228,8 @@ def check_errors(self):
228228
229229 def track_results (self ):
230230 filename = self .d ['results_file' ]
231- top_reporting_unit_id = dbr .name_to_id (self .session ,'ReportingUnit' , self .d ['top_reporting_unit' ])
232- election_id = dbr .name_to_id (self .session ,'Election' ,self .d ['election' ])
231+ top_reporting_unit_id = db .name_to_id (self .session ,'ReportingUnit' , self .d ['top_reporting_unit' ])
232+ election_id = db .name_to_id (self .session ,'Election' ,self .d ['election' ])
233233
234234 data = pd .DataFrame (
235235 [[self .d ['results_short_name' ],filename ,
@@ -238,12 +238,12 @@ def track_results(self):
238238 columns = ['short_name' , 'file_name' ,
239239 'download_date' , 'source' ,
240240 'note' , 'ReportingUnit_Id' , 'Election_Id' ,'created_at' ])
241- e = dbr .insert_to_cdf_db (self .session .bind , data , '_datafile' )
241+ e = db .insert_to_cdf_db (self .session .bind , data , '_datafile' )
242242 if e :
243243 return [0 , 0 ], e
244244 else :
245245 col_map = {'short_name' :'short_name' }
246- datafile_id = dbr .append_id_to_dframe (
246+ datafile_id = db .append_id_to_dframe (
247247 self .session .bind ,data ,'_datafile' ,col_map = col_map
248248 ).iloc [0 ]['_datafile_Id' ]
249249 return [datafile_id , election_id ], e
@@ -540,7 +540,7 @@ def add_elements_from_results_file(self, elements: iter, error: dict, results_fi
540540 wr , mu , error = ui .read_results (kwargs ,error )
541541
542542 for element in elements :
543- name_field = dbr .get_name_field (element )
543+ name_field = db .get_name_field (element )
544544 # append <element>_raw
545545 wr , error = mr .add_munged_column (
546546 wr , mu , element , error , mode = mu .cdf_elements .loc [element , 'source' ],
@@ -583,7 +583,7 @@ def starter_dictionary(self,include_existing=True) -> str:
583583 old .drop ()
584584 for element in elements :
585585 w [element ] = prep .get_element (self .d ['jurisdiction_path' ],element )
586- name_field = dbr .get_name_field (element )
586+ name_field = db .get_name_field (element )
587587 w [element ] = mr .add_constant_column (w [element ],'cdf_element' ,element )
588588 w [element ].rename (columns = {name_field :'cdf_internal_name' },inplace = True )
589589 w [element ]['raw_identifier_value' ] = w [element ]['cdf_internal_name' ]
@@ -650,21 +650,21 @@ def __init__(self):
650650 'db_name' , 'results_file' ])
651651 self .d ['results_file_short' ] = get_filename (self .d ['results_file' ])
652652
653- eng = dbr .sql_alchemy_connect (paramfile = self .d ['db_paramfile' ],
653+ eng = db .sql_alchemy_connect (paramfile = self .d ['db_paramfile' ],
654654 db_name = self .d ['db_name' ])
655655 Session = sessionmaker (bind = eng )
656656 self .session = Session ()
657657
658658
659659 def display_options (self , input , verbose = False , filters = None ):
660660 if not verbose :
661- results = dbr .get_input_options (self .session , input , False )
661+ results = db .get_input_options (self .session , input , False )
662662 else :
663663 if not filters :
664- df = pd .DataFrame (dbr .get_input_options (self .session , input , True ))
665- results = dbr .package_display_results (df )
664+ df = pd .DataFrame (db .get_input_options (self .session , input , True ))
665+ results = db .package_display_results (df )
666666 else :
667- results = dbr .get_filtered_input_options (self .session , input , filters )
667+ results = db .get_filtered_input_options (self .session , input , filters )
668668 if results :
669669 return results
670670 return None
@@ -678,9 +678,9 @@ def top_counts_by_vote_type(self, election, rollup_unit, sub_unit):
678678 else :
679679 connection = self .session .bind .raw_connection ()
680680 cursor = connection .cursor ()
681- rollup_unit_id = dbr .name_to_id (self .session , 'ReportingUnit' , rollup_unit )
682- sub_unit_id = dbr .name_to_id (self .session , 'ReportingUnitType' , sub_unit )
683- election_id = dbr .name_to_id (self .session , 'Election' , election )
681+ rollup_unit_id = db .name_to_id (self .session , 'ReportingUnit' , rollup_unit )
682+ sub_unit_id = db .name_to_id (self .session , 'ReportingUnitType' , sub_unit )
683+ election_id = db .name_to_id (self .session , 'Election' , election )
684684 err_str = a .create_rollup (cursor , d ['rollup_directory' ], rollup_unit_id ,
685685 sub_unit_id , election_id )
686686 connection .close ()
@@ -694,9 +694,9 @@ def top_counts(self, rollup_unit, sub_unit):
694694 print ("Data not created." )
695695 return
696696 else :
697- rollup_unit_id = dbr .name_to_id (self .session , 'ReportingUnit' , rollup_unit )
698- sub_unit_id = dbr .name_to_id (self .session , 'ReportingUnitType' , sub_unit )
699- results_info = dbr .get_datafile_info (self .session , self .d ['results_file_short' ])
697+ rollup_unit_id = db .name_to_id (self .session , 'ReportingUnit' , rollup_unit )
698+ sub_unit_id = db .name_to_id (self .session , 'ReportingUnitType' , sub_unit )
699+ results_info = db .get_datafile_info (self .session , self .d ['results_file_short' ])
700700 rollup = a .create_rollup (self .session , d ['rollup_directory' ], top_ru_id = rollup_unit_id ,
701701 sub_rutype_id = sub_unit_id , sub_rutype_othertext = '' , datafile_id_list = results_info [0 ],
702702 election_id = results_info [1 ], by_vote_type = False )
@@ -718,25 +718,25 @@ def scatter(self, jurisdiction, subdivision_type,
718718 print (error )
719719 print ("Data not created." )
720720 return
721- jurisdiction_id = dbr .name_to_id (self .session , 'ReportingUnit' , jurisdiction )
722- subdivision_type_id = dbr .name_to_id (self .session , 'ReportingUnitType' , subdivision_type )
723- h_election_id = dbr .name_to_id (self .session , 'Election' , h_election )
724- v_election_id = dbr .name_to_id (self .session , 'Election' , v_election )
721+ jurisdiction_id = db .name_to_id (self .session , 'ReportingUnit' , jurisdiction )
722+ subdivision_type_id = db .name_to_id (self .session , 'ReportingUnitType' , subdivision_type )
723+ h_election_id = db .name_to_id (self .session , 'Election' , h_election )
724+ v_election_id = db .name_to_id (self .session , 'Election' , v_election )
725725 # *_type is either candidates or contests
726726 h_count_item_type , h_type = self .split_category_input (h_category )
727727 v_count_item_type , v_type = self .split_category_input (v_category )
728728 if h_count == 'All Candidates' or h_count == 'All Contests' :
729729 h_count_id = - 1
730730 elif h_type == 'candidates' :
731- h_count_id = dbr .name_to_id (self .session , 'Candidate' , h_count )
731+ h_count_id = db .name_to_id (self .session , 'Candidate' , h_count )
732732 elif h_type == 'contests' :
733- h_count_id = dbr .name_to_id (self .session , 'CandidateContest' , h_count )
733+ h_count_id = db .name_to_id (self .session , 'CandidateContest' , h_count )
734734 if v_count == 'All Candidates' or v_count == 'All Contests' :
735735 v_count_id = - 1
736736 elif v_type == 'candidates' :
737- v_count_id = dbr .name_to_id (self .session , 'Candidate' , v_count )
737+ v_count_id = db .name_to_id (self .session , 'Candidate' , v_count )
738738 elif v_type == 'contests' :
739- v_count_id = dbr .name_to_id (self .session , 'CandidateContest' , v_count )
739+ v_count_id = db .name_to_id (self .session , 'CandidateContest' , v_count )
740740 h_count_item_type , h_type = self .split_category_input (h_category )
741741 v_count_item_type , v_type = self .split_category_input (v_category )
742742 agg_results = a .create_scatter (self .session , jurisdiction_id , subdivision_type_id ,
@@ -756,11 +756,11 @@ def bar(self, jurisdiction, contest_type=None, contest=None, fig_type=None):
756756 print (error )
757757 print ("Data not created." )
758758 return
759- jurisdiction_id = dbr .name_to_id (self .session , 'ReportingUnit' , jurisdiction )
760- most_granular_id = dbr .name_to_id (self .session , 'ReportingUnitType' ,
759+ jurisdiction_id = db .name_to_id (self .session , 'ReportingUnit' , jurisdiction )
760+ most_granular_id = db .name_to_id (self .session , 'ReportingUnitType' ,
761761 d ['sub_reporting_unit_type' ])
762- hierarchy = dbr .get_jurisdiction_hierarchy (self .session , jurisdiction_id , most_granular_id )
763- results_info = dbr .get_datafile_info (self .session , self .d ['results_file_short' ])
762+ hierarchy = db .get_jurisdiction_hierarchy (self .session , jurisdiction_id , most_granular_id )
763+ results_info = db .get_datafile_info (self .session , self .d ['results_file_short' ])
764764 # bar chart always at one level below top reporting unit
765765 agg_results = a .create_bar (self .session , jurisdiction_id , hierarchy [1 ], \
766766 contest_type , contest , results_info [1 ], False )
@@ -790,11 +790,11 @@ def export_outlier_data(self, jurisdiction, contest=None):
790790 print (error )
791791 print ("Data not created." )
792792 return
793- jurisdiction_id = dbr .name_to_id (self .session , 'ReportingUnit' , jurisdiction )
794- most_granular_id = dbr .name_to_id (self .session , 'ReportingUnitType' ,
793+ jurisdiction_id = db .name_to_id (self .session , 'ReportingUnit' , jurisdiction )
794+ most_granular_id = db .name_to_id (self .session , 'ReportingUnitType' ,
795795 d ['sub_reporting_unit_type' ])
796- hierarchy = dbr .get_jurisdiction_hierarchy (self .session , jurisdiction_id , most_granular_id )
797- results_info = dbr .get_datafile_info (self .session , self .d ['results_file_short' ])
796+ hierarchy = db .get_jurisdiction_hierarchy (self .session , jurisdiction_id , most_granular_id )
797+ results_info = db .get_datafile_info (self .session , self .d ['results_file_short' ])
798798 # bar chart always at one level below top reporting unit
799799 agg_results = a .create_bar (self .session , jurisdiction_id , hierarchy [1 ], \
800800 None , contest , results_info [1 ], True )
0 commit comments