Skip to content

Commit

Permalink
Merge pull request #227 from mordae/main
Browse files Browse the repository at this point in the history
Use TypedDict & Fix by_something queries
  • Loading branch information
Atem Serem authored Sep 14, 2023
2 parents 2e997ba + f3f8b60 commit a291f05
Showing 1 changed file with 140 additions and 46 deletions.
186 changes: 140 additions & 46 deletions isocodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,70 @@
import json
import os
import sys
from typing import Dict, Generator, List, Optional, Tuple
from typing import Any, Dict, Generator, List, Optional, Tuple, TypedDict

Record = Dict[str, str]
RecordRow = Tuple[str, Record]
NameRow = Tuple[str, str]

class Country(TypedDict, total=False):
alpha_2: str
alpha_3: str
common_name: str
flag: str
name: str
numeric: str
official_name: str


class Language(TypedDict, total=False):
alpha_2: str
alpha_3: str
bibliographic: str
common_name: str
name: str


class Currency(TypedDict, total=False):
alpha_3: str
name: str
numeric: str


class CountrySubdivision(TypedDict, total=False):
code: str
name: str
parent: str
type: str


class FormerCountry(TypedDict, total=False):
alpha_2: str
alpha_3: str
alpha_4: str
comment: str
name: str
numeric: str
withdrawal_date: str


class ExtendedLanguage(TypedDict, total=False):
alpha_2: str
alpha_3: str
bibliographic: str
common_name: str
inverted_name: str
name: str
scope: str
type: str


class LanguageFamily(TypedDict, total=False):
alpha_3: str
name: str


class ScriptName(TypedDict, total=False):
alpha_4: str
name: str
numeric: str


def get_script_dir(follow_symlinks: bool = True) -> str:
Expand All @@ -26,7 +85,7 @@ def get_script_dir(follow_symlinks: bool = True) -> str:

class ISO:
iso_key: str
data: List[Record]
data: List[Dict[str, str]]

def __init__(self, iso_key: str) -> None:
self.iso_key = iso_key
Expand All @@ -38,13 +97,16 @@ def __init__(self, iso_key: str) -> None:
def __len__(self) -> int:
return len(self.data)

def _name_from_index(self, index: str) -> Generator[NameRow, None, None]:
def _name_from_index(self, index: str) -> Generator[Any, None, None]:
return ((element[index], element["name"]) for element in self.data)

def _sorted_by_index(self, index: str) -> List[RecordRow]:
return sorted((element[index], element) for element in self.data)
def _sorted_by_index(self, index: str) -> List[Tuple[str, Any]]:
return sorted(
[(element[index], element) for element in self.data if index in element],
key=lambda x: x[0],
)

def get(self, **kwargs: str) -> Optional[Record]:
def get(self, **kwargs: str) -> Optional[Dict[str, str]]:
try:
key: str = next(iter(kwargs))
return [
Expand All @@ -56,169 +118,201 @@ def get(self, **kwargs: str) -> Optional[Record]:
return {}

@property
def items(self) -> List[Record]:
def items(self) -> List[Any]:
return self.data


class Countries(ISO):
@property
def by_alpha_2(self) -> List[RecordRow]:
def by_alpha_2(self) -> List[Tuple[str, Country]]:
return self._sorted_by_index(index="alpha_2")

@property
def by_alpha_3(self) -> List[RecordRow]:
def by_alpha_3(self) -> List[Tuple[str, Country]]:
return self._sorted_by_index(index="alpha_3")

@property
def by_common_name(self) -> List[RecordRow]:
def by_common_name(self) -> List[Tuple[str, Country]]:
return self._sorted_by_index(index="common_name")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, Country]]:
return self._sorted_by_index(index="name")

@property
def by_numeric(self) -> List[RecordRow]:
def by_numeric(self) -> List[Tuple[str, Country]]:
return self._sorted_by_index(index="numeric")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_2")

@property
def items(self) -> List[Country]:
return super().items


class Languages(ISO):
@property
def by_alpha_3(self) -> List[RecordRow]:
def by_alpha_3(self) -> List[Tuple[str, Language]]:
return self._sorted_by_index(index="alpha_3")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, Language]]:
return self._sorted_by_index(index="name")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_3")

@property
def items(self) -> List[Language]:
return super().items


