This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author wt
Recipients wt
Date 2014-03-06.18:53:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1394132007.23.0.255663589608.issue20860@psf.upfronthosting.co.za>
In-reply-to
Content
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')
>>> print(net.subnets(prefixlen_diff=2)[2])
"10.128.0.0/10"

As it stands now, I have to write something like the following to get the same result:

>>> import ipaddress as ipa
>>> net = ipa.ip_network('10.0.0.0/8')
>>> subnets = net.subnets(prefixlen_diff=2)
>>> for _ in xrange(0, 3):
...     subnet = subnets.next()
...
>>> print(subnet)
"10.128.0.0/10"


The simplest way I can come up with to add this feature is by wrapping the current body of that method in a nested generator function, creating an instance of that generator, adding a appropriate __getitem__ method to that object, and returning that object instead of the bare generator. What do you all think of that?

Also, it'd be nice to see this added to the ipaddress module on pypi for python 2.x also. :)
History
Date User Action Args
2014-03-06 18:53:27wtsetrecipients: + wt
2014-03-06 18:53:27wtsetmessageid: <1394132007.23.0.255663589608.issue20860@psf.upfronthosting.co.za>
2014-03-06 18:53:27wtlinkissue20860 messages
2014-03-06 18:53:26wtcreate