Skip to main content

About The Project

PyPI PyPI - Downloads PyPI - License docs

pycountries provides the ISO enums for the standards:

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

pip install pycountries

Usage

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

pip install fastapi
pip install pycountries

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

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 mind, 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

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.

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.

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, 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

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

(back to top)

Methodology

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

(back to top)

Important

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

(back to top)

License

Distributed under the MIT License. See LICENSE.md for more information.

(back to top)

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:

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

Best regards,

Ivan Koldakov

Visitor counter

Metadata

Release files for pycountries 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycountries 1.2.1
File Size Uploaded
pycountries-1.2.1.tar.gz 45.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycountries 1.2.1
File Interpreter ABI Platform
pycountries-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 87.0 kB

Release files / pycountries-1.2.1.tar.gz

Download URL pycountries-1.2.1.tar.gz
Size 45.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2c885528a9b0efe631c5e70959453a79febcf00063676c4781c460c45bfd55e6
BLAKE2b-256 checksum
How to use checksums
0e9ebb076d792502a5078735620e668def60e6dadaffe160519c84df6163959a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / pycountries-1.2.1-py3-none-any.whl

Download URL pycountries-1.2.1-py3-none-any.whl
Size 41.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb9d530ed1bc75a0ae7fafe8a6d069c672643636a0217fc81b0aa60016c6aeb0
BLAKE2b-256 checksum
How to use checksums
1fa0ffcad3bda64e23a2f348c2166bd80d5f8cef7a2918b0c82399385b00c89b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

1 release file

0.0.15

1 release file

0.0.14

1 release file

0.0.13

1 release file

0.0.12

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page