From f75088c733adcde15bf936a150244e8c4698858c Mon Sep 17 00:00:00 2001
From: Can Zhang <can@canx.me>
Date: Fri, 12 Apr 2013 15:23:10 +0800
Subject: [PATCH 2/4] ip address presentation convert helper methods

Signed-off-by: Can Zhang <can@canx.me>
---
 ryu/lib/ip.py | 51 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/ryu/lib/ip.py b/ryu/lib/ip.py
index 9077634..8e774d9 100644
--- a/ryu/lib/ip.py
+++ b/ryu/lib/ip.py
@@ -1,57 +1,70 @@
 import struct
 
+
 def ipNum(w, x, y, z):
     """Generate unsigned int from components of IP address
        returns: w << 24 | x << 16 | y << 8 | z"""
-    return ( w << 24 ) | ( x << 16 ) | ( y << 8 ) | z
+    return (w << 24) | (x << 16) | (y << 8) | z
+
 
-# ip eg '192.168.1.1'
 def ipv4_to_bin(ip):
-    "Parse an IP address and return an unsigned int."
-    args = [ int( arg ) for arg in ip.split( '.' ) ]
-    return ipNum( *args )
+    '''
+        Parse an IP address and return an unsigned int.
+        The IP address is in dotted decimal notation.
+    '''
+    args = [int(arg) for arg in ip.split('.')]
+    return ipNum(*args)
+
 
 def ipv4_to_str(ip):
     """Generate IP address string from an unsigned int.
        ip: unsigned int of form w << 24 | x << 16 | y << 8 | z
        returns: ip address string w.x.y.z"""
-    w = ( ip >> 24 ) & 0xff
-    x = ( ip >> 16 ) & 0xff
-    y = ( ip >> 8 ) & 0xff
+    w = (ip >> 24) & 0xff
+    x = (ip >> 16) & 0xff
+    y = (ip >> 8) & 0xff
     z = ip & 0xff
-    return "%i.%i.%i.%i" % ( w, x, y, z )
+    return "%i.%i.%i.%i" % (w, x, y, z)
 
 IPV6_PACK_STR = '!8H'
 
+
 def ipv6_to_arg_list(ipv6):
+    '''
+        convert ipv6 string to a list of 8 different parts
+    '''
     args = []
     if '::' in ipv6:
         h, t = ipv6.split('::')
-        h_list = [ int(x, 16) for x in h.split(':') ]
-        t_list = [ int(x, 16) for x in t.split(':') ]
+        h_list = [int(x, 16) for x in h.split(':')]
+        t_list = [int(x, 16) for x in t.split(':')]
         args += h_list
         zero = [0]
-        args += ( (8 - len(h_list) - len(t_list)) * zero )
+        args += ((8 - len(h_list) - len(t_list)) * zero)
         args += t_list
     else:
-        args = [ int(x, 16) for x in ipv6.split(':') ]
+        args = [int(x, 16) for x in ipv6.split(':')]
 
     return args
 
+
 def ipv6_to_bin(ipv6):
     '''
-        convert ipv6 string to bin 
+        convert ipv6 string to binary representation
     '''
     args = ipv6_to_arg_list(ipv6)
     return struct.pack(IPV6_PACK_STR, *args)
 
+
 def bin_to_ipv6(bin_addr):
+    '''
+        convert binary representation to human readable string
+    '''
     args = struct.unpack_from(IPV6_PACK_STR, bin_addr)
-    return ':'.join( '%x' % x for x in args)
-    
+    return ':'.join('%x' % x for x in args)
+
 if __name__ == '__main__':
     a = ipv4_to_bin('10.28.197.1')
     print ipv4_to_str(a)
-    print bin_to_ipv6( ipv6_to_bin('10::1:2'))
-    print bin_to_ipv6( ipv6_to_bin('2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'))
-
+    print bin_to_ipv6(ipv6_to_bin('0:10::1:2'))
+    print bin_to_ipv6(ipv6_to_bin('2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'))
-- 
1.7.12.4 (Apple Git-37)

