From: Raymond Hettinger Date: Thu, 14 May 2009 15:31:02 +0000 (+0000) Subject: Suggest how to use compare_networks() with a modern Python. X-Git-Tag: v3.1rc1~159 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9aaef2da4d53341cec20d2b8d72814d3b1503b9e;p=thirdparty%2FPython%2Fcpython.git Suggest how to use compare_networks() with a modern Python. --- diff --git a/Doc/library/ipaddr.rst b/Doc/library/ipaddr.rst index 1178204d2608..6a10895ea413 100644 --- a/Doc/library/ipaddr.rst +++ b/Doc/library/ipaddr.rst @@ -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)