pycountries


Namepycountries JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
SummaryList of existing countries and currencies
upload_time2024-04-08 19:24:21
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords pydantic fastapi countries currencies
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # About The Project
[![PyPI](https://img.shields.io/pypi/v/pycountries?logo=python&logoColor=white)][pypi_proj]
[![PyPI - Downloads](https://img.shields.io/pypi/dm/pycountries?logo=python&logoColor=white)][pypi_proj]
[![PyPI - License](https://img.shields.io/pypi/l/pycountries?logo=open-source-initiative&logoColor=white)](https://github.com/AivGitHub/pycountries/blob/main/LICENSE.md)
[![docs](https://img.shields.io/readthedocs/pycountries?logo=readthedocs&logoColor=white)][documentation]

pycountries provides the ISO enums for the standards:

- [iso-4217](https://www.iso.org/iso-4217-currency-codes.html)
- [iso-3166](https://www.iso.org/iso-3166-country-codes.html)
- [iso-639](https://www.iso.org/iso-639-language-code)

pycountries can be used natively with pydantic >= 1.5 and python >= 3.8.
It makes really easy to use fastapi and other related libraries.

## Requirements

1. Python >= 3.8

## Installation

```bash
pip install pycountries
```

## Usage

Examples show how to use pycountries with pydantic + fastapi, but
you can use it with pydantic and any other libraries.

```bash
pip install fastapi
pip install pycountries
```

When you have all required libraries you can quickstart with this code:

```python
from decimal import Decimal

from fastapi import FastAPI
from pycountries import Country, Currency, Language
from pydantic import BaseModel, model_validator


app = FastAPI()


class IndexRequest(BaseModel):
    country: Country
    currency: Currency
    amount: Decimal
    language: Language

    @model_validator(mode="after")
    def validate_amount(self) -> "IndexRequest":
        # TODO: Keep in you need to handle exceptions raised by clean_amount method,
        #  otherwise Internal Server Error will be raised.
        self.amount = self.currency.clean_amount(self.amount)
        return self


class IndexResponse(BaseModel):
    amount: Decimal


@app.post("/")
async def root(data: IndexRequest):
    return IndexResponse(**data.model_dump())
```

### Request Examples

```bash
curl -X POST -H "Content-Type: application/json" -d '{"country":"US", "currency":"USD", "amount":"20.20", "language":"eng"}' http://127.0.0.1:8000
{"amount":"20.20"}
curl -X POST -H "Content-Type: application/json" -d '{"country":"US", "currency":"USD", "amount":"-20.20", "language":"eng"}' http://127.0.0.1:8000
Internal Server Error
```

### Phones

I'm still not sure about the logic, if you have any ideas please create a task
[here](https://github.com/koldakov/pycountries/issues).

The problem here is that calling code is not unique per country. For example,
at least 3 countries have code +1: United States, Canada, Barbados. To determine the country
we need to see a prefix - N numbers after country code. And, looks like these prefixes are quite
dynamic, so should be managed accordingly.

For now logic is:
1. Return first calling code match if prefix is not provided.
2. If prefix is provided return first prefix match, or first "candidate" if there are no exact prefix matches.

It is very useful if you want to show phone dynamically. See an example below for more info.

```python
from pycountries import Phone

# Return first match with different types
Phone(1)  # Phone.BB
Phone("1")  # Phone.BB
Phone("+1")  # Phone.BB

# Result has changed, because results with prefixes in priority right now.
Phone(1, prefix=3)  # Phone.UM
Phone(1, prefix=32)  # Phone.UM
# Exact match found!
Phone(1, prefix=325)  # Phone.US
```

## Motivation

There is a great library [pycountry](https://github.com/pycountry/pycountry), but it is incompatible with enums,
for sure enums can be generated dynamically, but python does not work good with dynamic enums, for example, you can not
inherit from enums, annotations will be broken, etc. pycountries solves these issues and more.
Soon pycountries will be providing other ISO standards related to countries.

## Development

```bash
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

## Contributing

1. Fork the Project
2. Open a Pull Request
3. Or just read here: [contributing](https://docs.github.com/en/get-started/quickstart/contributing-to-projects)

<p align="right">(<a href="#top">back to top</a>)</p>

## Methodology

1. Do a lot, break a lot.
2. There are no difficult tasks, only interesting.
3. Mostly TBD.

<p align="right">(<a href="#top">back to top</a>)</p>

## Important

1. Quality.
2. Security.
3. Google first.

<p align="right">(<a href="#top">back to top</a>)</p>

## License

Distributed under the MIT License. See [LICENSE.md](LICENSE.md) for more information.

<p align="right">(<a href="#top">back to top</a>)</p>

## Buy me a coffee if you want to support me

https://www.buymeacoffee.com/aivCoffee

## Contact

Hi all,

How are you? Hope You've enjoyed the project.

There are my contacts:

- [Linkedin](https://www.linkedin.com/in/aiv/)
- [Send an Email](mailto:coldie322@gmail.com?subject=[GitHub]-qworpa)

Project Link: https://github.com/koldakov/pycountries

Best regards,

[Ivan Koldakov](https://www.linkedin.com/in/aiv/)


[pypi_proj]: https://pypi.org/project/pycountries/
[documentation]: https://pycountries.readthedocs.io

## Visitor counter

<img src="https://profile-counter.glitch.me/pycountries/count.svg" />

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pycountries",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "pydantic, fastapi, countries, currencies",
    "author": null,
    "author_email": "Ivan Koldakov <ivan@koldakov.com>",
    "download_url": "https://files.pythonhosted.org/packages/1b/f7/68fc9ce2b256c4dd6ed9c3ec676fe0c1f04ac64230a6dae7f4468da42fdb/pycountries-1.0.1.tar.gz",
    "platform": null,
    "description": "# About The Project\n[![PyPI](https://img.shields.io/pypi/v/pycountries?logo=python&logoColor=white)][pypi_proj]\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/pycountries?logo=python&logoColor=white)][pypi_proj]\n[![PyPI - License](https://img.shields.io/pypi/l/pycountries?logo=open-source-initiative&logoColor=white)](https://github.com/AivGitHub/pycountries/blob/main/LICENSE.md)\n[![docs](https://img.shields.io/readthedocs/pycountries?logo=readthedocs&logoColor=white)][documentation]\n\npycountries provides the ISO enums for the standards:\n\n- [iso-4217](https://www.iso.org/iso-4217-currency-codes.html)\n- [iso-3166](https://www.iso.org/iso-3166-country-codes.html)\n- [iso-639](https://www.iso.org/iso-639-language-code)\n\npycountries can be used natively with pydantic >= 1.5 and python >= 3.8.\nIt makes really easy to use fastapi and other related libraries.\n\n## Requirements\n\n1. Python >= 3.8\n\n## Installation\n\n```bash\npip install pycountries\n```\n\n## Usage\n\nExamples show how to use pycountries with pydantic + fastapi, but\nyou can use it with pydantic and any other libraries.\n\n```bash\npip install fastapi\npip install pycountries\n```\n\nWhen you have all required libraries you can quickstart with this code:\n\n```python\nfrom decimal import Decimal\n\nfrom fastapi import FastAPI\nfrom pycountries import Country, Currency, Language\nfrom pydantic import BaseModel, model_validator\n\n\napp = FastAPI()\n\n\nclass IndexRequest(BaseModel):\n    country: Country\n    currency: Currency\n    amount: Decimal\n    language: Language\n\n    @model_validator(mode=\"after\")\n    def validate_amount(self) -> \"IndexRequest\":\n        # TODO: Keep in you need to handle exceptions raised by clean_amount method,\n        #  otherwise Internal Server Error will be raised.\n        self.amount = self.currency.clean_amount(self.amount)\n        return self\n\n\nclass IndexResponse(BaseModel):\n    amount: Decimal\n\n\n@app.post(\"/\")\nasync def root(data: IndexRequest):\n    return IndexResponse(**data.model_dump())\n```\n\n### Request Examples\n\n```bash\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"country\":\"US\", \"currency\":\"USD\", \"amount\":\"20.20\", \"language\":\"eng\"}' http://127.0.0.1:8000\n{\"amount\":\"20.20\"}\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"country\":\"US\", \"currency\":\"USD\", \"amount\":\"-20.20\", \"language\":\"eng\"}' http://127.0.0.1:8000\nInternal Server Error\n```\n\n### Phones\n\nI'm still not sure about the logic, if you have any ideas please create a task\n[here](https://github.com/koldakov/pycountries/issues).\n\nThe problem here is that calling code is not unique per country. For example,\nat least 3 countries have code +1: United States, Canada, Barbados. To determine the country\nwe need to see a prefix - N numbers after country code. And, looks like these prefixes are quite\ndynamic, so should be managed accordingly.\n\nFor now logic is:\n1. Return first calling code match if prefix is not provided.\n2. If prefix is provided return first prefix match, or first \"candidate\" if there are no exact prefix matches.\n\nIt is very useful if you want to show phone dynamically. See an example below for more info.\n\n```python\nfrom pycountries import Phone\n\n# Return first match with different types\nPhone(1)  # Phone.BB\nPhone(\"1\")  # Phone.BB\nPhone(\"+1\")  # Phone.BB\n\n# Result has changed, because results with prefixes in priority right now.\nPhone(1, prefix=3)  # Phone.UM\nPhone(1, prefix=32)  # Phone.UM\n# Exact match found!\nPhone(1, prefix=325)  # Phone.US\n```\n\n## Motivation\n\nThere is a great library [pycountry](https://github.com/pycountry/pycountry), but it is incompatible with enums,\nfor sure enums can be generated dynamically, but python does not work good with dynamic enums, for example, you can not\ninherit from enums, annotations will be broken, etc. pycountries solves these issues and more.\nSoon pycountries will be providing other ISO standards related to countries.\n\n## Development\n\n```bash\npython3.12 -m venv .venv\nsource .venv/bin/activate\npip install -r requirements.txt\n```\n\n## Contributing\n\n1. Fork the Project\n2. Open a Pull Request\n3. Or just read here: [contributing](https://docs.github.com/en/get-started/quickstart/contributing-to-projects)\n\n<p align=\"right\">(<a href=\"#top\">back to top</a>)</p>\n\n## Methodology\n\n1. Do a lot, break a lot.\n2. There are no difficult tasks, only interesting.\n3. Mostly TBD.\n\n<p align=\"right\">(<a href=\"#top\">back to top</a>)</p>\n\n## Important\n\n1. Quality.\n2. Security.\n3. Google first.\n\n<p align=\"right\">(<a href=\"#top\">back to top</a>)</p>\n\n## License\n\nDistributed under the MIT License. See [LICENSE.md](LICENSE.md) for more information.\n\n<p align=\"right\">(<a href=\"#top\">back to top</a>)</p>\n\n## Buy me a coffee if you want to support me\n\nhttps://www.buymeacoffee.com/aivCoffee\n\n## Contact\n\nHi all,\n\nHow are you? Hope You've enjoyed the project.\n\nThere are my contacts:\n\n- [Linkedin](https://www.linkedin.com/in/aiv/)\n- [Send an Email](mailto:coldie322@gmail.com?subject=[GitHub]-qworpa)\n\nProject Link: https://github.com/koldakov/pycountries\n\nBest regards,\n\n[Ivan Koldakov](https://www.linkedin.com/in/aiv/)\n\n\n[pypi_proj]: https://pypi.org/project/pycountries/\n[documentation]: https://pycountries.readthedocs.io\n\n## Visitor counter\n\n<img src=\"https://profile-counter.glitch.me/pycountries/count.svg\" />\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "List of existing countries and currencies",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/koldakov/pycountries",
        "Issues": "https://github.com/koldakov/pycountries/issues"
    },
    "split_keywords": [
        "pydantic",
        " fastapi",
        " countries",
        " currencies"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b4fded95d9c4afb15d5c355d16ff481d49627526e1adea8331bd39453daf0167",
                "md5": "d75a1e9ee0e16ffa65f9d4f0b3f914c8",
                "sha256": "6320dd18ddcfab98aa266f1488cc1e683396e3c0a8a7938a3b997ad6fb6d469b"
            },
            "downloads": -1,
            "filename": "pycountries-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d75a1e9ee0e16ffa65f9d4f0b3f914c8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 37854,
            "upload_time": "2024-04-08T19:24:19",
            "upload_time_iso_8601": "2024-04-08T19:24:19.700581Z",
            "url": "https://files.pythonhosted.org/packages/b4/fd/ed95d9c4afb15d5c355d16ff481d49627526e1adea8331bd39453daf0167/pycountries-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1bf768fc9ce2b256c4dd6ed9c3ec676fe0c1f04ac64230a6dae7f4468da42fdb",
                "md5": "1a588fd6f251cc0880845a40a65c1329",
                "sha256": "72b0cdab77487149129d9732aebe452b2b8c08260fcf5a6729633a2c23f71e43"
            },
            "downloads": -1,
            "filename": "pycountries-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1a588fd6f251cc0880845a40a65c1329",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 40829,
            "upload_time": "2024-04-08T19:24:21",
            "upload_time_iso_8601": "2024-04-08T19:24:21.523258Z",
            "url": "https://files.pythonhosted.org/packages/1b/f7/68fc9ce2b256c4dd6ed9c3ec676fe0c1f04ac64230a6dae7f4468da42fdb/pycountries-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-08 19:24:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "koldakov",
    "github_project": "pycountries",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "pycountries"
}
        
Elapsed time: 0.29045s