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 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.
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
-
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 installOr 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
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1d0e96c67488ba6b9201032257f71b1d95a72d1511f5b1023eeff7962b280da
|
|
| MD5 |
937a34fefb304137685859b06e17d436
|
|
| BLAKE2b-256 |
a1ae67475e3c6912bad9a95610a309f260a4961f677649b41a50713939bd10ab
|
Provenance
The following attestation bundles were made for phzipcodes-0.1.7.tar.gz:
Publisher:
python-publish.yml on jayson-panganiban/phzipcodes
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phzipcodes-0.1.7.tar.gz -
Subject digest:
f1d0e96c67488ba6b9201032257f71b1d95a72d1511f5b1023eeff7962b280da - Sigstore transparency entry: 158636271
- Sigstore integration time:
-
Permalink:
jayson-panganiban/phzipcodes@2835e92773081eb6126ae62a87abfe15bfe68d60 -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/jayson-panganiban
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2835e92773081eb6126ae62a87abfe15bfe68d60 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1563c39acad6574a8fbb49da94536c6226ccc5559ccb1c72d3b7b7705e18bb0
|
|
| MD5 |
832586e7ca2835d3f3e4430869c986b1
|
|
| BLAKE2b-256 |
5287283d67b60e7a3c2e326f9ed79a712d4b5810c35d5b397e0414ad567d3093
|
Provenance
The following attestation bundles were made for phzipcodes-0.1.7-py3-none-any.whl:
Publisher:
python-publish.yml on jayson-panganiban/phzipcodes
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phzipcodes-0.1.7-py3-none-any.whl -
Subject digest:
a1563c39acad6574a8fbb49da94536c6226ccc5559ccb1c72d3b7b7705e18bb0 - Sigstore transparency entry: 158636273
- Sigstore integration time:
-
Permalink:
jayson-panganiban/phzipcodes@2835e92773081eb6126ae62a87abfe15bfe68d60 -
Branch / Tag:
refs/tags/v0.1.7 - Owner: https://github.com/jayson-panganiban
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2835e92773081eb6126ae62a87abfe15bfe68d60 -
Trigger Event:
release
-
Statement type: