2626__version__ = '0.6.0-dev'
2727
2828__all__ = (
29- 'IpRange' ,
30- 'IpRangeList' ,
31- )
29+ 'IpRange' ,
30+ 'IpRangeList' ,
31+ )
3232
3333# sniff for python2.x / python3k compatibility "fixes'
3434try :
4141 next = next
4242except NameError :
4343 # builtin next function doesn't exist
44- def next (iterable ):
44+ def next (iterable ):
4545 return iterable .next ()
4646
4747try :
@@ -54,6 +54,7 @@ def next (iterable):
5454
5555from . import ipv4
5656
57+
5758class IpRange (Sequence ):
5859 """
5960 Range of ip addresses.
@@ -93,7 +94,7 @@ class IpRange (Sequence):
9394 :param end: Ip address in dotted quad format or ``None``.
9495 :type end: str
9596 """
96- def __init__ (self , start , end = None ):
97+ def __init__ (self , start , end = None ):
9798 if end is None :
9899 if isinstance (start , IpRange ):
99100 # copy constructor
@@ -122,7 +123,7 @@ def __init__ (self, start, end=None):
122123 self ._len = self .endIp - self .startIp + 1
123124 #end __init__
124125
125- def __repr__ (self ):
126+ def __repr__ (self ):
126127 """
127128 >>> repr(IpRange('127.0.0.1'))
128129 "IpRange('127.0.0.1', '127.0.0.1')"
@@ -132,10 +133,10 @@ def __repr__ (self):
132133 "IpRange('127.0.0.0', '127.0.0.255')"
133134 """
134135 return "IpRange(%r, %r)" % (
135- ipv4 .long2ip (self .startIp ), ipv4 .long2ip (self .endIp ))
136+ ipv4 .long2ip (self .startIp ), ipv4 .long2ip (self .endIp ))
136137 #end __repr__
137138
138- def __str__ (self ):
139+ def __str__ (self ):
139140 """
140141 >>> str(IpRange('127.0.0.1'))
141142 "('127.0.0.1', '127.0.0.1')"
@@ -144,10 +145,11 @@ def __str__ (self):
144145 >>> str(IpRange('127.0.0.255', '127.0.0.0'))
145146 "('127.0.0.0', '127.0.0.255')"
146147 """
147- return (ipv4 .long2ip (self .startIp ), ipv4 .long2ip (self .endIp )).__repr__ ()
148+ return (
149+ ipv4 .long2ip (self .startIp ), ipv4 .long2ip (self .endIp )).__repr__ ()
148150 #end __str__
149151
150- def __eq__ (self , other ):
152+ def __eq__ (self , other ):
151153 """
152154 >>> IpRange('127.0.0.1') == IpRange('127.0.0.1')
153155 True
@@ -157,11 +159,11 @@ def __eq__ (self, other):
157159 True
158160 """
159161 return isinstance (other , IpRange ) and \
160- self .startIp == other .startIp and \
161- self .endIp == other .endIp
162+ self .startIp == other .startIp and \
163+ self .endIp == other .endIp
162164 #end __eq__
163165
164- def __len__ (self ):
166+ def __len__ (self ):
165167 """
166168 Return the length of the range.
167169
@@ -176,7 +178,7 @@ def __len__ (self):
176178 return self ._len
177179 #end __len__
178180
179- def __hash__ (self ):
181+ def __hash__ (self ):
180182 """
181183 >>> a = IpRange('127.0.0.0/8')
182184 >>> b = IpRange('127.0.0.0', '127.255.255.255')
@@ -191,7 +193,7 @@ def __hash__ (self):
191193 return hash ((self .startIp , self .endIp ))
192194 #end __hash__
193195
194- def _cast (self , item ):
196+ def _cast (self , item ):
195197 if isinstance (item , basestring ):
196198 item = ipv4 .ip2long (item )
197199 if type (item ) not in [type (1 ), type (ipv4 ._MAX_IP )]:
@@ -200,7 +202,7 @@ def _cast (self, item):
200202 return item
201203 #end _cast
202204
203- def index (self , item ):
205+ def index (self , item ):
204206 """
205207 Return the 0-based position of `item` in this IpRange.
206208
@@ -227,11 +229,11 @@ def index (self, item):
227229 raise ValueError ('%s is not in range' % ipv4 .long2ip (item ))
228230 #end index
229231
230- def count (self , item ):
232+ def count (self , item ):
231233 return int (item in self )
232234 #end count
233235
234- def __contains__ (self , item ):
236+ def __contains__ (self , item ):
235237 """
236238 Implements membership test operators ``in`` and ``not in`` for the
237239 address range.
@@ -258,7 +260,7 @@ def __contains__ (self, item):
258260 return self .startIp <= item <= self .endIp
259261 #end __contains__
260262
261- def __getitem__ (self , index ):
263+ def __getitem__ (self , index ):
262264 """
263265 >>> r = IpRange('127.0.0.1', '127.255.255.255')
264266 >>> r[0]
@@ -305,8 +307,8 @@ def __getitem__ (self, index):
305307 if stop > self ._len :
306308 raise IndexError ('stop index out of range' )
307309 return IpRange (
308- ipv4 .long2ip (self .startIp + start ),
309- ipv4 .long2ip (self .startIp + stop - 1 ))
310+ ipv4 .long2ip (self .startIp + start ),
311+ ipv4 .long2ip (self .startIp + stop - 1 ))
310312
311313 else :
312314 if index < 0 :
@@ -316,7 +318,7 @@ def __getitem__ (self, index):
316318 return ipv4 .long2ip (self .startIp + index )
317319 #end __getitem__
318320
319- def __iter__ (self ):
321+ def __iter__ (self ):
320322 """
321323 Return an iterator over ip addresses in the range.
322324
@@ -338,6 +340,7 @@ def __iter__ (self):
338340 #end __iter__
339341#end class IpRange
340342
343+
341344class IpRangeList (object ):
342345 """
343346 List of IpRange objects.
@@ -350,7 +353,8 @@ class IpRangeList (object):
350353 a smart object which allows CIDR notation.
351354
352355
353- >>> INTERNAL_IPS = IpRangeList('127.0.0.1','10/8',('192.168.0.1','192.168.255.255'))
356+ >>> INTERNAL_IPS = IpRangeList(
357+ ... '127.0.0.1','10/8',('192.168.0.1','192.168.255.255'))
354358 >>> '127.0.0.1' in INTERNAL_IPS
355359 True
356360 >>> '10.10.10.10' in INTERNAL_IPS
@@ -366,29 +370,32 @@ class IpRangeList (object):
366370 format.
367371 :type \*args: list of str and/or tuple
368372 """
369- def __init__ (self , * args ):
373+ def __init__ (self , * args ):
370374 self .ips = tuple (map (IpRange , args ))
371375 #end __init__
372376
373- def __repr__ (self ):
377+ def __repr__ (self ):
374378 """
375379 >>> repr(IpRangeList('127.0.0.1', '10/8', '192.168/16'))
376380 "IpRangeList(IpRange('127.0.0.1', '127.0.0.1'), IpRange('10.0.0.0', '10.255.255.255'), IpRange('192.168.0.0', '192.168.255.255'))"
377- >>> repr(IpRangeList(IpRange('127.0.0.1', '127.0.0.1'), IpRange('10.0.0.0', '10.255.255.255'), IpRange('192.168.0.0', '192.168.255.255')))
381+ >>> repr(
382+ ... IpRangeList(IpRange('127.0.0.1', '127.0.0.1'),
383+ ... IpRange('10.0.0.0', '10.255.255.255'),
384+ ... IpRange('192.168.0.0', '192.168.255.255')))
378385 "IpRangeList(IpRange('127.0.0.1', '127.0.0.1'), IpRange('10.0.0.0', '10.255.255.255'), IpRange('192.168.0.0', '192.168.255.255'))"
379386 """
380387 return "IpRangeList%r" % (self .ips ,)
381388 #end __repr__
382389
383- def __str__ (self ):
390+ def __str__ (self ):
384391 """
385392 >>> str(IpRangeList('127.0.0.1', '10/8', '192.168/16'))
386393 "(('127.0.0.1', '127.0.0.1'), ('10.0.0.0', '10.255.255.255'), ('192.168.0.0', '192.168.255.255'))"
387394 """
388395 return "(%s)" % ", " .join (str (i ) for i in self .ips )
389396 #end __str__
390397
391- def __contains__ (self , item ):
398+ def __contains__ (self , item ):
392399 """
393400 Implements membership test operators ``in`` and ``not in`` for the
394401 address ranges contained in the list.
@@ -417,7 +424,7 @@ def __contains__ (self, item):
417424 return False
418425 #end __contains__
419426
420- def __iter__ (self ):
427+ def __iter__ (self ):
421428 """
422429 Return an iterator over all ip addresses in the list.
423430
@@ -445,7 +452,7 @@ def __iter__ (self):
445452 yield ip
446453 #end __iter__
447454
448- def __len__ (self ):
455+ def __len__ (self ):
449456 """
450457 Return the length of all ranges in the list.
451458
0 commit comments