Skip to main content

Fast IP prefix lookup library with Rust backend

Project description

Pattrie

Fast IP prefix trie for Python with a Rust backend. Supports longest-prefix match (LPM) lookups on IPv4 and IPv6 networks.

Built with PyO3 and the prefix-trie crate. Inspired by pytricia.

Installation

pip install pattrie

Usage

import pattrie

t = pattrie.Pattrie()
t["10.0.0.0/8"] = "rfc1918"
t["10.1.0.0/16"] = "internal"

t["10.1.2.3"]          # "internal"      (LPM)
t["10.2.0.1"]          # "rfc1918"       (LPM)
"10.1.2.3" in t        # True
t.get_key("10.1.2.3")  # "10.1.0.0/16"
t.get("192.0.2.1")     # None

Keys can be strings, ipaddress.IPv4Address, IPv4Network, IPv6Address, or IPv6Network.

IPv6

import socket
import pattrie

t = pattrie.Pattrie(128, socket.AF_INET6)
t["2001:db8::/32"] = "documentation"
t["fe80::/10"] = "link-local"

t["fe80::1"]  # "link-local"

Concurrent reads with freeze()

After freeze(), read operations release the GIL, allowing true parallel lookups from multiple threads:

t = pattrie.Pattrie()
# ... populate ...
t.freeze()  # trie becomes read-only; reads release the GIL

# Safe to call from many threads simultaneously
value = t["10.1.2.3"]

t.thaw()    # restore mutability

API

class Pattrie:
    def __init__(self, maxbits: int = 32, family: int = socket.AF_INET) -> None: ...

    # Insert
    def insert(self, prefix: str | IPv4Network | IPv6Network, value: object) -> None: ...
    def insert(self, addr: str | IPv4Address | IPv6Address, prefixlen: int, value: object) -> None: ...
    def __setitem__(self, key: str | IPv4Network | IPv6Network, value: object) -> None: ...

    # Lookup (LPM)
    def __getitem__(self, key: str | IPv4Address | IPv6Address | IPv4Network | IPv6Network) -> object: ...
    def get(self, key, default=None) -> object: ...
    def get_key(self, key) -> str | None: ...
    def __contains__(self, key) -> bool: ...

    # Exact match
    def has_key(self, key: str | IPv4Network | IPv6Network) -> bool: ...

    # Delete
    def __delitem__(self, key: str | IPv4Network | IPv6Network) -> None: ...
    def delete(self, key: str | IPv4Network | IPv6Network) -> None: ...

    # Iteration
    def __iter__(self) -> Iterator[str]: ...
    def keys(self) -> list[str]: ...
    def __len__(self) -> int: ...

    # Concurrency
    def freeze(self) -> None: ...
    def thaw(self) -> None: ...

Performance

See benches/README.md for methodology and numbers.

License

MIT — see LICENSE.

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

pattrie-0.1.2.tar.gz (68.1 kB view details)

Uploaded Source

Built Distributions

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

