marvin-zhao / ipaddr-py

Automatically exported from code.google.com/p/ipaddr-py
0 stars 0 forks source link

I think IPNetwork objects should have a len() method (really __len__ I suppose) #111

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
   import ipaddr
   i =ipaddr.IPNetwork('192.168.1.0/24')
   print len(i)

   TypeError: object of type 'IPv4Network' has no len()

same error for ipv6,fyi.

What is the expected output? What do you see instead?
I think I'd like to see the length of the string representation... or:
  print len(str(i))
  14

What version of the product are you using? On what operating system?
whatever is installed on ubuntu 12.04 LTS...

||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  python-ipaddr  2.1.10-1     all          Python module for working with IP

Please provide any additional information below.

Original issue reported on code.google.com by morr...@google.com on 9 Sep 2014 at 8:01

GoogleCodeExporter commented 9 years ago
I think len() on a network object returning the length of the string 
representation doesn't pass the principal of least astonishment. That is, *I* 
would expect (and indeed, initially coded it this way), that len() on a network 
object would return the number of addresses contained in the network. That was 
ditched when I found out that __len__ always returns (or returned in python < 
3.x, I'm not sure what it does these days) a 32 bit integer, which is obviously 
insufficient for ipv6 networks.

anyway, the tl;dr, as you mentioned, is to do len(str(network)).

Original comment by pmo...@google.com on 10 Sep 2014 at 2:42

GoogleCodeExporter commented 9 years ago
ok, sounds reasonable.

Original comment by morr...@google.com on 10 Sep 2014 at 2:45