Skip to content

Commit

Permalink
Merge pull request #29 from quantmind/ls-mypy
Browse files Browse the repository at this point in the history
mypy
  • Loading branch information
lsbardel committed Jul 20, 2023
2 parents 43f9c3b + 739def9 commit a0843cd
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 641 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.10", "3.11"]

steps:
- uses: actions/checkout@v2
Expand Down
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ install: ## install packages for development
@pip install -U pip poetry
@poetry install

lint: ## run linters
@poetry run dev/lint
lint: ## Run linters
@poetry run ./dev/lint fix


lint-check: ## Run linters in check mode
@poetry run ./dev/lint


test: ## test with python 3.8 with coverage
@poetry run pytest -x -v --cov --cov-report xml

codecov: ## upload code coverage
@poetry run codecov --token $(CODECOV_TOKEN) --file ./build/coverage.xml

publish: ## release to pypi and github tag
publish: ## release to pypi and github tag
@poetry publish --build -u lsbardel -p $(PYPI_PASSWORD)
21 changes: 10 additions & 11 deletions ccy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Python currencies"""

__version__ = "1.3.0"
__version__ = "1.4.0"


from .core.country import (
Expand All @@ -21,7 +21,7 @@
currencydb,
dump_currency_table,
)
from .core.daycounter import ActActYears, alldc, getdc
from .core.daycounter import alldc, getdc
from .dates.converters import (
date2juldate,
date2timestamp,
Expand All @@ -44,7 +44,6 @@
"dump_currency_table",
#
"getdc",
"ActActYears",
"alldc",
#
"country",
Expand Down Expand Up @@ -81,18 +80,18 @@ def crossover(code):
return currency(code).as_cross("/")


def all():
return currencydb().keys()
def all() -> tuple[str, ...]:
return tuple(currencydb())


def g7():
return ["EUR", "GBP", "USD", "CAD"]
def g7() -> tuple[str, ...]:
return ("EUR", "GBP", "USD", "CAD")


def g10():
return g7() + ["CHF", "SEK", "JPY"]
def g10() -> tuple[str, ...]:
return g7() + ("CHF", "SEK", "JPY")


def g10m():
def g10m() -> tuple[str, ...]:
"""modified g10 = G10 + AUD, NZD, NOK"""
return g10() + ["AUD", "NZD", "NOK"]
return g10() + ("AUD", "NZD", "NOK")
37 changes: 24 additions & 13 deletions ccy/core/country.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#
# Requires pytz 2008i or higher
#
from __future__ import annotations

from typing import Protocol

from .currency import currencydb

# Eurozone countries (officially the euro area)
Expand All @@ -18,11 +19,17 @@ def print_eurozone():
print(c)


_countries = None
_countries: dict[str, Country] = {}
_country_ccys = None
_country_maps = {}


class Country(Protocol):
alpha_2: str
name: str
alpha_3: str = ""


class CountryError(Exception):
pass

Expand All @@ -39,19 +46,16 @@ def countryccy(code):
return cdb.get(code, None)


def countries():
def countries() -> dict[str, Country]:
"""
get country dictionar from pytz and add some extra.
get country dictionary from pytz and add some extra.
"""
global _countries
if not _countries:
v = {}
_countries = v
try:
from pytz import country_names
import pycountry

for k, n in country_names.items():
v[k.upper()] = n
_countries = {country.alpha_2: country for country in pycountry.countries}
except Exception:
pass
return _countries
Expand Down Expand Up @@ -106,7 +110,7 @@ def set_country_map(cfrom, cto, name=None, replace=True):
raise CountryError("Country %s not in database" % c)


def set_new_country(code, ccy, name):
def set_new_country(code: str, ccy: str, name: str) -> None:
"""
Add new country code to database
"""
Expand All @@ -118,7 +122,12 @@ def set_new_country(code, ccy, name):
ccy = str(ccy).upper()
if ccy not in ccys:
raise CountryError("Currency %s not in database" % ccy)
cdb[code] = str(name)
# hacky - but best way I could find
cdb[code] = type(cdb["IT"])(
alpha_2=code,
name=name,
official_name=name,
) # type: ignore
cccys = countryccys()
cccys[code] = ccy

Expand All @@ -133,4 +142,6 @@ def country_map(code):


# Add eurozone to list of Countries
set_new_country("EZ", "EUR", "Eurozone")
# lagacy - to remove
set_new_country("EU", "EUR", "Eurozone")

0 comments on commit a0843cd

Please sign in to comment.