No project description provided
Project description
Polars IPTools
Polars IPTools is a Rust-based extension to accelerates IP address manipulation and enrichment in Polars dataframes. This library includes various utility functions for working with IPv4 and IPv6 addresses and geoip enrichment using MaxMind databases.
Install
pip install polars-iptools
Examples
Simple enrichments
IPTools' Rust implementation gives you speedy answers to basic IP questions like "is this a private IP?"
>>> import polars as pl
>>> import polars_iptools as ip
>>> df = pl.DataFrame({'ip': ['8.8.8.8', '2606:4700::1111', '192.168.100.100', '172.21.1.1', '172.34.5.5', 'a.b.c.d']})
>>> df.with_columns(ip.is_private(pl.col('ip')).alias('is_private'))
shape: (6, 2)
┌─────────────────┬────────────┐
│ ip ┆ is_private │
│ --- ┆ --- │
│ str ┆ bool │
╞═════════════════╪════════════╡
│ 8.8.8.8 ┆ false │
│ 2606:4700::1111 ┆ false │
│ 192.168.100.100 ┆ true │
│ 172.21.1.1 ┆ true │
│ 172.34.5.5 ┆ false │
│ a.b.c.d ┆ false │
└─────────────────┴────────────┘
is_in
but for network ranges
Pandas and Polars have is_in
functions to perform membership lookups. IPTools extends this to enable IP address membership in IP networks. This function works seamlessly with both IPv4 and IPv6 addresses and converts the specified networks into a Level-Compressed trie (LC-Trie) for fast, efficient lookups.
>>> import polars as pl
>>> import polars_iptools as ip
>>> df = pl.DataFrame({'ip': ['8.8.8.8', '1.1.1.1', '2606:4700::1111']})
>>> networks = ['8.8.8.0/24', '2606:4700::/32']
>>> df.with_columns(ip.is_in(pl.col('ip'), networks).alias('is_in'))
shape: (3, 2)
┌─────────────────┬───────┐
│ ip ┆ is_in │
│ --- ┆ --- │
│ str ┆ bool │
╞═════════════════╪═══════╡
│ 8.8.8.8 ┆ true │
│ 1.1.1.1 ┆ false │
│ 2606:4700::1111 ┆ true │
└─────────────────┴───────┘
GeoIP enrichment
Using MaxMind's GeoLite2-ASN.mmdb and GeoLite2-City.mmdb databases, IPTools provides offline enrichment of network ownership and geolocation.
ip.geoip.full
returns a Polars struct containing all available metadata parameters. If you just want the ASN and AS organization, you can use ip.geoip.asn
.
>>> import polars as pl
>>> import polars_iptools as ip
>>> df = pl.DataFrame({"ip":["8.8.8.8", "192.168.1.1", "2606:4700::1111", "999.abc.def.123"]})
>>> df.with_columns([ip.geoip.full(pl.col("ip")).alias("geoip")])
shape: (4, 2)
┌─────────────────┬─────────────────────────────────┐
│ ip ┆ geoip │
│ --- ┆ --- │
│ str ┆ struct[11] │
╞═════════════════╪═════════════════════════════════╡
│ 8.8.8.8 ┆ {15169,"GOOGLE","","NA","","",… │
│ 192.168.1.1 ┆ {0,"","","","","","","",0.0,0.… │
│ 2606:4700::1111 ┆ {13335,"CLOUDFLARENET","","","… │
│ 999.abc.def.123 ┆ {null,null,null,null,null,null… │
└─────────────────┴─────────────────────────────────┘
>>> df.with_columns([ip.geoip.asn(pl.col("ip")).alias("asn")])
shape: (4, 2)
┌─────────────────┬───────────────────────┐
│ ip ┆ asn │
│ --- ┆ --- │
│ str ┆ str │
╞═════════════════╪═══════════════════════╡
│ 8.8.8.8 ┆ AS15169 GOOGLE │
│ 192.168.1.1 ┆ │
│ 2606:4700::1111 ┆ AS13335 CLOUDFLARENET │
│ 999.abc.def.123 ┆ │
└─────────────────┴───────────────────────┘
Environment Configuration
IPTools uses two MaxMind databases: GeoLite2-ASN.mmdb and GeoLite2-City.mmdb. You only need these files if you call the geoip functions.
Set the MAXMIND_MMDB_DIR
environment variable to tell the extension where these files are located.
export MAXMIND_MMDB_DIR=/path/to/your/mmdb/files
# or Windows users
set MAXMIND_MMDB_DIR=c:\path\to\your\mmdb\files
If the environment is not set, polars_iptools will check two other common locations (on Mac/Linux):
/usr/local/share/GeoIP
/opt/homebrew/var/GeoIP
Credit
Developing this extension was super easy by following Marco Gorelli's tutorial and cookiecutter template.
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
Built Distributions
File details
Details for the file polars_iptools-0.1.7.tar.gz
.
File metadata
- Download URL: polars_iptools-0.1.7.tar.gz
- Upload date:
- Size: 45.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a275b53cebc15eae0cfc6bd131ea8703b9c13e94b887f090054236b3fe8ba43 |
|
MD5 | cf977c84bb90ab27721c739c8d416e6e |
|
BLAKE2b-256 | 001cdf2872f153c707461ecc480ac272ea26d83b59896d9016abaf15176eaae4 |
Provenance
File details
Details for the file polars_iptools-0.1.7-cp38-abi3-win_amd64.whl
.
File metadata
- Download URL: polars_iptools-0.1.7-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 3.5 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04c20e631f325a6c658d3f8e1669cc8106264d3d857bd1326e2f9b9c50b9a987 |
|
MD5 | d33e9fdda055929e9b9ab3ef5bc708a5 |
|
BLAKE2b-256 | 608f17da7ffb8fc1092c8703e0bc89a09fe06fd3a270aecd42745b981269471c |
Provenance
File details
Details for the file polars_iptools-0.1.7-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: polars_iptools-0.1.7-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.2 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 360e5e08ef36809a877d143752d0a7264842097099e237c9b753a3b3e3bd5c75 |
|
MD5 | 65aeabd3140ba57da82f8e35d0cc31f4 |
|
BLAKE2b-256 | db7c1dca43a43ca4d96f0c3fd07d29dfebfa72443099bf3688c4c139fed44d6e |
Provenance
File details
Details for the file polars_iptools-0.1.7-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
.
File metadata
- Download URL: polars_iptools-0.1.7-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
- Upload date:
- Size: 3.5 MB
- Tags: CPython 3.8+, manylinux: glibc 2.12+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6151140d770fb07042890bb0082c2d3a843e7a06bf390f2d17d233448481d52d |
|
MD5 | aabfab273a8bb5e5a57beebad38cb299 |
|
BLAKE2b-256 | 4b2eac5432ae86cc7952f60f5d4937125885367df399f320f8c81435bf641fa3 |
Provenance
File details
Details for the file polars_iptools-0.1.7-cp38-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: polars_iptools-0.1.7-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 2.8 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f69fa15cd9f300ee6dd468a2d4a34f33c32f8a69358abc8dddd2f480dd30a32 |
|
MD5 | ae9803bb44aa1fe1bf9d52287dce0e87 |
|
BLAKE2b-256 | d948c0e4e8545cd210fe4d22ce80d94907fc1c03d69d72baea14411d1c1ee9c6 |
Provenance
File details
Details for the file polars_iptools-0.1.7-cp38-abi3-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: polars_iptools-0.1.7-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 3.2 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01fa960fb635e78be3fe20dacfe836b58eb85ae4e2c3440e9b79658fefbda288 |
|
MD5 | 92fa54358e06fc03bb2cb1859ff3f1e5 |
|
BLAKE2b-256 | 3594ff62fa659ebb2c893bde67cd4b6f1d6fc664e1cfa90b4bd772c3aba54457 |