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.
Ensure you have Python 3.11 or higher installed.
Install the package using pip:
pip install phzipcodesimport phzipcodeszip_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)')location_info = phzipcodes.find_by_city_municipality("Gen. Mariano Alvarez")
print(location_details)
# Output (list): [{'zip_code': '4117', 'province': 'Cavite', 'region': 'Region 4A (CALABARZON)'}]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)'))results = phzipcodes.search(
"Dasmariñas",
fields=["city_municipality"],
match_type=phzipcodes.MatchType.EXACT
)
print(results)regions = phzipcodes.get_regions()
provinces = phzipcodes.get_provinces("Region 4A (CALABARZON)")
cities = phzipcodes.get_cities_municipalities("Cavite")class MatchType(str, Enum):
CONTAINS # Match if query exists within field
STARTSWITH # Match if field starts with query
EXACT # Match if field equals query exactlyclass ZipCode(BaseModel):
code: str
city_municipality: str
province: str
region: strdef find_by_zip(zip_code: str) -> ZipResultGet location information by zip code.
- Parameters:
zip_code: Zip code to search for.
- Returns:
ZipCode | None- ZipCode object or None if not found.
def find_by_city_municipality(city_municipality: str) -> CityMunicipalityResultsGet 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.
def search(
query: str,
fields: Sequence[str] = DEFAULT_SEARCH_FIELDS,
match_type: MatchType = MatchType.CONTAINS
) -> SearchResultsSearch for zip codes based on query and criteria.
- Parameters:
query: Search termfields: 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.
def get_regions() -> RegionsGet all unique regions in the Philippines.
- Returns:
Regions: A list of unique regions.
def get_provinces(region: str) -> ProvincesGet all provinces within a specific region.
- Parameters:
region: str - Region to get provinces for
- Returns:
Provinces: A list of provinces in the specified region
def get_cities_municipalities(province: str) -> CitiesMunicipalitiesGet 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
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).
-
Clone the repository
git clone https://github.com/jayson-panganiban/phzipcodes.git cd phzipcodes -
Install Poetry if you haven't already
curl -sSL https://install.python-poetry.org | python3 - -
Install dependencies
poetry install
Or using pip:
pip install -r requirements.txt
-
Run Tests
poetry run pytest
-
Run linter
poetry run ruff check . -
Run formatter
poetry run ruff format . -
Run type checker
poetry run mypy phzipcodes
-
To update the zip codes data, run the scraper
poetry run python phzipcodes/scraper.py
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.