]> granicus.if.org Git - python/commitdiff
Suggest how to use compare_networks() with a modern Python.
authorRaymond Hettinger <python@rcn.com>
Thu, 14 May 2009 15:31:02 +0000 (15:31 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 14 May 2009 15:31:02 +0000 (15:31 +0000)
Doc/library/ipaddr.rst

index 1178204d26084d1321f1e5c3ac9b0ffeafe95171..6a10895ea413cf13c0c8ef63020681375fb36752 100644 (file)
@@ -199,6 +199,16 @@ both IPv4 and IPv6.
       1 if self.version > other.version
         eg: IPv6('::1/128') > IPv4('255.255.255.0/24')
 
+      .. note::
+
+         To sort networks with :func:`sorted`, :func:`min`, :func:`max` and
+         other tools with a *key* argument, use the :func:`operator.attrgetter`
+         function to extract the relevant fields::
+
+            >>> from operator import attrgetter
+            >>> s = [IPv6('::1/128'), IPv4('255.255.255.0/24')]
+            >>> sorted(s, key=attrgetter('version', 'network', 'netmask'))
+            [IPv4('255.255.255.0/24'), IPv6('::1/128')]
 
    .. method:: subnet(prefixlen_diff=1)