py83 lines3.5 KB
| 1 | """Owner portfolios: groups every parcel an owner holds into one row with property count, cities and total assessed value.""" |
| 2 | from collections import Counter, defaultdict |
| 3 | |
| 4 | from .classify import TRUST_RE, classify, group_key, norm_display |
| 5 | |
| 6 | COMPANY_COLS = ["name", "tier", "total_parcels", "cities", "n_cities", |
| 7 | "total_assessed_value", "mailing_address", "mailing_city", |
| 8 | "mailing_state", "mailing_zip", "name_variants"] |
| 9 | |
| 10 | |
| 11 | def best_form(forms): |
| 12 | """Most common original spelling; ties -> longest then alphabetical.""" |
| 13 | m = max(forms.values()) |
| 14 | return sorted([f for f, n in forms.items() if n == m], key=lambda x: (-len(x), x))[0] |
| 15 | |
| 16 | |
| 17 | def mode_or_blank(counter): |
| 18 | return counter.most_common(1)[0][0] if counter else "" |
| 19 | |
| 20 | |
| 21 | def build_portfolios(parcels, city_order=None): |
| 22 | """Aggregate parcel rows into one portfolio per owner, largest first. |
| 23 | |
| 24 | ``parcels`` yields dicts with the normalized parcel columns: city, |
| 25 | parcel_id, owner_name, total_value, mailing_address, mailing_city, |
| 26 | mailing_state, mailing_zip. Returns ``(rows, trust_rows)``: trusts are |
| 27 | quarantined to their own list instead of deleted, so the decision stays |
| 28 | auditable and a trust that is really an operating company can be rescued |
| 29 | on review. |
| 30 | """ |
| 31 | combined = defaultdict(lambda: {"parcels": 0, "forms": Counter(), "cities": Counter(), |
| 32 | "value": 0, "has_value": False, "mail": Counter()}) |
| 33 | seen = set() |
| 34 | for r in parcels: |
| 35 | pid = (r.get("city", ""), r.get("parcel_id", "")) |
| 36 | # dedupe: address points and view-join fan-out repeat a parcel id |
| 37 | if pid[1] and pid in seen: |
| 38 | continue |
| 39 | if pid[1]: |
| 40 | seen.add(pid) |
| 41 | owner = r.get("owner_name", "") |
| 42 | if not owner.strip(): |
| 43 | continue |
| 44 | up = norm_display(owner).upper() |
| 45 | key = group_key(up) |
| 46 | bucket = combined[key] |
| 47 | bucket["parcels"] += 1 |
| 48 | bucket["forms"][up] += 1 |
| 49 | tv = r.get("total_value", "") |
| 50 | if tv != "": |
| 51 | bucket["value"] += int(float(tv)) |
| 52 | bucket["has_value"] = True |
| 53 | mail = (r.get("mailing_address", ""), r.get("mailing_city", ""), |
| 54 | r.get("mailing_state", ""), r.get("mailing_zip", "")) |
| 55 | if any(mail): |
| 56 | bucket["mail"][mail] += 1 |
| 57 | bucket["cities"][r.get("city", "")] += 1 |
| 58 | |
| 59 | order = list(city_order or []) |
| 60 | rows = [] |
| 61 | for key, v in combined.items(): |
| 62 | mail = mode_or_blank(v["mail"]) or ("", "", "", "") |
| 63 | cities = [c for c in order if c in v["cities"]] + sorted(c for c in v["cities"] |
| 64 | if c not in order) |
| 65 | rows.append({ |
| 66 | "name": best_form(v["forms"]), |
| 67 | "tier": classify(key), |
| 68 | "total_parcels": v["parcels"], |
| 69 | "cities": " ".join(f"{c}:{v['cities'][c]}" for c in cities), |
| 70 | "n_cities": len(v["cities"]), |
| 71 | "total_assessed_value": str(v["value"]) if v["has_value"] else "", |
| 72 | "mailing_address": mail[0], "mailing_city": mail[1], |
| 73 | "mailing_state": mail[2], "mailing_zip": mail[3], |
| 74 | "name_variants": len(v["forms"]), |
| 75 | "_key": key, |
| 76 | }) |
| 77 | rows.sort(key=lambda r: (-r["total_parcels"], r["name"])) |
| 78 | |
| 79 | is_trust = lambda r: (r["tier"] == "TRUST" |
| 80 | or bool(TRUST_RE.search(r["name"].upper()))) |
| 81 | trust_rows = [r for r in rows if is_trust(r)] |
| 82 | rows = [r for r in rows if not is_trust(r)] |
| 83 | return rows, trust_rows |