New submission from Warren Turkal:
It would be very useful to be able to not only iterate through subnets, but
also index a subnet. For example, I would really like to be able to do the
following:
import ipaddress as ipa
net = ipa.ip_network('10.0.0.0/8')
R. David Murray added the comment:
I think you are looking for list(net.subnets(prefixlen_diff=2))[2]. This is
the standard Python way of going from an iterator to an indexable collection.
--
nosy: +r.david.murray
resolution: - rejected
stage: - committed/rejected
status: open -
Warren Turkal added the comment:
Won't that instantiate an object for each item in the list though? For example:
list(net.subnets(prefixlen_diff=16))[499]
This take a long time. I was trying to think of a way to lazily instantiate.
For example, I don't want to create 65536 network objects
R. David Murray added the comment:
The interface you are suggesting isn't consistent with other stdlib APIs.
Perhaps it would be better to discuss this concept on pyhon-ideas...additional
methods for computing the number of subnets for a given prefix, and a different
one for constructing one
R. David Murray added the comment:
Or maybe a subnet_range method that returns a range-like object.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue20860
___
STINNER Victor added the comment:
It would be very useful to be able to not only iterate through subnets, but
also index a subnet.
For your information, the IPy module supports that:
tuple(IPy.IP('192.168.1.128/31'))
(IP('192.168.1.128'), IP('192.168.1.129'))
IPy.IP('2000::/3')[2**120]
STINNER Victor added the comment:
I reopen the issue because the list option may create an huge list. Try the
IPv6 2000::/3 network :-)
--
nosy: +haypo
resolution: rejected -
status: closed - open
___
Python tracker rep...@bugs.python.org