Skip to main content

A Python package for Philippines zip codes

Project description

phzipcodes

Philippines zip codes package

Installation

Ensure you have Python 3.11 or higher installed.

Install the package using pip:

pip install phzipcodes

Usage

import phzipcodes

# Get zip code information by zip code
zip_info = phzipcodes.find_by_zip("4117")
print(zip_info)
# Output: ZipCode(code='4117', city_municipality='Gen. Mariano Alvarez', province='Cavite', region='Region 4A (CALABARZON)')

# Get location details by city/municipality
location_details = phzipcodes.find_by_city_municipality("Gen. Mariano Alvarez")
print(location_details)
# Output: [{'zip_code': '4117', 'province': 'Cavite', 'region': 'Region 4A (CALABARZON)'}]

# Basic search for zip codes
results = phzipcodes.search("Manila")
for result in results:
    print(result)

# Advanced search with specific field and exact matching
results = phzipcodes.search(
    "Dasmariñas", 
    fields=["city_municipality"], 
    match_type="exact"
)
print(results)
# Output: (ZipCode(code='4114', city_municipality='Dasmariñas', province='Cavite', region='Region 4A (CALABARZON)'),)

# Get all unique regions in the Philippines
regions = phzipcodes.get_regions()
print(regions[:2])
# Output: ['CAR (Cordillera Administrative Region)', 'NCR (National Capital Region)']

# Get all provinces in a specific region
provinces = phzipcodes.get_provinces("Region 4A (CALABARZON)")
print(provinces[:2])
# Output: ['Batangas', 'Cavite']

# Get all cities/municipalities in a specific province
cities_municipalities = phzipcodes.get_cities_municipalities("Cavite")
print(cities_municipalities[:2])
# Output: ['Alfonso', 'Amadeo']

API Reference

find_by_zip(zip_code: str) -> ZipCode | None

Get location information by zip code.

  • Parameters:
    • zip_code: str - The zip code to look up
  • Returns: ZipCode | None - ZipCode object if found, None otherwise

find_by_city_municipality(city_municipality: str) -> list[dict[str, str]]

Get zip codes, province and region by city/municipality name.

  • Parameters:
    • city_municipality: str - Name of the city/municipality to look up
  • Returns: list[dict[str, str]] - List of dictionaries containing zip_code, province and region

search(query: str, fields: Sequence[str] = DEFAULT_SEARCH_FIELDS, match_type: str = MatchType.CONTAINS) -> tuple[ZipCode, ...]

Search for zip codes based on query and criteria.

  • Parameters:
    • query: str - The search query
    • fields: Sequence[str] (optional) - Fields to search in (default: city_municipality, province, region)
    • match_type: str (optional) - Type of match to perform (default: "contains")
  • Returns: tuple[ZipCode, ...] - Tuple of matching ZipCode objects

get_regions() -> list[str]

Get all unique regions in the Philippines.

  • Returns: list[str] - A list of all unique regions

get_provinces(region: str) -> list[str]

Get all provinces within a specific region.

  • Parameters:
    • region: str - Region to get provinces for
  • Returns: list[str] - A list of provinces in the specified region

get_cities_municipalities(province: str) -> list[str]

Get all cities and municipalities within a specific province.

  • Parameters:
    • province: str - Province to get cities/municipalities for
  • Returns: list[str] - A list of cities/municipalities in the specified province

Data Structure

The package uses a ZipCode class with the following attributes:

class ZipCode(BaseModel):
    code: str
    city_municipality: str
    province: str
    region: str

Data Source and Collection

The zip code data used in this package is sourced from PHLPost (Philippine Postal Corporation), the official postal service of the Philippines.

To keep data current, use custom scraper tool (scraper.py).

Development

  1. Clone the repository

    git clone https://github.com/jayson-panganiban/phzipcodes.git
    cd phzipcodes
    
  2. Install Poetry if you haven't already

    curl -sSL https://install.python-poetry.org | python3 -
    
  3. Install dependencies

    poetry install
    

    Or using pip:

    pip install -r requirements.txt
    
  4. Run Tests

    poetry run pytest
    
  5. Run linter

    poetry run ruff check .
    
  6. Run formatter

    poetry run ruff format .
    
  7. Run type checker

    poetry run mypy phzipcodes
    
  8. To update the zip codes data, run the scraper

    poetry run python phzipcodes/scraper.py
    

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

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

phzipcodes-0.1.5.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

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

phzipcodes-0.1.5-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file phzipcodes-0.1.5.tar.gz.

File metadata

  • Download URL: phzipcodes-0.1.5.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0

File hashes

Hashes for phzipcodes-0.1.5.tar.gz
Algorithm Hash digest
SHA256 7e6a397091d6bad33f75745686de56bc82030f43a82a2b79ef24b930903231cc
MD5 8f0839e8d32468349dcd235d8ed02bca
BLAKE2b-256 e233148994b6de2aa001139859263c6735eaed3503182e1f6e8bf132188e6e18

See more details on using hashes here.

File details

Details for the file phzipcodes-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: phzipcodes-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0

File hashes

Hashes for phzipcodes-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4182a85d52414d61b0d69945ac967c5b7144662943b43422ffc090a1116e5da6
MD5 d56b63f4f27638000a30ecddef9a5023
BLAKE2b-256 e36a5a59c0b068c9d8bd9ae6711222417b604d878d1144ad7588529297e56563

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