Skip to main content

Python SDK for jpzip — Japanese postal-code lookup served from a static CDN.

Project description

jpzip — Python SDK

日本の郵便番号を CDN 配信の JSON データから引く Python SDK。

  • 配信ドメイン: https://jpzip.nadai.dev
  • プロトコル仕様: jpzip/spec
  • データ ETL: jpzip/data
pip install jpzip

Python 3.10 以上が必要です。

使い方

モジュール関数 API (同期、L1 のみ)

import jpzip

entry = jpzip.lookup("2310017")
# entry is None なら見つからなかった

dict_ = jpzip.lookup_group("23")   # 2 桁は 10 並列 fetch
all_ = jpzip.lookup_all()
meta = jpzip.get_meta()

同期クライアント API (L2 キャッシュ・複数インスタンス用)

from jpzip import JpzipClient

with JpzipClient(
    base_url="https://jpzip.nadai.dev",
    memory_cache_size=200,
    cache=my_cache,           # Cache プロトコルを満たす任意の実装
) as client:
    client.preload("all")
    entry = client.lookup("2310017")

非同期クライアント API

import asyncio
from jpzip import AsyncJpzipClient

async def main():
    async with AsyncJpzipClient() as client:
        entry = await client.lookup("2310017")
        print(entry)

asyncio.run(main())

Cache プロトコル

同期版 Cache と非同期版 AsyncCache の 2 つを提供しています。クライアントに合わせて使い分けてください。

from typing import Protocol

class Cache(Protocol):
    def get(self, key: str) -> bytes | None: ...
    def set(self, key: str, value: bytes) -> None: ...
    def delete(self, key: str) -> None: ...
    def clear(self) -> None: ...

class AsyncCache(Protocol):
    async def get(self, key: str) -> bytes | None: ...
    async def set(self, key: str, value: bytes) -> None: ...
    async def delete(self, key: str) -> None: ...
    async def clear(self) -> None: ...

ファイル / SQLite / Redis / S3 等の任意の実装を渡せます。 プロトコルなので継承宣言は不要で、構造的に満たしていれば OK です。

キャッシュ層

  • L1: メモリ内 LRU (URL キー、デフォルト 100 prefix)。常時 ON。
  • L2: cache= で渡したときのみ ON。デフォルト OFF。
  • L3: HTTP 層のキャッシュは httpx / OS / CDN に委ねる。

入力検証

lookup()^\d{7}$ にマッチしない入力には fetch せず None を返します。 書式チェックだけしたい場合は jpzip.is_valid_zipcode(s) を使ってください。

バージョン整合性

get_meta()spec_version が SDK のサポート版と異なる場合、 on_spec_mismatch で渡したコールバックが 1 度だけ呼ばれます。 データバージョン (version) が変わった場合は L1/L2 を自動 invalidate します。

client = JpzipClient(
    on_spec_mismatch=lambda expected, received: print(
        f"spec mismatch: expected={expected} received={received}"
    ),
)

リトライ

5xx 応答とネットワークエラーには指数バックオフ (200ms × 2^n) で最大 3 回リトライします。 404 は「該当なし」として None を返し、リトライしません。

ライセンス

MIT

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

jpzip-0.1.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jpzip-0.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file jpzip-0.1.0.tar.gz.

File metadata

  • Download URL: jpzip-0.1.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jpzip-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4c16b3dd64eeb745a5fab2cf056dc589e1689cfebe083eee710168521ed3a8ae
MD5 aa36406ccf69abc2cbc661f22242e169
BLAKE2b-256 df8217de387ec79c6d5c4924a050f987422a04f6273c94072f78cfb4fd139b47

See more details on using hashes here.

Provenance

The following attestation bundles were made for jpzip-0.1.0.tar.gz:

Publisher: publish.yml on jpzip/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jpzip-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jpzip-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jpzip-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 92706654962586dafd1ca78afd0607fbd80a12c6f1da83127e44e22a463ac77c
MD5 d8faa1f8726dbb18b742b6abaf98bbc1
BLAKE2b-256 10c97afa781d4852590b49bc4875a16967f230c89464512391eb6a00fb1122d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for jpzip-0.1.0-py3-none-any.whl:

Publisher: publish.yml on jpzip/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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