Skip to content

Commit

Permalink
Merge branch 'pyproject'
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinbjornt committed Sep 18, 2023
2 parents 4a89db7 + da82908 commit 307d3dd
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 128 deletions.
27 changes: 12 additions & 15 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Build
name: tests

on:
push:
branches: [ master ]
branches:
- '*'
pull_request:
branches: [ master ]
branches:
- '*'

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8"]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
python -m pip install pytest types-setuptools mypy
- name: Run mypy
python -m pip install ".[dev]"
- name: Run ruff linter
run: |
mypy iceaddr/*.py
ruff src/iceaddr
- name: Test with pytest
run: |
python -m pytest
python -m pytest -vvv
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __pycache__/
*.py[cod]
*$py.class/
/dist/
/*.egg-info
*.egg-info
*.shp
p3*/
/stadfangaskra.db
Expand All @@ -18,3 +18,5 @@ venv
*.csv
.vscode
*.pyc
.coverage
.coverage.*
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# iceaddr

[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)
[![Release](https://shields.io/github/v/release/sveinbjornt/iceaddr?display_name=tag)]()
[![PyPI](https://img.shields.io/pypi/v/iceaddr)]()
[![Build](https://github.com/sveinbjornt/iceaddr/actions/workflows/python-package.yml/badge.svg)]()

### Look up Icelandic street addresses, postcodes and placenames

`iceaddr` is a Python 3.7+ package to look up information about Icelandic streets, addresses, placenames,
`iceaddr` is a Python >=3.8 package to look up information about Icelandic streets, addresses, placenames,
landmarks, locations and postcodes. The underlying data is contained in a local SQLite database assembled
from the following sources:

Expand Down Expand Up @@ -243,7 +243,7 @@ Landakotsvöllur

## Build process

To build your own version of the package, you need to have Python 3.7+ installed.
To build your own version of the package, you need to have Python >=3.8 installed.
Then, after optionally creating a virtual environment, run the following command
from the repository root to install dependencies:

Expand All @@ -269,6 +269,7 @@ python setup.py install

## Version History

* 0.5.7: Updated address and placename data. Now requires Python 3.8+ (18/09/2024)
* 0.5.6: Updated address and placename data. (11/08/2023)
* 0.5.5: Updated address and placename data. Removed ISN93 coords. Now requires Python 3.7+ (11/12/2022)
* 0.5.4: Updated address and placename data (09/11/2022)
Expand Down
10 changes: 5 additions & 5 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
import fiona # type: ignore

from iceaddr.dist import in_iceland

Expand All @@ -30,7 +30,7 @@
DEFAULT_DBNAME = "iceaddr.db"


def center_point(coords: List[Tuple]) -> Tuple[float, float]:
def center_point(coords: List[Tuple[float, float]]) -> Tuple[float, float]:
"""Find the center point of a given set of coordinates."""
x: float = 0
y: float = 0
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
14 changes: 8 additions & 6 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
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 @@ -164,8 +166,8 @@ def main() -> None:
print("\tInserting: %d\r" % cnt, end="")
sys.stdout.flush()

bytesize = os.stat(db_path).st_size
human_size = humanize.naturalsize(bytesize)
bytesize: int = os.stat(db_path).st_size
human_size = humanize.naturalsize(bytesize) # type: ignore

print("\nCreated database with %d entries (%s)" % (cnt, human_size))

Expand Down
14 changes: 6 additions & 8 deletions build_postcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,29 @@ def main() -> None:
# the dative form (þgf.). Try to lemmatise to nominative (nf.) using Reynir.
postcode = int(r["Póstnúmer"])
if postcode not in pc_keys:
logging.warning(
"Postcode '{0}' did not already exist in data.".format(postcode)
)
logging.warning(f"Postcode '{postcode}' did not already exist in data.")
changed = True

tp = r["Tegund"]
p_dat = _clean_name(r["Staður"])
p_nom = NounPhrase(p_dat).nominative
if not p_nom:
logging.warning("Unable to decline placename '{0}'".format(p_dat))
logging.warning(f"Unable to decline placename '{p_dat}'")
p_nom = p_dat

if pc[postcode]["stadur_nf"] != p_nom:
pc[postcode]["stadur_nf"] = p_nom
print("{0} --> {1}".format(pc[postcode]["stadur_nf"], p_nom))
print(f"{pc[postcode]['stadur_nf']} --> {p_nom}")
changed = True

if pc[postcode]["stadur_tgf"] != p_dat:
pc[postcode]["stadur_tgf"] = p_dat
print("{0} --> {1}".format(pc[postcode]["stadur_tgf"], p_dat))
print(f"{pc[postcode]['stadur_tgf']} --> {p_dat}")
changed = True

if pc[postcode]["tegund"] != tp:
pc[postcode]["tegund"] = tp
print("{0} --> {1}".format(pc[postcode]["tegund"], tp))
print(f"{pc[postcode]['tegund']} --> {tp}")
changed = True

if not changed:
Expand All @@ -78,5 +76,5 @@ def main() -> None:


if __name__ == "__main__":
""" Command line invocation. """
"""Command line invocation."""
main()
151 changes: 151 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
[project]
name = "iceaddr"
version = "0.5.7"
description = "Look up information about Icelandic street addresses, postcodes, landmarks, locations and placenames"
authors = [
{ name = "Sveinbjorn Thordarson", email = "[email protected]" },
]
readme = { file = "README.md", content-type = "text/markdown" }
license = { file = "LICENSE.txt" }
# For classifier list see: https://pypi.org/pypi?%3Aaction=list_classifiers
classifiers = [
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Natural Language :: Icelandic",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Utilities",
"Topic :: Text Processing :: Linguistic",
]
requires-python = ">=3.8"
dependencies = []

[project.urls]
Repository = "https://github.com/sveinbjornt/iceaddr"

[project.optional-dependencies]
# Dev dependencies
dev = [
# Building database
"fiona>=1.9.4", # for processing GIS data
"humanize>=0.5.1", # for progress bar
"reynir>=3.1.0", # for parsing and declining placenames
# Linting, testing
"pytest>=7.2.1",
"pre-commit>=3.3.3",
"black>=23.7.0",
"mypy>=1.5.1",
"types-setuptools>=68.2.0",
"ruff>=0.0.285",
"coverage[toml]>=7.3.1",
]

# *** Configuration of tools ***

[tool.pytest.ini_options]
filterwarnings = [
# Ignore deprecation warnings in libraries, their problem not ours
"ignore::DeprecationWarning",
]

[tool.coverage.run]
branch = true # Enable branch coverage
source = ["src/iceaddr"] # Only test coverage of `iceaddr`
command_line = "-m pytest" # Run all tests when calculating coverage

[tool.coverage.report]
exclude_also = ["if TYPE_CHECKING:", "raise NotImplementedError"]
skip_covered = true # Skip showing fully covered files
skip_empty = true # Skip empty files
sort = "-Cover" # Sort by coverage percentage
precision = 2 # Precision of output percentage
fail_under = 65 # Fail if total covered under threshold


[tool.mypy]
overrides = []


[tool.pyright]
typeCheckingMode = "strict"
# The following settings are off by default, even in strict mode
reportCallInDefaultInitializer = "information"
reportImplicitOverride = "information"
reportImplicitStringConcatenation = "information"
reportImportCycles = "warning"
reportMissingSuperCall = "none"
reportPropertyTypeMismatch = "warning"
reportShadowedImports = "warning"
reportUninitializedInstanceVariable = "information"
reportUnnecessaryTypeIgnoreComment = "warning"
reportUnusedCallResult = "none"


[tool.ruff]
# See https://beta.ruff.rs/docs/rules/ for list of rules
# Enable all rules
select = ["ALL"]
# Ignore specific rules
# (we should aim to have these as few as possible)
ignore = [
"D", # Docstring style rules
"ANN", # Missing type annotations
"TD", # Pedantic TODO comment rules
"FIX002", # Line contains TODO rule
"SLOT000", # str subclass should define __slots__
"SIM105", # contextlib.suppress rule
"BLE", # Blind `except:`
"A", # Shadowing of builtins
"ERA", # Commented out code
"FBT", # Forbids boolean positional arguments
"COM", # Commas (sometimes takes issue with black formatting)
"S101", # Disallow assert statements rule
"PLR0912", # "Too many statements" rule
"C90", # Function complexity rules
"UP", # Deprecation rules
"TRY", # Pedantic exception rules
"EM", # Pedantic exception message rules
"TID", # No relative parent imports rule
"TCH", # Move into type checking block - too risky, causes issues
"RSE102", # Bug: https://github.com/astral-sh/ruff/issues/5416
]
# Silence complaints when black doesn't format
# lines that are slightly over 88 characters long
line-length = 100

[tool.ruff.per-file-ignores]
"./run.py" = ["T201"]
"cli_client/run.py" = ["INP001", "T201", "PLR0915"]

[tool.ruff.isort]
section-order = [
"future",
"typehints",
"standard-library",
"third-party",
"first-party",
"local-folder",
]

[tool.ruff.isort.sections]
# Have typing libraries above other imports
typehints = ["typing", "typing_extensions", "types", "types-setuptools"]

# *** Build system configuration ***

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
iceaddr = ["*.db"]

[build-system]
requires = ["setuptools>=45", "setuptools_scm>=6.2"]
build-backend = "setuptools.build_meta"
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

Loading

0 comments on commit 307d3dd

Please sign in to comment.