Country lookup: every ISO country, the widest unit wins, several European countries are EU

country_of folds with _place_fold (ł, đ, ø, æ map to ASCII; _fold keeps building
company_key unchanged), knows every ISO country by English name and European
ones by their own, and the cities among the 150 most frequent unresolved
places on the live data. Within one location a country beats a US state or
Canadian province, which beats a city. A state code that is also an ISO code
counts as the state only when the line names a US place. Several countries,
all European, are EU; otherwise ''. A code in parentheses is read when no
name matched.

Migration 12 recomputes country from place for roles extract has not read
and whose board sent no country in raw. On the live data: open roles with no
country 2,827 -> 1,183 of 16,841; 1,705 open roles change, 48 from a code.

Co-Authored-By: Claude Opus <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus committed 2026-09-29 15:06:15 +02:00
1 parent 706b127bc0
commit 2c4b992c50
6 files changed
+611 -109

No files matched your search

+27
View File
@@ -50,6 +50,33 @@ def store(tmp_path: Path) -> Store:
("London or New York", ""),
("Remote", ""),
("", ""),
("Wrocław", "PL"), # ł and đ do not decompose under NFKD
("Łódź, Poland", "PL"),
("Bogota, CO", "CO"), # a state code that is a country's too
("Paderborn, DE", ""), # ... is no US state without a US place
("Sliema, MT", ""),
("Casablanca, MA", "MA"),
("Cambridge, MA", "US"),
("Chicago, IL", "US"),
("Madrid, MD, Spain", "ES"),
("Dublin, London", "EU"), # several European countries
("Anywhere in France, Belgium, Spain", "EU"),
("Amsterdam, Netherlands; London, United Kingdom; Remote - Europe", "EU"),
("Remote, EU or US", ""), # a region and a country outside it
("London, Ontario", "CA"), # a province beats the city before it
("Bellevue, Washington", "US"),
("Tilburg, NB, Netherlands", "NL"), # a country beats a province code
("Atlanta, Georgia", "US"),
("Tbilisi, Georgia", "GE"),
("Malaysia", "MY"),
("Belgrade, Serbia", "RS"),
("Dubai", "AE"),
("United Arab Emirates", "AE"),
("Buenos Aires, , Argentina", "AR"),
("Leicester (GB)", "GB"), # a code in parentheses when nothing else
("Cagliari (CA)", "IT"), # ... and not over a name
("Hybrid", ""),
("Distributed", ""),
])
def test_country_of(place: str, code: str) -> None:
assert country_of(place) == code
+28 -1
View File
@@ -66,7 +66,7 @@ def test_schema_creates_on_an_empty_file(tmp_path: Path) -> None:
"application_events", "agents", "scores", "state"} <= tables
assert "idx_roles_cluster" in indexes
assert store.migrations_applied == []
assert LATEST == 11
assert LATEST == 12
# A second open finds it current and applies nothing.
assert Store(path).migrations_applied == []
@@ -257,6 +257,33 @@ def test_v10_backfills_posted_at_by_the_same_rule_upsert_uses(
assert after_untrusted.posted_at == before_untrusted.posted_at == before_untrusted.first_seen
def test_v12_recomputes_country_from_place_unless_the_board_or_extract_set_it(
tmp_path: Path, clock: Clock) -> None:
path = tmp_path / "v11.db"
store = Store(path)
store.clock = clock
wrong = posting("1", place="Bogota, CO") # was read as US
several = posting("2", place="Dublin, London") # was ''
given = posting("3", place="Bogota, CO", country="US") # the board said so
extracted = posting("4", place="Bogota, CO")
for item in (wrong, several, given, extracted):
store.upsert_role(item)
with sqlite3.connect(path) as conn:
conn.execute("UPDATE roles SET country = 'US' WHERE place = 'Bogota, CO'")
conn.execute("UPDATE roles SET country = '' WHERE uid = ?", (several.uid,))
conn.execute("UPDATE roles SET extracted_at = '2026-09-23' WHERE uid = ?",
(extracted.uid,))
conn.execute("PRAGMA user_version = 11")
migrated = Store(path)
assert migrated.migrations_applied == list(range(12, LATEST + 1))
countries = {uid: role.country for uid in (wrong.uid, several.uid, given.uid, extracted.uid)
if (role := migrated.role(uid)) is not None}
assert countries == {wrong.uid: "CO", several.uid: "EU", given.uid: "US",
extracted.uid: "US"}
def test_state_is_a_mapping(store: Store) -> None:
store.state["ats:last"] = "2026-09-23"
assert store.state["ats:last"] == "2026-09-23"