python: Use enumerate() builtin function to simplify counted iteration.
authorBen Pfaff <blp@nicira.com>
Tue, 23 Aug 2011 21:43:54 +0000 (14:43 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 24 Aug 2011 19:06:53 +0000 (12:06 -0700)
Suggested-by: Reid Price <reid@nicira.com>
python/ovs/db/data.py
python/ovs/json.py

index 9530fb9cdfc5b873d00a2de0ed9e0caa636ab216..8e2a9d7696cef832b84b5ba36397f37a4be514ce 100644 (file)
@@ -348,11 +348,9 @@ class Datum(object):
         if head:
             s.append(head)
 
-        i = 0
-        for key in sorted(self.values):
+        for i, key in enumerate(sorted(self.values)):
             if i:
                 s.append(", ")
-            i += 1
 
             s.append(key.to_string())
             if self.type.is_map():
@@ -412,18 +410,14 @@ class Datum(object):
         s += ["%s->keys = xmalloc(%d * sizeof *%s->keys);"
               % (var, len(self.values), var)]
 
-        i = 0
-        for key, value in sorted(self.values.items()):
+        for i, key in enumerate(sorted(self.values)):
             s += key.cInitAtom("%s->keys[%d]" % (var, i))
-            i += 1
         
         if self.type.value:
             s += ["%s->values = xmalloc(%d * sizeof *%s->values);"
                   % (var, len(self.values), var)]
-            i = 0
-            for key, value in sorted(self.values.items()):
+            for i, (key, value) in enumerate(sorted(self.values.items())):
                 s += value.cInitAtom("%s->values[%d]" % (var, i))
-                i += 1
         else:
             s += ["%s->values = NULL;" % var]
 
index 97ff1bbdfb2a8f2e522470d9fdf1f99262a09f0d..5e7d8fab9ed8adff45478a284b08379af0f95d7b 100644 (file)
@@ -51,22 +51,18 @@ def to_stream(obj, stream, pretty=False, sort_keys=True):
             items = sorted(obj.items())
         else:
             items = obj.iteritems()
-        i = 0
-        for key, value in items:
+        for i, (key, value) in enumerate(items):
             if i > 0:
                 stream.write(u",")
-            i += 1
             __dump_string(stream, unicode(key))
             stream.write(u":")
             to_stream(value, stream, pretty, sort_keys)
         stream.write(u"}")
     elif type(obj) in (list, tuple):
         stream.write(u"[")
-        i = 0
-        for value in obj:
+        for i, value in enumerate(obj):
             if i > 0:
                 stream.write(u",")
-            i += 1
             to_stream(value, stream, pretty, sort_keys)
         stream.write(u"]")
     else: