Skip to main content

A Python package for Philippines zip codes

Project description

phzipcodes

Philippines zip codes package

Installation

Ensure you have Python 3.9 or higher installed.

Install the package using pip:

pip install phzipcodes

Usage

import phzipcodes

# Get zip code information by zip code
zip_info = phzipcodes.get_by_zip("1000")
print(zip_info)
# Output: ZipCode(code='1000', city_municipality='Manila', province='Metro Manila', region='NCR')

# Search for zip codes
results = phzipcodes.search("Manila")
for result in results:
    print(result)

# Advanced search options
results = phzipcodes.search("Manila", fields=("city_municipality",), match_type="exact")
print([result.code for result in results])
# Output: ['1000', '1001', '1002', '1003', '1004', '1005', '1006', '1007', '1008']

# Get all unique regions
regions = phzipcodes.get_regions()
print(regions[:3])
# Output: ['NCR', 'CAR', 'Region I']

# Get all provinces in a specific region
provinces = phzipcodes.get_provinces("NCR")
print(provinces)
# Output: ['Metro Manila']

# Get all cities/municipalities in a specific province
cities = phzipcodes.get_cities_municipalities("Metro Manila")
print(cities[:3])
# Output: ['Manila', 'Quezon City', 'Caloocan']

API Reference

search(query: str, fields: tuple[str, ...] = DEFAULT_SEARCH_FIELDS, match_type: str = "contains") -> tuple[ZipCode, ...]

Search for zip codes based on query and criteria.

  • Parameters:
    • query: str - The search query
    • fields: tuple[str, ...] (optional) - Fields to search in (default: city, province, region)
    • match_type: str (optional) - Type of match to perform (default: "contains")
  • Returns: tuple[ZipCode, ...] - Tuple of matching ZipCode objects

get_by_zip(zip_code: str) -> ZipCode | None

Retrieve zip code information by zip code.

  • Parameters:
    • zip_code: str - The zip code to look up
  • Returns: ZipCode | None - ZipCode object if found, None otherwise

get_regions() -> list[str]

Get all unique regions.

  • Returns: list[str] - List of all unique regions

get_provinces(region: str) -> List[str]

Get all provinces in a specific region.

  • Parameters:
    • region: str - The region to get provinces for
  • Returns: list[str] - List of provinces in the specified region

get_cities_municipalities(province: str) -> List[str]

Get all cities/municipalities in a specific province.

  • Parameters:
    • province: str - The province to get cities for
  • Returns: list[str] - List of cities/municipalities in the specified province

Data Structure

The package uses a ZipCode class with the following attributes:

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

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.3.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

phzipcodes-0.1.3-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: phzipcodes-0.1.3.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.0 Windows/10

File hashes

Hashes for phzipcodes-0.1.3.tar.gz
Algorithm Hash digest
SHA256 57b22508a31a4937ae30b5cf629a14103a4a86ae290e28f794b49e29d29390d3
MD5 6a272b06d5d55b7f5cc1200d6a684483
BLAKE2b-256 a4bb1ee5a0f2b22b132b833a0b23ce03279228f1c0e6024b53ecb30ec9da5fef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: phzipcodes-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.0 Windows/10

File hashes

Hashes for phzipcodes-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f38b0f6cce341d54a6843804c2c393428083f98f6fc02fd2e3830f8b82ba9453
MD5 af155a420dda9fb70ddeff578b5f9635
BLAKE2b-256 0cef15c9a337ac51fd380692f2a0496f73c005c30892dd2c7e12400f9c3a4504

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page