@@ -515,7 +515,7 @@ def append_id_to_dframe(
515515def get_column_names (cursor , table : str ) -> (list , dict ):
516516 q = sql .SQL (
517517 """SELECT column_name, data_type FROM information_schema.columns
518- WHERE table_schema = 'public' AND table_name = %s"""
518+ WHERE table_schema = 'public' AND table_name = %s"""
519519 )
520520 cursor .execute (q , [table ])
521521 results = cursor .fetchall ()
@@ -558,101 +558,101 @@ def export_rollup_to_csv(
558558 if contest_type == "Candidate" :
559559 q = sql .SQL (
560560 """ COPY
561- (SELECT
562- 'Candidate' contest_type,
563- C."Name" "Contest",
564- EDRUT."Txt" contest_district_type,
565- Cand."BallotName" "Selection",
566- IntermediateRU."Name" "ReportingUnit",
567- CIT."Txt" "CountItemType",
568- sum(vc."Count") "Count"
569- FROM "VoteCount" vc
570- LEFT JOIN _datafile d on vc."_datafile_Id" = d."Id"
571- LEFT JOIN "Contest" C on vc."Contest_Id" = C."Id"
572- LEFT JOIN "CandidateSelection" CS on CS."Id" = vc."Selection_Id"
573- LEFT JOIN "Candidate" Cand on CS."Candidate_Id" = Cand."Id"
574- -- sum over all children
575- LEFT JOIN "ReportingUnit" ChildRU on vc."ReportingUnit_Id" = ChildRU."Id"
576- LEFT JOIN "ComposingReportingUnitJoin" CRUJ_sum on ChildRU."Id" = CRUJ_sum."ChildReportingUnit_Id"
577- -- roll up to the intermediate RUs
578- LEFT JOIN "ReportingUnit" IntermediateRU on CRUJ_sum."ParentReportingUnit_Id" =IntermediateRU."Id"
579- LEFT JOIN "ReportingUnitType" IntermediateRUT on IntermediateRU."ReportingUnitType_Id" = IntermediateRUT."Id"
580- -- intermediate RUs must nest in top RU
581- LEFT JOIN "ComposingReportingUnitJoin" CRUJ_top on IntermediateRU."Id" = CRUJ_top."ChildReportingUnit_Id"
582- LEFT JOIN "ReportingUnit" TopRU on CRUJ_top."ParentReportingUnit_Id" = TopRU."Id"
583- LEFT JOIN "CountItemType" CIT on vc."CountItemType_Id" = CIT."Id"
584- LEFT JOIN "CandidateContest" on C."Id" = "CandidateContest"."Id"
585- LEFT JOIN "Office" O on "CandidateContest"."Office_Id" = O."Id"
586- LEFT JOIN "ReportingUnit" ED on O."ElectionDistrict_Id" = ED."Id"
587- LEFT JOIN "ReportingUnitType" EDRUT on ED."ReportingUnitType_Id" = EDRUT."Id"
588- WHERE C.contest_type = 'Candidate'
589- AND TopRU."Name" = %s -- top RU
590- AND IntermediateRUT."Txt" = %s -- intermediate_reporting_unit_type
591- AND d.{by} in %s -- tuple of datafile short_names
592- {restrict}
593- GROUP BY
594- C."Name",
595- EDRUT."Txt",
596- Cand."BallotName",
597- IntermediateRU."Name",
598- CIT."Txt"
599- ORDER BY
600- C."Name",
601- EDRUT."Txt",
602- Cand."BallotName",
603- IntermediateRU."Name",
604- CIT."Txt")
605- TO %s DELIMITER %s CSV HEADER;
606- """
561+ (SELECT
562+ 'Candidate' contest_type,
563+ C."Name" "Contest",
564+ EDRUT."Txt" contest_district_type,
565+ Cand."BallotName" "Selection",
566+ IntermediateRU."Name" "ReportingUnit",
567+ CIT."Txt" "CountItemType",
568+ sum(vc."Count") "Count"
569+ FROM "VoteCount" vc
570+ LEFT JOIN _datafile d on vc."_datafile_Id" = d."Id"
571+ LEFT JOIN "Contest" C on vc."Contest_Id" = C."Id"
572+ LEFT JOIN "CandidateSelection" CS on CS."Id" = vc."Selection_Id"
573+ LEFT JOIN "Candidate" Cand on CS."Candidate_Id" = Cand."Id"
574+ -- sum over all children
575+ LEFT JOIN "ReportingUnit" ChildRU on vc."ReportingUnit_Id" = ChildRU."Id"
576+ LEFT JOIN "ComposingReportingUnitJoin" CRUJ_sum on ChildRU."Id" = CRUJ_sum."ChildReportingUnit_Id"
577+ -- roll up to the intermediate RUs
578+ LEFT JOIN "ReportingUnit" IntermediateRU on CRUJ_sum."ParentReportingUnit_Id" =IntermediateRU."Id"
579+ LEFT JOIN "ReportingUnitType" IntermediateRUT on IntermediateRU."ReportingUnitType_Id" = IntermediateRUT."Id"
580+ -- intermediate RUs must nest in top RU
581+ LEFT JOIN "ComposingReportingUnitJoin" CRUJ_top on IntermediateRU."Id" = CRUJ_top."ChildReportingUnit_Id"
582+ LEFT JOIN "ReportingUnit" TopRU on CRUJ_top."ParentReportingUnit_Id" = TopRU."Id"
583+ LEFT JOIN "CountItemType" CIT on vc."CountItemType_Id" = CIT."Id"
584+ LEFT JOIN "CandidateContest" on C."Id" = "CandidateContest"."Id"
585+ LEFT JOIN "Office" O on "CandidateContest"."Office_Id" = O."Id"
586+ LEFT JOIN "ReportingUnit" ED on O."ElectionDistrict_Id" = ED."Id"
587+ LEFT JOIN "ReportingUnitType" EDRUT on ED."ReportingUnitType_Id" = EDRUT."Id"
588+ WHERE C.contest_type = 'Candidate'
589+ AND TopRU."Name" = %s -- top RU
590+ AND IntermediateRUT."Txt" = %s -- intermediate_reporting_unit_type
591+ AND d.{by} in %s -- tuple of datafile short_names
592+ {restrict}
593+ GROUP BY
594+ C."Name",
595+ EDRUT."Txt",
596+ Cand."BallotName",
597+ IntermediateRU."Name",
598+ CIT."Txt"
599+ ORDER BY
600+ C."Name",
601+ EDRUT."Txt",
602+ Cand."BallotName",
603+ IntermediateRU."Name",
604+ CIT."Txt")
605+ TO %s DELIMITER %s CSV HEADER;
606+ """
607607 ).format (by = sql .Identifier (by ), restrict = sql .SQL (restrict ))
608608
609609 elif contest_type == "BallotMeasure" :
610610 q = sql .SQL (
611611 """ COPY
612- (SELECT
613- 'Candidate' contest_type,
614- C."Name" "Contest",
615- EDRUT."Txt" contest_district_type,
616- BMS."Name" "Selection",
617- IntermediateRU."Name" "ReportingUnit",
618- CIT."Txt" "CountItemType",
619- sum(vc."Count") "Count"
620- FROM "VoteCount" vc
621- LEFT JOIN _datafile d on vc."_datafile_Id" = d."Id"
622- LEFT JOIN "Contest" C on vc."Contest_Id" = C."Id"
623- LEFT JOIN "BallotMeasureContest" BMC on vc."Contest_Id" = BMC."Id"
624- LEFT JOIN "BallotMeasureSelection" BMS on BMS."Id" = vc."Selection_Id"
625- -- sum over all children
626- LEFT JOIN "ReportingUnit" ChildRU on vc."ReportingUnit_Id" = ChildRU."Id"
627- LEFT JOIN "ComposingReportingUnitJoin" CRUJ_sum on ChildRU."Id" = CRUJ_sum."ChildReportingUnit_Id"
628- -- roll up to the intermediate RUs
629- LEFT JOIN "ReportingUnit" IntermediateRU on CRUJ_sum."ParentReportingUnit_Id" =IntermediateRU."Id"
630- LEFT JOIN "ReportingUnitType" IntermediateRUT on IntermediateRU."ReportingUnitType_Id" = IntermediateRUT."Id"
631- -- intermediate RUs must nest in top RU
632- LEFT JOIN "ComposingReportingUnitJoin" CRUJ_top on IntermediateRU."Id" = CRUJ_top."ChildReportingUnit_Id"
633- LEFT JOIN "ReportingUnit" TopRU on CRUJ_top."ParentReportingUnit_Id" = TopRU."Id"
634- LEFT JOIN "CountItemType" CIT on vc."CountItemType_Id" = CIT."Id"
635- LEFT JOIN "ReportingUnit" ED on BMC."ElectionDistrict_Id" = ED."Id"
636- LEFT JOIN "ReportingUnitType" EDRUT on ED."ReportingUnitType_Id" = EDRUT."Id"
637- WHERE C.contest_type = 'BallotMeasure'
638- AND TopRU."Name" = %s -- top RU
639- AND IntermediateRUT."Txt" = %s -- intermediate_reporting_unit_type
640- AND d.{by} in %s -- tuple of datafile short_names
641- {restrict}
642- GROUP BY
643- C."Name",
644- EDRUT."Txt",
645- BMS."Name",
646- IntermediateRU."Name",
647- CIT."Txt"
648- ORDER BY
649- C."Name",
650- EDRUT."Txt",
651- BMS."Name",
652- IntermediateRU."Name",
653- CIT."Txt")
654- TO %s DELIMITER %s CSV HEADER;
655- """
612+ (SELECT
613+ 'Candidate' contest_type,
614+ C."Name" "Contest",
615+ EDRUT."Txt" contest_district_type,
616+ BMS."Name" "Selection",
617+ IntermediateRU."Name" "ReportingUnit",
618+ CIT."Txt" "CountItemType",
619+ sum(vc."Count") "Count"
620+ FROM "VoteCount" vc
621+ LEFT JOIN _datafile d on vc."_datafile_Id" = d."Id"
622+ LEFT JOIN "Contest" C on vc."Contest_Id" = C."Id"
623+ LEFT JOIN "BallotMeasureContest" BMC on vc."Contest_Id" = BMC."Id"
624+ LEFT JOIN "BallotMeasureSelection" BMS on BMS."Id" = vc."Selection_Id"
625+ -- sum over all children
626+ LEFT JOIN "ReportingUnit" ChildRU on vc."ReportingUnit_Id" = ChildRU."Id"
627+ LEFT JOIN "ComposingReportingUnitJoin" CRUJ_sum on ChildRU."Id" = CRUJ_sum."ChildReportingUnit_Id"
628+ -- roll up to the intermediate RUs
629+ LEFT JOIN "ReportingUnit" IntermediateRU on CRUJ_sum."ParentReportingUnit_Id" =IntermediateRU."Id"
630+ LEFT JOIN "ReportingUnitType" IntermediateRUT on IntermediateRU."ReportingUnitType_Id" = IntermediateRUT."Id"
631+ -- intermediate RUs must nest in top RU
632+ LEFT JOIN "ComposingReportingUnitJoin" CRUJ_top on IntermediateRU."Id" = CRUJ_top."ChildReportingUnit_Id"
633+ LEFT JOIN "ReportingUnit" TopRU on CRUJ_top."ParentReportingUnit_Id" = TopRU."Id"
634+ LEFT JOIN "CountItemType" CIT on vc."CountItemType_Id" = CIT."Id"
635+ LEFT JOIN "ReportingUnit" ED on BMC."ElectionDistrict_Id" = ED."Id"
636+ LEFT JOIN "ReportingUnitType" EDRUT on ED."ReportingUnitType_Id" = EDRUT."Id"
637+ WHERE C.contest_type = 'BallotMeasure'
638+ AND TopRU."Name" = %s -- top RU
639+ AND IntermediateRUT."Txt" = %s -- intermediate_reporting_unit_type
640+ AND d.{by} in %s -- tuple of datafile short_names
641+ {restrict}
642+ GROUP BY
643+ C."Name",
644+ EDRUT."Txt",
645+ BMS."Name",
646+ IntermediateRU."Name",
647+ CIT."Txt"
648+ ORDER BY
649+ C."Name",
650+ EDRUT."Txt",
651+ BMS."Name",
652+ IntermediateRU."Name",
653+ CIT."Txt")
654+ TO %s DELIMITER %s CSV HEADER;
655+ """
656656 ).format (by = sql .Identifier (by ), restrict = sql .SQL (restrict ))
657657 else :
658658 err_str = f"Unrecognized contest_type: { contest_type } . No results exported"
@@ -672,12 +672,12 @@ def vote_type_list(cursor, datafile_list: list, by: str = "Id") -> (list, str):
672672
673673 q = sql .SQL (
674674 """
675- SELECT distinct CIT."Txt"
676- FROM "VoteCount" VC
677- LEFT JOIN _datafile d on VC."_datafile_Id" = d."Id"
678- LEFT JOIN "CountItemType" CIT on VC."CountItemType_Id" = CIT."Id"
679- WHERE d.{by} in %s
680- """
675+ SELECT distinct CIT."Txt"
676+ FROM "VoteCount" VC
677+ LEFT JOIN _datafile d on VC."_datafile_Id" = d."Id"
678+ LEFT JOIN "CountItemType" CIT on VC."CountItemType_Id" = CIT."Id"
679+ WHERE d.{by} in %s
680+ """
681681 ).format (by = sql .Identifier (by ))
682682 try :
683683 cursor .execute (q , [tuple (datafile_list )])
@@ -794,17 +794,17 @@ def get_input_options(session, input, verbose):
794794 SELECT '{ states } '
795795 )
796796 , unnested AS (
797- SELECT UNNEST(regexp_split_to_array(states, '\n ')) AS states
798- FROM states
797+ SELECT UNNEST(regexp_split_to_array(states, '\n ')) AS states
798+ FROM states
799799 )
800800 , ordered AS (
801- SELECT *, ROW_NUMBER() OVER() AS order_by
802- FROM unnested u
801+ SELECT *, ROW_NUMBER() OVER() AS order_by
802+ FROM unnested u
803803 )
804- SELECT states as parent,
804+ SELECT states as parent,
805805 states AS name,
806806 CASE WHEN "Id" IS null THEN false ELSE true END AS type
807- FROM ordered o
807+ FROM ordered o
808808 LEFT JOIN "ReportingUnit" ru ON o.states = ru."Name"
809809 ORDER BY order_by
810810 """
@@ -821,8 +821,8 @@ def get_input_options(session, input, verbose):
821821 FROM "BallotMeasureContest" bmc
822822 JOIN "ReportingUnit" ru ON bmc."ElectionDistrict_Id" = ru."Id"
823823 JOIN "ReportingUnitType" rut ON ru."ReportingUnitType_Id" = rut."Id"
824- JOIN "Contest" c on bmc."Id" = c."Id"
825- WHERE contest_type = 'BallotMeasure'
824+ JOIN "Contest" c on bmc."Id" = c."Id"
825+ WHERE contest_type = 'BallotMeasure'
826826 ORDER BY c."Name"
827827 """
828828 )
@@ -835,8 +835,8 @@ def get_input_options(session, input, verbose):
835835 JOIN "Office" o ON cc."Office_Id" = o."Id"
836836 JOIN "ReportingUnit" ru ON o."ElectionDistrict_Id" = ru."Id"
837837 JOIN "ReportingUnitType" rut ON ru."ReportingUnitType_Id" = rut."Id"
838- JOIN "Contest" c on cc."Id" = c."Id"
839- WHERE contest_type = 'Candidate'
838+ JOIN "Contest" c on cc."Id" = c."Id"
839+ WHERE contest_type = 'Candidate'
840840 ORDER BY c."Name"
841841 """
842842 )
@@ -850,7 +850,7 @@ def get_input_options(session, input, verbose):
850850 JOIN "Party" p ON cs."Party_Id" = p."Id"
851851 JOIN "VoteCount" vc on cs."Id" = vc."Selection_Id"
852852 JOIN "CandidateContest" cc ON vc."Contest_Id" = cc."Id"
853- JOIN "Contest" ct on cc."Id" = ct."Id"
853+ JOIN "Contest" ct on cc."Id" = ct."Id"
854854 ORDER BY c."BallotName"
855855 """
856856 )
@@ -865,7 +865,7 @@ def get_input_options(session, input, verbose):
865865 JOIN "Party" p ON cs."Party_Id" = p."Id"
866866 JOIN "VoteCount" vc on cs."Id" = vc."Selection_Id"
867867 JOIN "CandidateContest" cc ON vc."Contest_Id" = cc."Id"
868- JOIN "Contest" ct on cc."Id" = ct."Id"
868+ JOIN "Contest" ct on cc."Id" = ct."Id"
869869 WHERE c."BallotName" ILIKE '%{ search_str } %'
870870 """
871871 )
@@ -1134,12 +1134,12 @@ def get_relevant_contests(session, filters):
11341134def get_jurisdiction_hierarchy (session , jurisdiction_id , subdivision_type_id ):
11351135 q = session .execute (
11361136 f"""
1137- SELECT regexp_split_to_array("Name", ';') unit_array
1138- FROM "ComposingReportingUnitJoin" j
1137+ SELECT regexp_split_to_array("Name", ';') unit_array
1138+ FROM "ComposingReportingUnitJoin" j
11391139 JOIN "ReportingUnit" ru ON j."ChildReportingUnit_Id" = ru."Id"
1140- WHERE "ParentReportingUnit_Id" = { jurisdiction_id }
1140+ WHERE "ParentReportingUnit_Id" = { jurisdiction_id }
11411141 AND "ReportingUnitType_Id" = { subdivision_type_id }
1142- LIMIT 1
1142+ LIMIT 1
11431143 """
11441144 ).fetchall ()
11451145
@@ -1149,9 +1149,9 @@ def get_jurisdiction_hierarchy(session, jurisdiction_id, subdivision_type_id):
11491149 unit = ";" .join (unit_portions [0 : i + 1 ])
11501150 q = session .execute (
11511151 f"""
1152- SELECT "ReportingUnitType_Id"
1153- FROM "ReportingUnit"
1154- WHERE "Name" = '{ unit } '
1152+ SELECT "ReportingUnitType_Id"
1153+ FROM "ReportingUnit"
1154+ WHERE "Name" = '{ unit } '
11551155 """
11561156 ).fetchall ()
11571157 hierarchy .append (q [0 ][0 ])
@@ -1160,38 +1160,38 @@ def get_jurisdiction_hierarchy(session, jurisdiction_id, subdivision_type_id):
11601160
11611161def get_candidate_votecounts (session , election_id , top_ru_id , subdivision_type_id ):
11621162 q = f"""
1163- SELECT vc."Id" as "VoteCount_Id", "Count", "CountItemType_Id",
1164- vc."ReportingUnit_Id", "Contest_Id", "Selection_Id",
1165- vc."Election_Id", "_datafile_Id", IntermediateRU."Id" as "ParentReportingUnit_Id",
1166- ChildRU."Name", ChildRU."ReportingUnitType_Id",
1167- IntermediateRU."Name" as "ParentName", IntermediateRU."ReportingUnitType_Id" as "ParentReportingUnitType_Id",
1168- CIT."Txt" as "CountItemType", C."Name" as "Contest",
1169- Cand."BallotName" as "Selection", "ElectionDistrict_Id", Cand."Id" as "Candidate_Id", "contest_type",
1170- EDRUT."Txt" as "contest_district_type"
1171- FROM "VoteCount" vc
1172- LEFT JOIN _datafile d on vc."_datafile_Id" = d."Id"
1173- LEFT JOIN "Contest" C on vc."Contest_Id" = C."Id"
1174- LEFT JOIN "CandidateSelection" CS on CS."Id" = vc."Selection_Id"
1175- LEFT JOIN "Candidate" Cand on CS."Candidate_Id" = Cand."Id"
1176- -- sum over all children
1177- LEFT JOIN "ReportingUnit" ChildRU on vc."ReportingUnit_Id" = ChildRU."Id"
1178- LEFT JOIN "ComposingReportingUnitJoin" CRUJ_sum on ChildRU."Id" = CRUJ_sum."ChildReportingUnit_Id"
1179- -- roll up to the intermediate RUs
1180- LEFT JOIN "ReportingUnit" IntermediateRU on CRUJ_sum."ParentReportingUnit_Id" =IntermediateRU."Id"
1181- LEFT JOIN "ReportingUnitType" IntermediateRUT on IntermediateRU."ReportingUnitType_Id" = IntermediateRUT."Id"
1182- -- intermediate RUs must nest in top RU
1183- LEFT JOIN "ComposingReportingUnitJoin" CRUJ_top on IntermediateRU."Id" = CRUJ_top."ChildReportingUnit_Id"
1184- LEFT JOIN "ReportingUnit" TopRU on CRUJ_top."ParentReportingUnit_Id" = TopRU."Id"
1185- LEFT JOIN "CountItemType" CIT on vc."CountItemType_Id" = CIT."Id"
1186- LEFT JOIN "CandidateContest" on C."Id" = "CandidateContest"."Id"
1187- LEFT JOIN "Office" O on "CandidateContest"."Office_Id" = O."Id"
1188- LEFT JOIN "ReportingUnit" ED on O."ElectionDistrict_Id" = ED."Id"
1189- LEFT JOIN "ReportingUnitType" EDRUT on ED."ReportingUnitType_Id" = EDRUT."Id"
1190- WHERE C.contest_type = 'Candidate'
1191- AND TopRU."Id" = { top_ru_id }
1192- AND IntermediateRU."ReportingUnitType_Id" = { subdivision_type_id }
1193- AND vc."Election_Id" = { election_id }
1194- """
1163+ SELECT vc."Id" as "VoteCount_Id", "Count", "CountItemType_Id",
1164+ vc."ReportingUnit_Id", "Contest_Id", "Selection_Id",
1165+ vc."Election_Id", "_datafile_Id", IntermediateRU."Id" as "ParentReportingUnit_Id",
1166+ ChildRU."Name", ChildRU."ReportingUnitType_Id",
1167+ IntermediateRU."Name" as "ParentName", IntermediateRU."ReportingUnitType_Id" as "ParentReportingUnitType_Id",
1168+ CIT."Txt" as "CountItemType", C."Name" as "Contest",
1169+ Cand."BallotName" as "Selection", "ElectionDistrict_Id", Cand."Id" as "Candidate_Id", "contest_type",
1170+ EDRUT."Txt" as "contest_district_type"
1171+ FROM "VoteCount" vc
1172+ LEFT JOIN _datafile d on vc."_datafile_Id" = d."Id"
1173+ LEFT JOIN "Contest" C on vc."Contest_Id" = C."Id"
1174+ LEFT JOIN "CandidateSelection" CS on CS."Id" = vc."Selection_Id"
1175+ LEFT JOIN "Candidate" Cand on CS."Candidate_Id" = Cand."Id"
1176+ -- sum over all children
1177+ LEFT JOIN "ReportingUnit" ChildRU on vc."ReportingUnit_Id" = ChildRU."Id"
1178+ LEFT JOIN "ComposingReportingUnitJoin" CRUJ_sum on ChildRU."Id" = CRUJ_sum."ChildReportingUnit_Id"
1179+ -- roll up to the intermediate RUs
1180+ LEFT JOIN "ReportingUnit" IntermediateRU on CRUJ_sum."ParentReportingUnit_Id" =IntermediateRU."Id"
1181+ LEFT JOIN "ReportingUnitType" IntermediateRUT on IntermediateRU."ReportingUnitType_Id" = IntermediateRUT."Id"
1182+ -- intermediate RUs must nest in top RU
1183+ LEFT JOIN "ComposingReportingUnitJoin" CRUJ_top on IntermediateRU."Id" = CRUJ_top."ChildReportingUnit_Id"
1184+ LEFT JOIN "ReportingUnit" TopRU on CRUJ_top."ParentReportingUnit_Id" = TopRU."Id"
1185+ LEFT JOIN "CountItemType" CIT on vc."CountItemType_Id" = CIT."Id"
1186+ LEFT JOIN "CandidateContest" on C."Id" = "CandidateContest"."Id"
1187+ LEFT JOIN "Office" O on "CandidateContest"."Office_Id" = O."Id"
1188+ LEFT JOIN "ReportingUnit" ED on O."ElectionDistrict_Id" = ED."Id"
1189+ LEFT JOIN "ReportingUnitType" EDRUT on ED."ReportingUnitType_Id" = EDRUT."Id"
1190+ WHERE C.contest_type = 'Candidate'
1191+ AND TopRU."Id" = { top_ru_id }
1192+ AND IntermediateRU."ReportingUnitType_Id" = { subdivision_type_id }
1193+ AND vc."Election_Id" = { election_id }
1194+ """
11951195 result = session .execute (q )
11961196 result_df = pd .DataFrame (result )
11971197 result_df .columns = result .keys ()
0 commit comments