pattrie-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl (283.9 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

pattrie-0.1.2-cp314-cp314-musllinux_1_2_aarch64.whl (272.7 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ ARM64

pattrie-0.1.2-cp314-cp314-manylinux_2_28_aarch64.whl (273.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ ARM64

pattrie-0.1.2-cp314-cp314-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (563.3 kB view details)

Uploaded CPython 3.14macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

pattrie-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl (282.8 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

pattrie-0.1.2-cp313-cp313-musllinux_1_2_aarch64.whl (271.3 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ ARM64

pattrie-0.1.2-cp313-cp313-manylinux_2_28_aarch64.whl (272.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ ARM64

pattrie-0.1.2-cp312-cp312-win_amd64.whl (185.9 kB view details)

Uploaded CPython 3.12Windows x86-64

pattrie-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl (283.3 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

pattrie-0.1.2-cp312-cp312-musllinux_1_2_aarch64.whl (271.7 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

pattrie-0.1.2-cp312-cp312-manylinux_2_28_aarch64.whl (272.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

pattrie-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (303.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file pattrie-0.1.2.tar.gz.

File metadata

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

File hashes

Hashes for pattrie-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1ebb120eae31252c02dd94cce87c960b35f63004c33b0ceba1120ff3742793fc
MD5 72767da22293cebe54b40295615cc2f0
BLAKE2b-256 8fcb8a86d3b0d104d016af615212b6560108593795f9bfb02ad722c5846cf1d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2.tar.gz:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 532bae63dbbd6e88f70077355f7905da2a7118430b930b7653aac296f732a1d2
MD5 36c9fdb4108da42508eb587117fde835
BLAKE2b-256 a94d799c5c24e976769b84b6ba7c37111529207df3c93458b5c724e322821488

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp314-cp314-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp314-cp314-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 11177213cade7237ac8f4225513173469deb7568f81eb7de84cfff4b248792b9
MD5 ed4c556ad2a5604c323a6154ce54e14a
BLAKE2b-256 22d86b8c2076a80bea8188576c487d8214498efc312daf48f7caf8f29254a056

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp314-cp314-musllinux_1_2_aarch64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp314-cp314-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp314-cp314-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 de95df8781e589f631a62ba7829089fe6aee559c8826b20d737dcaba63f70e63
MD5 a6823c7bca72cebcb88665c2573a5c70
BLAKE2b-256 a23bf4852c0ebf9d6f7ef14116b2a65c3f367eb60ef11b7a79b47d872973f11e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp314-cp314-manylinux_2_28_aarch64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp314-cp314-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp314-cp314-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 781e1ed168653954ad3b5c8a17b6590d83622f72c199f85ddd0527458a0a689c
MD5 2dddd32a661cfaae161cc12c12d3f070
BLAKE2b-256 2846947cadf1a02337ec64d94b3b5bb9248403fc5e6fc1a4dfd64e69638d3508

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp314-cp314-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 422916f36b966ff0402f8510b9b0d8282537f281f103e133e04e3d452dba5398
MD5 291b3044a8162c9601f2cf9e53389ff2
BLAKE2b-256 909672d785f701d6374f5717d7d3465412fc18c01b174620b3bfc59401cc8497

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp313-cp313-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp313-cp313-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 b3fddb66d4dd7ed74650d2f43bc660dc94b584a331a6ccdfea9289058b411066
MD5 d978793b2aedde540e86ef56a9b475a9
BLAKE2b-256 b89ceec4fe0c3805dc3a8a0f4988261eea3a31fdde677279427432407b74784c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp313-cp313-musllinux_1_2_aarch64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp313-cp313-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp313-cp313-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 88840b621eeaa029b11f16cd7b51028bbff7fe6bc78d8ede2a535d702eeb08e8
MD5 2bb6d16e6055ccc045039f0a1fe9ed90
BLAKE2b-256 e785cc54194e25bccd66cb8fd9d4ae3f7b7a3dec0343f4db3875f32a11a89d90

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp313-cp313-manylinux_2_28_aarch64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pattrie-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 185.9 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pattrie-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 525d5cf8f62e0a75924311a5c699bfb0ca761393ed0446d6b482610e2f45caf1
MD5 2d58359e038eb234a477bba0e2605b09
BLAKE2b-256 70aec96fb389875b1aa41167a89a551517cc61811132c436d02333e8aba7f84f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f02904b1a6fa9e43a1270b5598ab1b804e33a95807d6450470db0dc2a9698a35
MD5 f2f143e111862335daefec08ea1e1948
BLAKE2b-256 abbf4ea9df6787e888a95381b55894db580dd23a8f01bde82af4dd4d7a70860c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4a4875229e374b61a8795578981b2e4917ac604d0cf869df3e44b341ca380e32
MD5 f7a7505568ce18b02b5bbd62ab4903cd
BLAKE2b-256 4e2c0c5795f206d1b7d5f99300d3373232b1438e567f24fa43ef0bc8613d65c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp312-cp312-musllinux_1_2_aarch64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9a97648dd7a6a9d16dc4c24ffab7e7ac232b54d2ee8688981b5cd399afe6fded
MD5 56d01468b1d7a60b11c176cc0cfcc88a
BLAKE2b-256 bde740218d02dfee3a391bc6290dd6de303fd3861ebb7bfb32ffef556df2ec31

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp312-cp312-manylinux_2_28_aarch64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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

File details

Details for the file pattrie-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5f616334f85538d671299a7d7b39da461afeb6eb9f7b659b5eed5110b38f088
MD5 335a5fd59a2cb242b2d6cdf8107b9d95
BLAKE2b-256 e54329a5676e57559d565eff83270668d23cb4af032cf1ed980823995a4cf175

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on dogukancagatay/pattrie

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