Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/state breakdown #334

Merged
merged 8 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions code/adhoc_tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import csv
import glob
import logging
import os
Expand Down Expand Up @@ -257,6 +258,36 @@ def generate_all_suburbs_nbn_tallies():
utils.write_json_file("results/all-suburbs-nbn-tallies.json", tallies, indent=1)


def generate_state_breakdown():
"""Generate results/breakdown.STATE.csv containing history of connection-types by state"""
output = {}
all_ctypes = set()
for date, state_info in utils.read_json_file("results/breakdown-suburbs.json").items():
logging.info("Processing %s", date)
output[date] = {}
for state, suburb_list in state_info.items():
# logging.info(" State: %s", state)
state_tally = {}
for suburb, connections in suburb_list.items():
# logging.info(" State: %s", suburb)
for ctype, ccount in connections.items():
state_tally[ctype] = state_tally.get(ctype, 0) + ccount
all_ctypes.add(ctype)
output[date][state] = state_tally
utils.write_json_file("results/breakdown-state.json", output)

# write CSV per state
for state in data.STATES:
rows = [
{"date": date} | {ctype: output[date].get(state, {}).get(ctype, 0) for ctype in all_ctypes}
for date in output
]
with open(f"results/breakdown.{state}.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(rows[0].keys())
writer.writerows(r.values() for r in rows)


if __name__ == "__main__":
LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper()
logging.basicConfig(level=LOGLEVEL, format="%(asctime)s %(levelname)s %(threadName)s %(message)s")
Expand Down
4 changes: 2 additions & 2 deletions code/nbn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import difflib
import logging
import urllib.parse

import difflib
import diskcache
import requests
from requests.adapters import HTTPAdapter
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_nbn_loc_id(self, key: str, address: str) -> str:
suggestions = sorted(
suggestions,
key=lambda s: difflib.SequenceMatcher(None, address, s["formattedAddress"]).ratio(),
reverse=True
reverse=True,
)
if suggestions:
loc_id = result["suggestions"][0]["id"]
Expand Down
3 changes: 2 additions & 1 deletion code/update_breakdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime

import utils
from adhoc_tools import get_tech_and_upgrade_breakdown
from adhoc_tools import generate_state_breakdown, get_tech_and_upgrade_breakdown
from tabulate import tabulate


Expand Down Expand Up @@ -40,3 +40,4 @@ def print_breakdowns(breakdowns):
logging.basicConfig(level=logging.INFO)
bd = update_breakdown()
print_breakdowns(bd)
generate_state_breakdown()