# phzipcodes
[](https://badge.fury.io/py/phzipcodes)
`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](#installation)
- [Usage](#usage)
- [API Reference](#api-reference)
- [Contributing](#contributing)
- [License](#license)
## Installation
Ensure you have Python 3.11 or higher installed.
Install the package using pip:
```bash
pip install phzipcodes
```
## Usage
```python
import phzipcodes
```
#### 1) Find by Zip Code
```python
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
```python
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
```python
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
```python
results = phzipcodes.search(
"DasmariƱas",
fields=["city_municipality"],
match_type=phzipcodes.MatchType.EXACT
)
print(results)
```
#### 5) Get Regions, Provinces, Cities/Municipalities
```python
regions = phzipcodes.get_regions()
provinces = phzipcodes.get_provinces("Region 4A (CALABARZON)")
cities = phzipcodes.get_cities_municipalities("Cavite")
```
## API Reference
### Types
#### `MatchType`
```python
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`
```python
class ZipCode(BaseModel):
code: str
city_municipality: str
province: str
region: str
```
### Functions
#### `find_by_zip()`
```python
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()`
```python
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()`
```python
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()`
```python
def get_regions() -> Regions
```
Get all unique regions in the Philippines.
- **Returns:** `Regions`: A list of unique regions.
#### `get_provinces()`
```python
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()`
```python
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](https://phlpost.gov.ph/) (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**
```bash
git clone https://github.com/jayson-panganiban/phzipcodes.git
cd phzipcodes
```
2. **Install Poetry if you haven't already**
```bash
curl -sSL https://install.python-poetry.org | python3 -
```
3. **Install dependencies**
```bash
poetry install
```
Or using pip:
```bash
pip install -r requirements.txt
```
4. **Run Tests**
```bash
poetry run pytest
```
5. **Run linter**
```bash
poetry run ruff check .
```
6. **Run formatter**
```bash
poetry run ruff format .
```
7. **Run type checker**
```bash
poetry run mypy phzipcodes
```
8. **To update the zip codes data, run the scraper**
```bash
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](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "phzipcodes",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "philippines, zipcode, zipcodes, postal code",
"author": "Jayson",
"author_email": "jsoncp@proton.me",
"download_url": "https://files.pythonhosted.org/packages/42/0a/cf60129f465fa1709ccc29c9f656e877763b0140c721b7f5bb6e78faf875/phzipcodes-0.1.8.tar.gz",
"platform": null,
"description": "# phzipcodes\n[](https://badge.fury.io/py/phzipcodes)\n\n`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.\n\n## Table of Contents\n- [Installation](#installation)\n- [Usage](#usage)\n- [API Reference](#api-reference)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Installation\n\nEnsure you have Python 3.11 or higher installed.\n\nInstall the package using pip:\n\n```bash\npip install phzipcodes\n```\n\n## Usage\n\n```python\nimport phzipcodes\n```\n\n\n#### 1) Find by Zip Code\n```python\nzip_info = phzipcodes.find_by_zip(\"4117\")\nprint(zip_info)\n# Output (object): ZipCode(code='4117', city_municipality='Gen. Mariano Alvarez', province='Cavite', region='Region 4A (CALABARZON)')\n```\n\n#### 2) Find by City/Municipality\n```python\nlocation_info = phzipcodes.find_by_city_municipality(\"Gen. Mariano Alvarez\")\nprint(location_details)\n# Output (list): [{'zip_code': '4117', 'province': 'Cavite', 'region': 'Region 4A (CALABARZON)'}]\n```\n\n#### 3) Search with Match Type\n```python\nresults = phzipcodes.search(\"Silang\", match_type=phzipcodes.MatchType.CONTAINS)\n# Check API Reference for MatchTypes (CONTAINS, STARTSWITH, EXACT)\n\n# Output (tuple): \n# (ZipCode(code='1119', city_municipality='Bagong Silangan', province='Metro Manila', region='NCR (National Capital Region)'), \n# ZipCode(code='1428', city_municipality='Bagong Silang', province='Metro Manila', region='NCR (National Capital Region)'), \n# ZipCode(code='4118', city_municipality='Silang', province='Cavite', region='Region 4A (CALABARZON)'))\n```\n\n#### 4) Search in Specific Fields\n```python\nresults = phzipcodes.search(\n \"Dasmari\u00f1as\", \n fields=[\"city_municipality\"], \n match_type=phzipcodes.MatchType.EXACT\n)\nprint(results)\n```\n\n#### 5) Get Regions, Provinces, Cities/Municipalities\n```python\nregions = phzipcodes.get_regions()\nprovinces = phzipcodes.get_provinces(\"Region 4A (CALABARZON)\")\ncities = phzipcodes.get_cities_municipalities(\"Cavite\")\n```\n\n## API Reference\n\n### Types\n\n#### `MatchType`\n```python\nclass MatchType(str, Enum):\n CONTAINS # Match if query exists within field\n STARTSWITH # Match if field starts with query\n EXACT # Match if field equals query exactly\n```\n#### `ZipCode`\n```python\nclass ZipCode(BaseModel):\n code: str\n city_municipality: str\n province: str\n region: str\n```\n### Functions\n#### `find_by_zip()`\n```python\ndef find_by_zip(zip_code: str) -> ZipResult\n```\nGet location information by zip code.\n- **Parameters:**\n - `zip_code`: Zip code to search for.\n- **Returns:** \n - `ZipCode | None` - ZipCode object or None if not found.\n\n\n#### `find_by_city_municipality()`\n```python\ndef find_by_city_municipality(city_municipality: str) -> CityMunicipalityResults\n```\nGet zip codes, province and region by city/municipality name.\n\n- **Parameters:**\n - `city_municipality`: city or municipality name.\n- **Returns**: \n - `CityMunicipalityResults`: List of dictionaries with zip code, province, and region.\n\n#### `search()`\n```python\ndef search(\n query: str,\n fields: Sequence[str] = DEFAULT_SEARCH_FIELDS,\n match_type: MatchType = MatchType.CONTAINS\n) -> SearchResults\n```\nSearch for zip codes based on query and criteria.\n- **Parameters:**\n - `query`: Search term\n - `fields`: Fields to search in (default: city_municipality, province, region)\n - `match_type`: Type of match to perform (default: CONTAINS)\n- **Returns:** \n - `SearchResults`: A tuple of ZipCode objects matching the query.\n\n#### `get_regions()`\n```python\ndef get_regions() -> Regions\n```\nGet all unique regions in the Philippines.\n- **Returns:** `Regions`: A list of unique regions.\n\n#### `get_provinces()`\n```python\ndef get_provinces(region: str) -> Provinces\n```\nGet all provinces within a specific region.\n\n- **Parameters:**\n - `region`: str - Region to get provinces for\n- **Returns:**\n - `Provinces`: A list of provinces in the specified region\n\n#### `get_cities_municipalities()`\n```python\ndef get_cities_municipalities(province: str) -> CitiesMunicipalities\n```\nGet all cities/municipalities within a specific province.\n- **Parameters:**\n - `province`: str - Province to get cities/municipalities for\n- **Returns:**\n - `CitiesMunicipalities`: A list of cities/municipalities in the specified province\n\n## Data Source and Collection\n\nThe zip code data used in this package is sourced from [PHLPost](https://phlpost.gov.ph/) (Philippine Postal Corporation), the official postal service of the Philippines.\n\nTo keep data current, use custom scraper tool (`scraper.py`).\n\n## Development\n\n1. **Clone the repository**\n\n ```bash\n git clone https://github.com/jayson-panganiban/phzipcodes.git\n cd phzipcodes\n ```\n\n2. **Install Poetry if you haven't already**\n\n ```bash\n curl -sSL https://install.python-poetry.org | python3 -\n ```\n\n3. **Install dependencies**\n\n ```bash\n poetry install\n ```\n\n Or using pip:\n\n ```bash\n pip install -r requirements.txt\n ```\n\n4. **Run Tests**\n\n ```bash\n poetry run pytest\n ```\n\n5. **Run linter**\n\n ```bash\n poetry run ruff check .\n ```\n\n6. **Run formatter**\n\n ```bash\n poetry run ruff format .\n ```\n\n7. **Run type checker**\n\n ```bash\n poetry run mypy phzipcodes\n ```\n\n8. **To update the zip codes data, run the scraper**\n\n ```bash\n poetry run python phzipcodes/scraper.py\n ```\n\n## Contributing\n\nContributions are welcome! Please open an issue or submit a pull request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python package for Philippines zip codes",
"version": "0.1.8",
"project_urls": {
"Repository": "https://github.com/jayson-panganiban/phzipcodes"
},
"split_keywords": [
"philippines",
" zipcode",
" zipcodes",
" postal code"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d314b8f0fe28d19474adfba1ce40279ff428de484356a0df166c847bc623eaba",
"md5": "16b38168ed7dee89c5d3408757cb48e5",
"sha256": "b795740b6bab51cb827e43467810d93873c4b0a2106a58fe40c0ba8a27941293"
},
"downloads": -1,
"filename": "phzipcodes-0.1.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "16b38168ed7dee89c5d3408757cb48e5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 21789,
"upload_time": "2025-01-12T04:39:53",
"upload_time_iso_8601": "2025-01-12T04:39:53.605603Z",
"url": "https://files.pythonhosted.org/packages/d3/14/b8f0fe28d19474adfba1ce40279ff428de484356a0df166c847bc623eaba/phzipcodes-0.1.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "420acf60129f465fa1709ccc29c9f656e877763b0140c721b7f5bb6e78faf875",
"md5": "4caf2d9813afd90d84712fcdce6055d6",
"sha256": "be9aa20c83f20f787ec4ce07ac2505d0d3b27da9358bab944b54b10db95e78e9"
},
"downloads": -1,
"filename": "phzipcodes-0.1.8.tar.gz",
"has_sig": false,
"md5_digest": "4caf2d9813afd90d84712fcdce6055d6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 22076,
"upload_time": "2025-01-12T04:39:56",
"upload_time_iso_8601": "2025-01-12T04:39:56.638071Z",
"url": "https://files.pythonhosted.org/packages/42/0a/cf60129f465fa1709ccc29c9f656e877763b0140c721b7f5bb6e78faf875/phzipcodes-0.1.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-12 04:39:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jayson-panganiban",
"github_project": "phzipcodes",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "pydantic",
"specs": [
[
">=",
"2.9.2"
]
]
},
{
"name": "aiohttp",
"specs": [
[
">=",
"3.10.10"
]
]
},
{
"name": "bs4",
"specs": [
[
">=",
"0.0.2"
]
]
},
{
"name": "toolz",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "cachetools",
"specs": [
[
">=",
"5.5.0"
]
]
},
{
"name": "pytest",
"specs": [
[
">=",
"8.3.3"
]
]
},
{
"name": "pytest-cov",
"specs": [
[
">=",
"5.0.0"
]
]
},
{
"name": "mypy",
"specs": [
[
">=",
"1.12.0"
]
]
},
{
"name": "ruff",
"specs": [
[
">=",
"0.7.0"
]
]
},
{
"name": "types-beautifulsoup4",
"specs": [
[
">=",
"4.12.0.20240907"
]
]
},
{
"name": "types-cachetools",
"specs": [
[
">=",
"5.5.0.20240820"
]
]
},
{
"name": "types-html5lib",
"specs": [
[
">=",
"1.1.11.20241018"
]
]
}
],
"lcname": "phzipcodes"
}