[New-bugs-announce] [issue20860] ipaddress network subnets() method should return object with __getitem__

Warren Turkal report at bugs.python.org
Thu Mar 6 19:53:27 CET 2014


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')
>>> 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. :)

----------
components: Library (Lib)
messages: 212836
nosy: Warren.Turkal
priority: normal
severity: normal
status: open
title: ipaddress network subnets() method should return object with __getitem__
type: enhancement
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20860>
_______________________________________


More information about the New-bugs-announce mailing list