From cb840a4a26b3f69cb8b0c5facd54386f127feeac Mon Sep 17 00:00:00 2001 From: Sveinbjorn Thordarson Date: Mon, 18 Sep 2023 18:30:58 +0000 Subject: [PATCH] More typing fixes, moved iceaddrb from resources dir to iceaddr/ --- add_placename_data.py | 8 ++++---- build_db.py | 12 +++++++----- pyproject.toml | 4 +--- src/iceaddr/db.py | 4 ++-- src/iceaddr/{resources => }/iceaddr.db | Bin tests/test_iceaddr.py | 20 ++++++++++---------- 6 files changed, 24 insertions(+), 24 deletions(-) rename src/iceaddr/{resources => }/iceaddr.db (100%) diff --git a/add_placename_data.py b/add_placename_data.py index 64c4b3b..d9ad074 100755 --- a/add_placename_data.py +++ b/add_placename_data.py @@ -7,11 +7,11 @@ from typing import List, Tuple -from pprint import pprint +from pprint import pprint # type: ignore import sqlite3 from pathlib import Path -import fiona # noqa +import fiona # type: ignore from iceaddr.dist import in_iceland @@ -83,7 +83,7 @@ def delete_table(dbpath: str) -> sqlite3.Connection: return dbconn -def add_placename_additions(dbc) -> None: +def add_placename_additions(dbc: sqlite3.Connection) -> None: """Read manual placename additions from text file, insert into "ornefni" DB table.""" print("Inserting placename additions") f = open("placename_additions.txt", "r") @@ -114,7 +114,7 @@ def add_placename_additions(dbc) -> None: dbc.commit() -def add_placenames_from_is50v(dbc) -> None: +def add_placenames_from_is50v(dbc: sqlite3.Connection) -> None: """Read IS50V geo layers from file, add all placenames ("örnefni") to DB.""" if not Path(GPKG_FILE).exists(): print(f"Could not find file {GPKG_FILE}") diff --git a/build_db.py b/build_db.py index b488a3b..524f116 100755 --- a/build_db.py +++ b/build_db.py @@ -10,7 +10,7 @@ """ -from typing import Iterator, Dict +from typing import Iterator, Dict, Any from builtins import input @@ -23,7 +23,7 @@ from urllib.request import urlopen from iceaddr.dist import in_iceland -import humanize # noqa +import humanize # type: ignore STADFONG_REMOTE_URL = "https://fasteignaskra.is/Stadfangaskra.csv" @@ -76,13 +76,15 @@ def create_db(path: str) -> sqlite3.Connection: return dbconn -def read_rows(dsv_file: TextIOWrapper, delimiter: str = ",") -> Iterator: +def read_rows( + dsv_file: TextIOWrapper, delimiter: str = "," +) -> Iterator[Dict[Any, Any]]: reader = csv.DictReader(dsv_file, delimiter=delimiter) for row in reader: yield row -def insert_address_entry(e: Dict, conn: sqlite3.Connection) -> None: +def insert_address_entry(e: Dict[Any, Any], conn: sqlite3.Connection) -> None: # The stadfong datafile is quite dirty so we need to # sanitise values before inserting into the database @@ -165,7 +167,7 @@ def main() -> None: sys.stdout.flush() bytesize: int = os.stat(db_path).st_size - human_size = humanize.naturalsize(bytesize) + human_size = humanize.naturalsize(bytesize) # type: ignore print("\nCreated database with %d entries (%s)" % (cnt, human_size)) diff --git a/pyproject.toml b/pyproject.toml index 142dbce..4a670fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -139,11 +139,9 @@ typehints = ["typing", "typing_extensions", "types"] # *** Build system configuration *** -[tool.setuptools_scm] -fallback_version = "0.0.0" - [tool.setuptools.packages.find] where = ["src"] + [tool.setuptools.package-data] iceaddr = ["*.db"] diff --git a/src/iceaddr/db.py b/src/iceaddr/db.py index 1f74efc..604701c 100644 --- a/src/iceaddr/db.py +++ b/src/iceaddr/db.py @@ -13,11 +13,11 @@ from pkg_resources import resource_filename -_DB_REL_PATH = "resources/iceaddr.db" +_DB_REL_PATH = "iceaddr.db" class SharedDB: - """Singleton object wrapper for local SQLite3 DB""" + """Singleton object wrapper for local SQLite3 database.""" def __init__(self): self.db_conn = None diff --git a/src/iceaddr/resources/iceaddr.db b/src/iceaddr/iceaddr.db similarity index 100% rename from src/iceaddr/resources/iceaddr.db rename to src/iceaddr/iceaddr.db diff --git a/tests/test_iceaddr.py b/tests/test_iceaddr.py index d17888f..ff01dfb 100644 --- a/tests/test_iceaddr.py +++ b/tests/test_iceaddr.py @@ -9,7 +9,7 @@ """ -from typing import Dict, List, Optional +from typing import Dict, Optional import sys import os @@ -25,17 +25,17 @@ postcodes_for_region, POSTCODES, nearest_addr, - nearest_placenames, + # nearest_placenames, ) def test_address_lookup(): """Test address lookup using various known addresses.""" ADDR_TO_POSTCODE = [ - ["Öldugata", 4, "Reykjavík", 101], - ["öldugötu", 12, "hafnarfirði", 220], - ["Tómasarhaga", 12, "Reykjavík", 107], - ["smiðjuvegur", 22, None, 200], + ("Öldugata", 4, "Reykjavík", 101), + ("öldugötu", 12, "hafnarfirði", 220), + ("Tómasarhaga", 12, "Reykjavík", 107), + ("smiðjuvegur", 22, "", 200), ] for a in ADDR_TO_POSTCODE: @@ -46,16 +46,16 @@ def test_address_lookup(): assert res and res[0]["postnr"] == 310 and res[0]["stadur_nf"] == "Borgarnes" POSTCODE_TO_PLACENAME = [ - ["Öldugata", 4, 101, "Reykjavík", "Höfuðborgarsvæðið", "Þéttbýli"], - [ + ("Öldugata", 4, 101, "Reykjavík", "Höfuðborgarsvæðið", "Þéttbýli"), + ( "dagverðardalur", 11, 400, "Ísafjörður", "Vesturland og Vestfirðir", "Þéttbýli", - ], - ["Höfðabraut", 3, 805, "Selfoss", "Suðurland og Reykjanes", "Þéttbýli"], + ), + ("Höfðabraut", 3, 805, "Selfoss", "Suðurland og Reykjanes", "Þéttbýli"), ] for p in POSTCODE_TO_PLACENAME: