Hacker News new | past | comments | ask | show | jobs | submit login

I was quite surprised — only a few days ago in fact — to discover the standard Python library has no support for Olson (as in tzdata) timezones. Time arithmetic is impossible without them.

The ipaddress library also has no support for calculating subnets. It is quite hard to go from 2a00:aaaa:bbbb::/48 to 2a00:aaaa:bbbb:cccc::/64. It would be less weird if the essence of the documentation didn’t make it sound like the library was otherwise very thorough in the coverage of its implementation.

Can anyone write a PEP? Maybe I should get off my behind and actually submit a patch for proper IP calculations? Or maybe I missed it in the documentation (which, aside, I wish wasn’t written with such GNU-info style formality.)




Unless I misunderstand what you're looking for, I think that functionality is in there.

    original_net_48 = ip_network("2a00:aaaa:bbbb::/48")
    desired_subnet = ip_network('2a00:aaaa:bbbb:cccc::/64')
    subnets_64 = original_net_48.subnets(16)
    print(f"{desired_subnet} is one of the computed subnets: {desired_subnet in subnets_64}")
    #=> 2a00:aaaa:bbbb:cccc::/64 is one of the computed subnets: True


Thanks, but your second line kind of has the answer in it already. It’s more like...

  site = ip_network(‘2a00:aaaa:bbbb::/48’)
  subnet = f(site, 64, 0xcccc)
...and I don’t think f() is in the standard library. But maybe I just index the calculated subnets, from your example? I’ll give it a go!

Edit: yes! But it’s a bit slow...

https://repl.it/repls/PoshPapayawhipNaturaldocs#main.py


Well, you can speed it up slightly by using the iterator directly instead of forcing the whole generator into a list.

But on the other hand, you can make it WAY faster by doing it with bit manipulation instead like you're writing C:

https://repl.it/repls/ClearcutElaborateEmbeds


Oh, yeah, you're right. That's a shame — that function does exactly what you want, but it has to do it for every possible subnet up to the one you want, and the logic isn't included as a separate function.


The ipaddress library doesn’t do link-local IPv6 addresses either.


Yes, I feel like PEP 615 for timezones is about 20 years late.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: