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 <[email protected]>
<http://bugs.python.org/issue20860>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com