GithubHelp home page GithubHelp logo

atem18 / isocodes Goto Github PK

View Code? Open in Web Editor NEW
13.0 13.0 4.0 13.37 MB

This project provides lists of various ISO standards (e.g. country, language, language scripts, and currency names) in one place

License: GNU Lesser General Public License v2.1

Python 96.98% Shell 3.02%
countries country i18n internationalization translation

isocodes's Introduction

Hi there πŸ‘‹

isocodes's People

Contributors

atem18 avatar dependabot[bot] avatar lordwelch avatar mordae avatar pre-commit-ci[bot] avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

isocodes's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency hatchling to v1.12.2
  • chore(deps): update dependency httpx to v0.23.3
  • chore(deps): update dependency rich to v13.0.1
  • chore(deps): update actions/checkout action to v3.3.0
  • chore(deps): update dependency iniconfig to v2
  • chore(deps): update dependency pyflakes to v3
  • chore(deps): update dependency rfc3986 to v2
  • πŸ” Create all rate-limited PRs at once πŸ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/publish-test.yaml
  • actions/checkout v3.2.0
  • actions/setup-python v4
  • pypa/gh-action-pypi-publish v1.6.4
.github/workflows/publish.yaml
  • actions/checkout v3.2.0
  • actions/setup-python v4
  • pypa/gh-action-pypi-publish v1.6.4
.github/workflows/test.yaml
  • actions/checkout v3.2.0
  • actions/setup-python v4
pip_requirements
dev-requirements.txt
  • anyio ==3.6.2
  • attrs ==22.2.0
  • black ==22.12.0
  • certifi ==2022.12.7
  • click ==8.1.3
  • commonmark ==0.9.1
  • distlib ==0.3.6
  • editables ==0.3
  • exceptiongroup ==1.1.0
  • filelock ==3.9.0
  • flake8 ==5.0.4
  • ghp-import ==2.1.0
  • h11 ==0.14.0
  • hatch ==1.6.3
  • hatchling ==1.12.1
  • httpcore ==0.16.3
  • httpx ==0.23.2
  • hyperlink ==21.0.0
  • idna ==3.4
  • importlib-metadata ==4.2.0
  • iniconfig ==1.1.1
  • jaraco-classes ==3.2.3
  • jinja2 ==3.0.3
  • keyring ==23.9.3
  • markdown ==3.3.4
  • markupsafe ==2.1.1
  • mccabe ==0.7.0
  • mergedeep ==1.3.4
  • mkdocs ==1.2.4
  • mkdocs-autorefs ==0.4.1
  • mkdocs-material ==8.2.7
  • mkdocs-material-extensions ==1.1.1
  • mkdocstrings ==0.19.1
  • more-itertools ==9.0.0
  • mypy ==0.991
  • mypy-extensions ==0.4.3
  • packaging ==22.0
  • pathspec ==0.10.3
  • pexpect ==4.8.0
  • platformdirs ==2.6.2
  • pluggy ==1.0.0
  • ptyprocess ==0.7.0
  • py ==1.11.0
  • pycodestyle ==2.9.1
  • pyflakes ==2.5.0
  • pygments ==2.14.0
  • pymdown-extensions ==9.9
  • pyperclip ==1.8.2
  • pytest ==7.2.0
  • python-dateutil ==2.8.2
  • pyyaml ==6.0
  • pyyaml-env-tag ==0.1
  • rfc3986 ==1.5.0
  • rich ==13.0.0
  • shellingham ==1.5.0
  • six ==1.16.0
  • sniffio ==1.3.0
  • tomli ==2.0.1
  • tomli-w ==1.0.0
  • tomlkit ==0.11.6
  • tox ==3.28.0
  • typed-ast ==1.5.4
  • typing-extensions ==4.4.0
  • userpath ==1.8.0
  • virtualenv ==20.16.2
  • watchdog ==2.2.1
  • zipp ==3.11.0

  • Check this box to trigger a request for Renovate to run again on this repository

Please let lookup by common name

Example:

>>> from isocodes import countries
>>> countries.get(alpha_2='VE')
{'alpha_2': 'VE', 'alpha_3': 'VEN', 'common_name': 'Venezuela', 'flag': 'πŸ‡»πŸ‡ͺ', 'name': 'Venezuela, Bolivarian Republic of', 'numeric': '862', 'official_name': 'Bolivarian Republic of Venezuela'}

