Skip to main content

phzipcodes

PyPI version

phzipcodes is a Python package for accessing and searching Philippine zip codes. It provides functionalities to find zip codes by city, search with specific match types, and retrieve regions and provinces. This package is designed to be simple and easy to use, making it a valuable tool for developers working with Philippine address data.

Table of Contents

Installation

Ensure you have Python 3.11 or higher installed.

Install the package using pip:

pip install phzipcodes

Usage

import phzipcodes

1) Find by Zip Code

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

2) Find by City/Municipality

location_info = phzipcodes.find_by_city_municipality("Gen. Mariano Alvarez")
print(location_details)
# Output (list): [{'zip_code': '4117', 'province': 'Cavite', 'region': 'Region 4A (CALABARZON)'}]

3) Search with Match Type

results = phzipcodes.search("Silang", match_type=phzipcodes.MatchType.CONTAINS)
# Check API Reference for MatchTypes (CONTAINS, STARTSWITH, EXACT)

# Output (tuple): 
# (ZipCode(code='1119', city_municipality='Bagong Silangan', province='Metro Manila', region='NCR (National Capital Region)'), 
# ZipCode(code='1428', city_municipality='Bagong Silang', province='Metro Manila', region='NCR (National Capital Region)'), 
# ZipCode(code='4118', city_municipality='Silang', province='Cavite', region='Region 4A (CALABARZON)'))

4) Search in Specific Fields

results = phzipcodes.search(
    "Dasmariñas", 
    fields=["city_municipality"], 
    match_type=phzipcodes.MatchType.EXACT
)
print(results)

5) Get Regions, Provinces, Cities/Municipalities

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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for phzipcodes 0.1.8

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

Source distribution (sdist)

Source distribution for phzipcodes 0.1.8
File Size Uploaded
phzipcodes-0.1.8.tar.gz 22.1 kB Details

Built distribution (wheel)

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

Total release size: 43.9 kB

Release files / phzipcodes-0.1.8.tar.gz

Download URL phzipcodes-0.1.8.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
be9aa20c83f20f787ec4ce07ac2505d0d3b27da9358bab944b54b10db95e78e9
BLAKE2b-256 checksum
How to use checksums
420acf60129f465fa1709ccc29c9f656e877763b0140c721b7f5bb6e78faf875
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 12, 2025.

Transparency log

Release files / phzipcodes-0.1.8-py3-none-any.whl

Download URL phzipcodes-0.1.8-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b795740b6bab51cb827e43467810d93873c4b0a2106a58fe40c0ba8a27941293
BLAKE2b-256 checksum
How to use checksums
d314b8f0fe28d19474adfba1ce40279ff428de484356a0df166c847bc623eaba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 12, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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