Skip to main content

Random addresses from bestrandoms.com with country name and flag

Project description

bestrandom

Python library to get random addresses from bestrandoms.com, with country name and flag for the requested country code. Useful for testing, sample data, or forms.

Installation

From PyPI:

pip install bestrandom

Requirements: Python 3.10+, curl_cffi (installed automatically with bestrandom).

Quick start

from bestrandom import random_address, get_country_info, list_countries

# One random address for a country
addr = random_address("US")
print(addr["street"], addr["city"], addr["state"])
print(addr["country_flag"], addr["country_name"])  # 🇺🇸 United States

# Country info only (name and flag)
info = get_country_info("MX")  # {"country_name": "México", "country_flag": "🇲🇽", ...}

# List all supported countries
for c in list_countries():
    print(c["country_flag"], c["country_name"], c["country_code"])

Public API

random_address(country_code: str) -> dict

Generates a random address for the given country by calling bestrandoms.com and adds country name and flag from the internal catalog.

Parameter Type Description
country_code str Country code (e.g. "US", "MX", "ES"). Case-insensitive.

Returns a dict with:

Key Type Description
street str | None Street
city str | None City
state str | None State / Province / Area
phone_number str | None Phone number
zip str | None Zip / postal code
phone_code str | None Country calling code
country_code str Country code (normalized)
country_name str | None Country name
country_flag str | None Flag emoji (e.g. 🇺🇸)

Example

addr = random_address("es")
# {
#   "street": "Calle Example 123",
#   "city": "Madrid",
#   "state": "Madrid",
#   "phone_number": "...",
#   "zip": "28001",
#   "phone_code": "+34",
#   "country_code": "ES",
#   "country_name": "Spain",
#   "country_flag": "🇪🇸"
# }

If the country is not in the internal catalog, country_name and country_flag will be None; the address is still fetched from bestrandoms.com.

get_country_info(country_code: str) -> dict | None

Returns only country info (name, flag, code) from the internal catalog. No HTTP request is made.

Parameter Type Description
country_code str Country code (case-insensitive).

Returns

  • A dict with country_code, country_name, and country_flag, or
  • None if the code is not in the catalog.

Example

get_country_info("MX")   # {"country_code": "MX", "country_name": "México", "country_flag": "🇲🇽"}
get_country_info("XX")   # None

list_countries() -> list[dict]

Returns the list of all supported countries in the catalog (same source used by get_country_info and random_address for name and flag).

Returns a list of dicts, each with country_code, country_name, and country_flag.

Example

countries = list_countries()
# [{"country_code": "AL", "country_name": "Albania", "country_flag": "🇦🇱"}, ...]

GenerateRandomAddress(country_code: str) -> dict

Alias for random_address(country_code). Kept for backward compatibility.

Supported countries

The catalog includes 120+ countries. Common codes: US, UK, CA, MX, ES, AR, BR, DE, FR, IT, JP, CN, IN, AU, etc. Use list_countries() for the full list.

Country codes in the bestrandoms.com URL can be 2 or 3 letters (e.g. US, BOL). They must match what the site accepts; the internal catalog may have name/flag for that code.

Architecture (Clean Architecture)

The project is organized in layers: domain, application (use cases + ports), and infrastructure.

bestrandom/
├── __init__.py              # Public API and default wiring
├── domain/                  # Domain layer
│   ├── entities.py          # Address, Country entities
│   └── ...
├── application/             # Application layer
│   ├── ports.py             # Interfaces: IAddressProvider, ICountryRepository
│   └── use_cases.py         # GetRandomAddressUseCase, GetCountryInfoUseCase, ListCountriesUseCase
├── infrastructure/         # Infrastructure layer
│   ├── bestrandoms_client.py   # HTTP client → IAddressProvider
│   ├── country_repository.py   # In-memory catalog → ICountryRepository
│   └── html_parser.py          # HTML extraction utility
└── data/                    # Static data
    └── countries.py         # Country list (code, name, flag)
  • Domain: Address and Country entities with no external dependencies.
  • Application: Use cases that depend only on ports (interfaces).
  • Infrastructure: Concrete implementations of those ports (HTTP client, in-memory repository, parser).
  • Public API: __init__.py instantiates adapters and use cases and exposes random_address, get_country_info, list_countries, and GenerateRandomAddress.

You can replace the HTTP client or country repository (e.g. for tests or another data source) without changing the domain or use cases.

Advanced: dependency injection

To use your own address provider or country repository (e.g. for tests or another API), instantiate the use cases yourself:

from bestrandom.application.use_cases import GetRandomAddressUseCase, GetCountryInfoUseCase, ListCountriesUseCase
from bestrandom.infrastructure import BestRandomsAddressProvider, MemoryCountryRepository

# Default implementations
address_provider = BestRandomsAddressProvider()
country_repository = MemoryCountryRepository()

# Injected use cases
get_address = GetRandomAddressUseCase(address_provider, country_repository)
get_country = GetCountryInfoUseCase(country_repository)
list_all = ListCountriesUseCase(country_repository)

# Usage
address = get_address.execute("US")
print(address.to_dict())

country = get_country.execute("MX")
countries = list_all.execute()

You can replace BestRandomsAddressProvider or MemoryCountryRepository with your own implementations that satisfy the protocols in application/ports.py.

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bestrandom-0.1.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bestrandom-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file bestrandom-0.1.0.tar.gz.

File metadata

  • Download URL: bestrandom-0.1.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for bestrandom-0.1.0.tar.gz
Algorithm Hash digest
SHA256 00422be5e668b7490bb4edd87684c4dd0a773846a598a71c1ca83a72f8b8703c
MD5 a44c894ed3a662ccb2e3d8ed40f7bfab
BLAKE2b-256 b56fccb82c834af7b205e131b1ba09b4eaf77f99ca4895a34caccd9b2d65ffed

See more details on using hashes here.

File details

Details for the file bestrandom-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bestrandom-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for bestrandom-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6683c9a47c9b92e3f08a733da1aa285b0e3db489c1b54387ca315529e32a6d9b
MD5 c42231bd502b30f199dbeadce5834bbe
BLAKE2b-256 fe22dd48317b32a04f759c91da62173adc9c8637ab438054afd9a6caad53dff1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page