>>> countries.get(common_name='Venezuela')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/quan/Works/Envs/vieperid/lib/python3.10/site-packages/isocodes/__init__.py", line 30, in get
    return [element for element in self.data if element[key] == kwargs[key]][0]
  File "/home/quan/Works/Envs/vieperid/lib/python3.10/site-packages/isocodes/__init__.py", line 30, in <listcomp>
    return [element for element in self.data if element[key] == kwargs[key]][0]
KeyError: 'common_name'

'common_name'

isocodes.languages.get(name="Spanish") is {}

Thank you for pushing pycountry into the future!! I have a bug to report.

Using latest isocodes, Python 3.11.4

import isocodes
assert isocodes.languages.get(name="Spanish")

^ the assert fails.

Direct string matches are a pain πŸ˜…
Here's the line that shows the data has "Spanish; ..." rather than "Spanish" alone. Something like iso639's match functionality would supercharge this library.

Use importlib.resources for python >=3.9

Currently isocodes does not work with PyInstaller 6.2.0.
importlib.resources is available for Python >=3.9 for Python 3.8 isocodes can either depend on the importlib_metadata>=1.3 backport package or fall back to file which does work with PyInstaller.
Using importlib.resources would also make isocodes compatible with more exotic python loaders with minimal effort https://pyoxidizer.readthedocs.io/en/v0.6.0/packaging_pitfalls.html (yes pyoxidizer is on 0.23.0 and now supports the __file__ attribute for loading filesystem resources, but the information is still valid, later documentation splits it up into several different pages)

I can open a pull request if you would like

An example of how this could be implemented:

def get_resource(resource: str) -> 'importlib.resources.abc.Traversable':
    """Return a file handle on a named resource in a Package."""

    # Attempt impotlib.resources
    if sys.version_info >= (3, 9):
        import importlib.resources
        return importlib.resources.files('isocodes').joinpath(resource)

    # Attempt importlib_resources backport
    try:
        if sys.version_info < (3, 9):
            import importlib_resources
            return importlib_resources.files('isocodes').joinpath(resource)
    except ImportError:
        ...

    # Fall back to __file__.

    # Undefined __file__ will raise NameError on variable access.
    try:
        package_path = os.path.abspath(os.path.dirname(__file__))
    except NameError:
        package_path = None

    if package_path is not None:
        resource_path = os.path.join(package_path, resource)

        return pathlib.Path(resource_path)

    # Could not resolve package path from __file__.
    raise Exception(f'do not know how to load resource: {resource}')


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

    def __init__(self, iso_key: str) -> None:
        self.iso_key = iso_key
        with get_resource(
            f"share/iso-codes/json/iso_{self.iso_key}.json"
        ).open(encoding="utf-8") as iso_file:
            self.data = json.load(iso_file)[self.iso_key]

Here is a reproducible example

$ pyinstaller main.py --collect-data isocodes
$ dist/main/main
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import isocodes
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "isocodes/__init__.py", line 317, in <module>
  File "isocodes/__init__.py", line 92, in __init__
FileNotFoundError: [Errno 2] No such file or directory: '/home/lordwelch/isocodes/dist/main/share/iso-codes/json/iso_3166-1.json'
[28509] Failed to execute script 'main' due to unhandled exception!

main.py:

import isocodes
print('success')

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 690: character maps to <undefined>

Details

isocodes package doesn’t use encoding="utf-8" to read the json files in the ISO.__init.py which is causing issues on Windows as the default Python encoding on Windows is not UTF-8.


Error Stacktrace

C:\Anaconda3\lib\site-packages\isocodes\__init__.py in <module>
    217 languages = Languages("639-2")
    218 currencies = Currencies("4217")
--> 219 subdivisions_countries = SubdivisionsCountries("3166-2")
    220 former_countries = FormerCountries("3166-3")
    221 extendend_languages = ExtendedLanguages("639-3")

C:\Anaconda3\lib\site-packages\isocodes\__init__.py in __init__(self, iso_key)
     27             f"{BASE_DIR}/share/iso-codes/json/iso_{self.iso_key}.json"
     28         ) as iso_file:
---> 29             self.data: List[Dict] = json.load(iso_file)[self.iso_key]
     30 
     31     def __len__(self) -> int:

C:\Anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    291     kwarg; otherwise ``JSONDecoder`` is used.
    292     """
--> 293     return loads(fp.read(),
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,

C:\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

Suggested fix

class ISO:
    def __init__(self, iso_key: str) -> None:
        self.iso_key: str = iso_key
        with open(
            f"{BASE_DIR}/share/iso-codes/json/iso_{self.iso_key}.json", encoding="utf-8"
        ) as iso_file:
            self.data: List[Dict] = json.load(iso_file)[self.iso_key]`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.