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
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)'}]

# Search with specific match type
results = phzipcodes.search("Manila", match_type=phzipcodes.MatchType.CONTAINS)
for result in results:
    print(result)

# Search with custom fields and exact matching
results = phzipcodes.search(
    "Dasmariñas", 
    fields=["city_municipality"], 
    match_type=phzipcodes.MatchType.EXACT
)
print(results)

# Get geographic data
regions = phzipcodes.get_regions()
provinces = phzipcodes.get_provinces("Region 4A (CALABARZON)")
cities = phzipcodes.get_cities_municipalities("Cavite")

API Reference

Types

MatchType

class MatchType(str, Enum):
    CONTAINS    # Match if query exists within field
    STARTSWITH  # Match if field starts with query
    EXACT       # Match if field equals query exactly

ZipCode

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

Functions

find_by_zip

def find_by_zip(zip_code: str) -> ZipResult

Get location information by zip code.

  • Parameters:
    • zip_code: Zip code to search for.
  • Returns:
    • ZipCode | None - ZipCode object or None if not found.

find_by_city_municipality

def find_by_city_municipality(city_municipality: str) -> CityMunicipalityResults

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

  • Parameters:
    • city_municipality: city or municipality name.
  • Returns:
    • CityMunicipalityResults: List of dictionaries with zip code, province, and region.

search

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

Search for zip codes based on query and criteria.

  • Parameters:
    • query: Search term
    • fields: Fields to search in (default: city_municipality, province, region)
    • match_type: Type of match to perform (default: CONTAINS)
  • Returns:
    • SearchResults: A tuple of ZipCode objects matching the query.

get_regions

def get_regions() -> Regions

Get all unique regions in the Philippines.

  • Returns: Regions: A list of unique regions.

get_provinces

def get_provinces(region: str) -> Provinces

Get all provinces within a specific region.

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

get_cities_municipalities

def get_cities_municipalities(province: str) -> CitiesMunicipalities

Get all cities/municipalities within a specific province.

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

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.7.tar.gz (21.3 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.7-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: phzipcodes-0.1.7.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for phzipcodes-0.1.7.tar.gz
Algorithm Hash digest
SHA256 f1d0e96c67488ba6b9201032257f71b1d95a72d1511f5b1023eeff7962b280da
MD5 937a34fefb304137685859b06e17d436
BLAKE2b-256 a1ae67475e3c6912bad9a95610a309f260a4961f677649b41a50713939bd10ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for phzipcodes-0.1.7.tar.gz:

Publisher: python-publish.yml on jayson-panganiban/phzipcodes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: phzipcodes-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for phzipcodes-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a1563c39acad6574a8fbb49da94536c6226ccc5559ccb1c72d3b7b7705e18bb0
MD5 832586e7ca2835d3f3e4430869c986b1
BLAKE2b-256 5287283d67b60e7a3c2e326f9ed79a712d4b5810c35d5b397e0414ad567d3093

See more details on using hashes here.

Provenance

The following attestation bundles were made for phzipcodes-0.1.7-py3-none-any.whl:

Publisher: python-publish.yml on jayson-panganiban/phzipcodes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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