jsommers / pytricia

A library for fast IP address lookup in Python.
GNU Lesser General Public License v3.0
214 stars 22 forks source link

Add (bytes, size) input/output to all functions #18

Closed tristan-ovh closed 5 years ago

tristan-ovh commented 6 years ago

Hi, I don't know if this feature will be interesting to you. I have added the possibility to use "raw" input and output for prefixes.

All functions accept prefixes as tuples (bytes, size) as input as well as the normal CIDR string format.

I have also added a raw_output argument to the PytriciaTree class init. It defaults to False. When set to True, the prefixes output by each function are tuples (bytes, size) instead of CIDR strings.

I have added tests that show how the feature can be used. I use this library in an application that manipulates addresses a lot, and converting them to CIDRs and back to bytes is time-consuming.

If you are interested in adding features to the library, I have another function "find_children" a bit more flexible than the current "children" function (works if the prefix is not in the tree, and has an option to include grandchildren or not). I could make another pull request, but it depends on this one.

ghost commented 5 years ago

Hi, this feature is very interesting. Could you merge it in the main project ?

for 10 000 000 lookup:

!

Thanks.

jsommers commented 5 years ago

Thanks for this pull request, and sorry for taking so long to comment on it. I'm happy to merge it. There is a bit of refactoring I'll want to do at some point soon, but I'll merge as-is and do that later. I'll consider the find_children function if you want to do a pull request for that, too (and I'll try to respond a bit sooner).