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.1.tar.gz (37.0 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.1-cp314-cp314-musllinux_1_2_x86_64.whl (282.3 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

pattrie-0.1.1-cp314-cp314-musllinux_1_2_aarch64.whl (271.3 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ ARM64

pattrie-0.1.1-cp314-cp314-manylinux_2_28_aarch64.whl (272.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ ARM64

pattrie-0.1.1-cp314-cp314-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (562.5 kB view details)

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

pattrie-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl (281.3 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

pattrie-0.1.1-cp313-cp313-musllinux_1_2_aarch64.whl (269.9 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ ARM64

pattrie-0.1.1-cp313-cp313-manylinux_2_28_aarch64.whl (271.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ ARM64

pattrie-0.1.1-cp312-cp312-win_amd64.whl (184.3 kB view details)

Uploaded CPython 3.12Windows x86-64

pattrie-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (281.7 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

pattrie-0.1.1-cp312-cp312-musllinux_1_2_aarch64.whl (270.3 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

pattrie-0.1.1-cp312-cp312-manylinux_2_28_aarch64.whl (271.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

pattrie-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (301.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pattrie-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6877784429dbf82573db8d6475102e69b4606ae2201f49a9a00826fdbc78450b
MD5 70d060a49aa7438b3a3586bc6ea7aed3
BLAKE2b-256 a0c43f96fa2426fed0d7b1966453f767c41e81912bd57772683b9c76982d4b60

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1.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.1-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3f6ff7cf5dd6a6eec36ef5149d76eded075ab346647dd861158e07c7666e2dd8
MD5 c09fbf9876048fae7bb44300d508666d
BLAKE2b-256 9980d787ad08e420a46bbd8e2fff5b67b6a8cc1d6cd120330efbd50cc16ba581

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp314-cp314-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp314-cp314-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 960bbb1c21a3b738961e2ed6592870cb85a0093d18db0060a0972698bc0fe186
MD5 1227cddbc75fc8d9b85ebe2247f5c64e
BLAKE2b-256 52134f59851b93440a357f8daf8787c1cd5f086cc56762d5d9484e234bbf61ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp314-cp314-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp314-cp314-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 12fa5d020fa0d3bf2fe6197e838a044a776a8f981ac5d75f923d2e1bc6367fb5
MD5 ddeea88fb63f0095be0a8878bf2ec489
BLAKE2b-256 9be10a5b677fc407549ddc3e9960c2aed6173779740f9d18af4fbf9398722e91

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-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.1-cp314-cp314-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 a6799499fc5109d9cb8a7346559439f963881261f2d42141c1e740b0941fbed4
MD5 55d3a9da7c0f12bf354dac996f2b29bf
BLAKE2b-256 e334a74a24a02fabf7b914cb2807f507c4aa315ebd64f269b11d10c253ff777f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1e0e678bb87d20635abfa290561ab8ede1eea55cdbe4cd9f8fdaac1bcfcc38d6
MD5 97299b4a4e959229cc5cc290c846e421
BLAKE2b-256 627c56bb7dcb8aa235f7bbc07e6b3ae80fe33de2c0ab9ab4608b5681933395ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp313-cp313-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp313-cp313-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8917ba26382dc7af31e04bdcad7ca0ec89dcf78571afc357d15d04f61737df96
MD5 7320f0d77f05a9c11b69601bab110246
BLAKE2b-256 275c7c40b4ba089925bfa823ede9321181eac53419365ff259638c96c9f02790

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp313-cp313-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp313-cp313-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5140d85bbdef3dda56a81562474154150389e18c0a07fbc5023a8e5d878819d6
MD5 2927541e81ee2f41547209831568faf0
BLAKE2b-256 28562a1445e3939920ad2d19680473265479e11bf202237ec9946a7511e9658d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp312-cp312-win_amd64.whl.

File metadata

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

File hashes

Hashes for pattrie-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 65584925069b9c6f2a47e49545787a69b1fd9554cf91595c496ea55b6bd7acb1
MD5 462b29f43f5d8528d257904efe6a3a6f
BLAKE2b-256 62ef1f885c2d427cf5aa24fae8df8574d2c6fcec613831fe50d050f2c8fd8776

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ef02756d73dadb868782791dad81dcea284fa89360f650778763ca11e240ac7d
MD5 eac77b98c894809a0be73742dc3da67a
BLAKE2b-256 8659d3ec213dc991445e4c449417c5a9fadf99f733600d43205c8002366307f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 cb16522d44df12a011d736bf55fa3989a02747ad81c4e2b02b380011b733a0ff
MD5 4e598d14b9612a4ee7d4bbd0671a015f
BLAKE2b-256 96aa4a1e6d7ed0b5fb8550e87b4f84b51b756ac6c6ccbb9b8cb4d2999bc865bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 45aa7b05b3e061aae7c24d3f15a4c277900bc97af42b841e3a4e289cb7e90753
MD5 129e38860cf06fbc1a5b91bce3366052
BLAKE2b-256 6a62c986351aecc686598bc133c1d11a21ba272c9160e59d5e37a0255e3fcbcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pattrie-0.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a36055bc6844ffee2c1a144f073a9ba09d720f1bcf61e55e514c245bdaf2ac10
MD5 f45cfa95e5c66065d8dbbb546bd2abef
BLAKE2b-256 b01cbd9026e28fae93933d84d3ad878e08cc5a6f7897dd33ea948fd56b89fa63

See more details on using hashes here.

Provenance

The following attestation bundles were made for pattrie-0.1.1-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