class Currencies(ISO):
@property
def by_alpha_3(self) -> List[RecordRow]:
def by_alpha_3(self) -> List[Tuple[str, Currency]]:
return self._sorted_by_index(index="alpha_3")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, Currency]]:
return self._sorted_by_index(index="name")

@property
def by_numeric(self) -> List[RecordRow]:
def by_numeric(self) -> List[Tuple[str, Currency]]:
return self._sorted_by_index(index="numeric")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_3")

@property
def items(self) -> List[Currency]:
return super().items


class SubdivisionsCountries(ISO):
@property
def by_code(self) -> List[RecordRow]:
def by_code(self) -> List[Tuple[str, CountrySubdivision]]:
return self._sorted_by_index(index="code")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, CountrySubdivision]]:
return self._sorted_by_index(index="name")

@property
def by_type(self) -> List[RecordRow]:
def by_type(self) -> List[Tuple[str, CountrySubdivision]]:
return self._sorted_by_index(index="type")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="code")

@property
def items(self) -> List[CountrySubdivision]:
return super().items


class FormerCountries(ISO):
@property
def by_alpha_2(self) -> List[RecordRow]:
def by_alpha_2(self) -> List[Tuple[str, FormerCountry]]:
return self._sorted_by_index(index="alpha_2")

@property
def by_alpha_3(self) -> List[RecordRow]:
def by_alpha_3(self) -> List[Tuple[str, FormerCountry]]:
return self._sorted_by_index(index="alpha_3")

@property
def by_alpha_4(self) -> List[RecordRow]:
def by_alpha_4(self) -> List[Tuple[str, FormerCountry]]:
return self._sorted_by_index(index="alpha_4")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, FormerCountry]]:
return self._sorted_by_index(index="name")

@property
def by_numeric(self) -> List[RecordRow]:
def by_numeric(self) -> List[Tuple[str, FormerCountry]]:
return self._sorted_by_index(index="numeric")

@property
def by_withdrawal_date(self) -> List[RecordRow]:
def by_withdrawal_date(self) -> List[Tuple[str, FormerCountry]]:
return self._sorted_by_index(index="withdrawal_date")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_2")

@property
def items(self) -> List[FormerCountry]:
return super().items


class ExtendedLanguages(ISO):
@property
def by_alpha_3(self) -> List[RecordRow]:
def by_alpha_3(self) -> List[Tuple[str, ExtendedLanguage]]:
return self._sorted_by_index(index="alpha_3")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, ExtendedLanguage]]:
return self._sorted_by_index(index="name")

@property
def by_scope(self) -> List[RecordRow]:
def by_scope(self) -> List[Tuple[str, ExtendedLanguage]]:
return self._sorted_by_index(index="scope")

@property
def by_type(self) -> List[RecordRow]:
def by_type(self) -> List[Tuple[str, ExtendedLanguage]]:
return self._sorted_by_index(index="type")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_3")

@property
def items(self) -> List[ExtendedLanguage]:
return super().items


class LanguageFamilies(ISO):
@property
def by_alpha_3(self) -> List[RecordRow]:
def by_alpha_3(self) -> List[Tuple[str, LanguageFamily]]:
return self._sorted_by_index(index="alpha_3")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, LanguageFamily]]:
return self._sorted_by_index(index="name")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_3")

@property
def items(self) -> List[LanguageFamily]:
return super().items


class ScriptNames(ISO):
@property
def by_alpha_4(self) -> List[RecordRow]:
def by_alpha_4(self) -> List[Tuple[str, ScriptName]]:
return self._sorted_by_index(index="alpha_4")

@property
def by_name(self) -> List[RecordRow]:
def by_name(self) -> List[Tuple[str, ScriptName]]:
return self._sorted_by_index(index="name")

@property
def by_numeric(self) -> List[RecordRow]:
def by_numeric(self) -> List[Tuple[str, ScriptName]]:
return self._sorted_by_index(index="numeric")

@property
def name(self) -> Generator[NameRow, None, None]:
def name(self) -> Generator[Tuple[str, str], None, None]:
return self._name_from_index(index="alpha_4")

@property
def items(self) -> List[ScriptName]:
return super().items


countries = Countries("3166-1")
languages = Languages("639-2")
Expand Down

0 comments on commit a291f05

Please sign in to comment.