From a2c28d089a9aee1bd147af4bb15e43d22224ed0e Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 23 Nov 2023 00:24:30 -0500 Subject: [PATCH] Fix contributions, maybe --- fec.py | 34 ++++++++++++++++++++++++++++---- server/data/fec/contributions.py | 9 ++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/fec.py b/fec.py index 15b90f8..bb0ea85 100755 --- a/fec.py +++ b/fec.py @@ -1,11 +1,17 @@ #!/usr/bin/env python3 # ruff: noqa: E501 +import json + import click -from server.data.fec.contributions import ContributionsManager +from server.data.fec.contributions import ( + ContributionsManager, + ContributionSummariesManager, + FuzzyIdentifier, +) from server.data.manager import DataManager -from server.data.names.nicknames import MessyNicknamesManager +from server.data.names.nicknames import MessyNicknamesManager, NicknamesManager @click.group() @@ -59,9 +65,29 @@ def summarize(data: str | None = None): @contributions.command() -def search(): +@click.argument("first_name") +@click.argument("last_name") +@click.argument("zip_code") +@click.option( + "--data", + type=click.Path(exists=True), + help="Path to data dir.", + required=False, + default=None, +) +def search(first_name: str, last_name: str, zip_code: str, data: str | None = None): """Search summarized FEC contributions data.""" - pass + data_manager = DataManager(data) if data is not None else DataManager.default() + nicknames_manager = NicknamesManager.from_data_manager(data_manager) + fuzzy_id = FuzzyIdentifier( + last_name, first_name, zip_code, get_nickname_index=nicknames_manager + ).fuzzy_id + summaries_manager = ContributionSummariesManager.from_data_manager(data_manager) + summary = summaries_manager.get_summary(fuzzy_id) + if summary is None: + print("No matching summary.") + else: + print(json.dumps(summary.to_data(), indent=2)) if __name__ == "__main__": diff --git a/server/data/fec/contributions.py b/server/data/fec/contributions.py index a0fba8d..bf106a0 100644 --- a/server/data/fec/contributions.py +++ b/server/data/fec/contributions.py @@ -105,7 +105,7 @@ def _first_nickname(self) -> str | None: def _make_fuzzy_id(self) -> str: """Make the fuzzy ID.""" - return f"{self.last_name}-{self._first_nickname}-{self.zip_code}".upper() + return f"{self.last_name}-{self._first_nickname}-{self.zip_code[:5]}".upper() def _make_fuzzy_id_if_needed(self) -> None: if self._fuzzy_id is None: @@ -329,7 +329,7 @@ def from_data(cls, value: t.Any) -> "ContributionSummary": zip_code=v.get_str(data, "zip_code"), total=v.get_convert_decimal(data, "total"), by_party={ - party: v.validate_convert_decimal(amount) + (None if party == "null" else party): v.validate_convert_decimal(amount) for party, amount in by_party_data.items() }, by_committee={ @@ -345,7 +345,10 @@ def to_data(self) -> dict: "name": self.name, "zip_code": self.zip_code, "total": str(self.total), - "by_party": {party: str(amount) for party, amount in self.by_party.items()}, + "by_party": { + party if party else "null": str(amount) + for party, amount in self.by_party.items() + }, "by_committee": { committee: str(amount) for committee, amount in self.by_committee.items()