Skip to content

Commit

Permalink
Add CI and MyPy
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Jun 23, 2024
1 parent 9ae167b commit 50f6f3a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Tests
on: [pull_request, workflow_dispatch]
jobs:
test:
if: github.repository_owner == 'ParkingReformNetwork'
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Initialize Pants
uses: pantsbuild/actions/init-pants@main
with:
gha-cache-key: cache0-py${{ matrix.python_version }}
named-caches-hash: ${{ hashFiles('*.lock') }}
- name: Lint
run: pants lint ::
- name: Typecheck
run: pants check ::
- name: Run tests
run: pants test ::
5 changes: 3 additions & 2 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
pants_version = "2.20.1"
backend_packages = [
"pants.backend.python",
"pants.backend.python.lint.black",
"pants.backend.build_files.fmt.black",
"pants.backend.experimental.python.lint.ruff.check",
"pants.backend.experimental.python.lint.ruff.format",
"pants.backend.python.typecheck.mypy",
]

[anonymous-telemetry]
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[[tool.mypy.overrides]]
module = [
"geopy",
"uszipcode"
]
ignore_missing_imports = true
15 changes: 8 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import metro_csvs
from mailchimp_entry import MailchimpEntry
from salesforce_entry import SalesforceEntry
from salesforce_api import load_salesforce_data
from salesforce_api import init_salesforce_client, load_salesforce_data


def main() -> None:
entries = load_salesforce_data()
salesforce_client = init_salesforce_client()
entries = load_salesforce_data(salesforce_client)

# TODO: read in Mailchimp data
mailchimp_by_email = {}
mailchimp_by_email: dict[str, MailchimpEntry] = {}

us_zip_to_metro = metro_csvs.read_us_zip_to_metro()
us_city_and_state_to_metro = metro_csvs.read_us_city_and_state_to_metro()
Expand All @@ -23,9 +23,10 @@ def main() -> None:

for entry in entries:
# The order of operations matters.
entry.populate_via_latitude_longitude(
mailchimp_by_email.get(entry.email), geocoder
)
if entry.email:
entry.populate_via_latitude_longitude(
mailchimp_by_email.get(entry.email), geocoder
)
entry.normalize()
entry.populate_via_zipcode(zipcode_search_engine)
entry.populate_metro_area(us_zip_to_metro, us_city_and_state_to_metro)
Expand Down
15 changes: 9 additions & 6 deletions src/salesforce_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
from salesforce_entry import SalesforceEntry


INSTANCE_URL = os.environ.pop("SALESFORCE_INSTANCE_URL")
TOKEN = os.environ.pop("SALESFORCE_TOKEN")
sf = Salesforce(instance_url=INSTANCE_URL, session_id=TOKEN)
def init_salesforce_client() -> Salesforce:
INSTANCE_URL = os.environ.pop("SALESFORCE_INSTANCE_URL")
TOKEN = os.environ.pop("SALESFORCE_TOKEN")
return Salesforce(instance_url=INSTANCE_URL, session_id=TOKEN)


def load_salesforce_data() -> list[SalesforceEntry]:
fields = ", ".join(info.alias for info in SalesforceEntry.model_fields.values())
def load_salesforce_data(client: Salesforce) -> list[SalesforceEntry]:
fields = ", ".join(
info.alias or name for name, info in SalesforceEntry.model_fields.items()
)
return [
SalesforceEntry(**raw)
for raw in sf.query_all_iter(f"SELECT {fields} FROM Contact")
for raw in client.query_all_iter(f"SELECT {fields} FROM Contact")
]
20 changes: 13 additions & 7 deletions src/salesforce_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def normalize(self) -> None:
def populate_via_latitude_longitude(
self, mailchimp: MailchimpEntry | None, geocoder: Nominatim
) -> None:
mailchimp_missing = mailchimp is None or not (
mailchimp.latitude and mailchimp.longitude
)
if mailchimp is None or not (mailchimp.latitude and mailchimp.longitude):
return

metro_area_can_be_computed = self.zipcode or (self.city and self.country)
if mailchimp_missing or metro_area_can_be_computed:
if metro_area_can_be_computed:
return

addr = geocoder.reverse(f"{mailchimp.latitude}, {mailchimp.longitude}").raw[
Expand Down Expand Up @@ -110,6 +110,12 @@ def populate_metro_area(
) -> None:
if self.country != "USA":
return
self.metro = us_zip_to_metro.get(
self.zipcode
) or us_city_and_state_to_metro.get((self.city, self.state))

new_metro = None
if self.zipcode:
new_metro = us_zip_to_metro.get(self.zipcode)
elif self.city and self.state:
new_metro = us_city_and_state_to_metro.get((self.city, self.state))

if new_metro is not None:
self.metro = new_metro

0 comments on commit 50f6f3a

Please sign in to comment.