Skip to content

Commit

Permalink
More typing fixes, moved iceaddrb from resources dir to iceaddr/
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinbjornt committed Sep 18, 2023
1 parent e6de04a commit cb840a4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions add_placename_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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}")
Expand Down
12 changes: 7 additions & 5 deletions build_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

from typing import Iterator, Dict
from typing import Iterator, Dict, Any

from builtins import input

Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
4 changes: 2 additions & 2 deletions src/iceaddr/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions tests/test_iceaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from typing import Dict, List, Optional
from typing import Dict, Optional

import sys
import os
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit cb840a4

Please sign in to comment.