[ovs-dev,16/55] python: Convert dict iterators.
diff mbox

Message ID 1450730875-18083-17-git-send-email-russell@ovn.org
State Deferred
Headers show

Commit Message

Russell Bryant Dec. 21, 2015, 8:47 p.m. UTC
In Python 2, dict.items(), dict.keys(), and dict.values() returned a
list.  dict.iteritems(), dict.iterkeys(), and dict.itervalues() returned
an iterator.

As of Python 3, dict.iteritems(), dict.itervalues(), and dict.iterkeys()
are gone.  items(), keys(), and values() now return an iterator.

In the case where we want an iterator, we now use the six.iter*()
helpers.  If we want a list, we explicitly create a list from the
iterator.

Signed-off-by: Russell Bryant <russell@ovn.org>
---
 python/ovs/db/data.py   | 18 ++++++++++--------
 python/ovs/db/idl.py    | 34 ++++++++++++++++++----------------
 python/ovs/db/schema.py | 18 ++++++++++--------
 python/ovs/json.py      |  3 ++-
 python/ovs/poller.py    |  2 +-
 python/ovs/stream.py    |  4 +++-
 python/ovs/vlog.py      |  9 +++++----
 7 files changed, 49 insertions(+), 39 deletions(-)

Patch
diff mbox

diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py
index 6baff38..cd535d4 100644
--- a/python/ovs/db/data.py
+++ b/python/ovs/db/data.py
@@ -15,6 +15,8 @@ 
 import re
 import uuid
 
+import six
+
 import ovs.poller
 import ovs.socket_util
 import ovs.json
@@ -293,7 +295,7 @@  class Datum(object):
         This function is not commonly useful because the most ordinary way to
         obtain a datum is ultimately via Datum.from_json() or Atom.from_json(),
         which check constraints themselves."""
-        for keyAtom, valueAtom in self.values.iteritems():
+        for keyAtom, valueAtom in six.iteritems(self.values):
             keyAtom.check_constraints(self.type.key)
             if valueAtom is not None:
                 valueAtom.check_constraints(self.type.value)
@@ -354,7 +356,7 @@  class Datum(object):
             return ["map", [[k.to_json(), v.to_json()]
                             for k, v in sorted(self.values.items())]]
         elif len(self.values) == 1:
-            key = self.values.keys()[0]
+            key = list(self.values.keys())[0]
             return key.to_json()
         else:
             return ["set", [k.to_json() for k in sorted(self.values.keys())]]
@@ -388,9 +390,9 @@  class Datum(object):
 
     def as_list(self):
         if self.type.is_map():
-            return [[k.value, v.value] for k, v in self.values.iteritems()]
+            return [[k.value, v.value] for k, v in six.iteritems(self.values)]
         else:
-            return [k.value for k in self.values.iterkeys()]
+            return [k.value for k in six.iterkeys(self.values)]
 
     def as_dict(self):
         return dict(self.values)
@@ -398,10 +400,10 @@  class Datum(object):
     def as_scalar(self):
         if len(self.values) == 1:
             if self.type.is_map():
-                k, v = self.values.iteritems()[0]
+                k, v = list(six.iteritems(self.values))[0]
                 return [k.value, v.value]
             else:
-                return self.values.keys()[0].value
+                return list(self.values.keys())[0].value
         else:
             return None
 
@@ -448,7 +450,7 @@  class Datum(object):
                 return value
         elif self.type.is_map():
             value = {}
-            for k, v in self.values.iteritems():
+            for k, v in six.iteritems(self.values):
                 dk = uuid_to_row(k.value, self.type.key)
                 dv = uuid_to_row(v.value, self.type.value)
                 if dk is not None and dv is not None:
@@ -476,7 +478,7 @@  class Datum(object):
         'type_'."""
         d = {}
         if type(value) == dict:
-            for k, v in value.iteritems():
+            for k, v in six.iteritems(value):
                 ka = Atom.from_python(type_.key, row_to_uuid(k))
                 va = Atom.from_python(type_.value, row_to_uuid(v))
                 d[ka] = va
diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py
index c50766c..02950d3 100644
--- a/python/ovs/db/idl.py
+++ b/python/ovs/db/idl.py
@@ -14,6 +14,8 @@ 
 
 import uuid
 
+import six
+
 import ovs.jsonrpc
 import ovs.db.parser
 import ovs.db.schema
@@ -124,8 +126,8 @@  class Idl(object):
         self.txn = None
         self._outstanding_txns = {}
 
-        for table in schema.tables.itervalues():
-            for column in table.columns.itervalues():
+        for table in six.itervalues(schema.tables):
+            for column in six.itervalues(table.columns):
                 if not hasattr(column, 'alert'):
                     column.alert = True
             table.need_table = False
@@ -283,7 +285,7 @@  class Idl(object):
     def __clear(self):
         changed = False
 
-        for table in self.tables.itervalues():
+        for table in six.itervalues(self.tables):
             if table.rows:
                 changed = True
                 table.rows = {}
@@ -338,9 +340,9 @@  class Idl(object):
 
     def __send_monitor_request(self):
         monitor_requests = {}
-        for table in self.tables.itervalues():
+        for table in six.itervalues(self.tables):
             columns = []
-            for column in table.columns.keys():
+            for column in six.iterkeys(table.columns):
                 if ((table.name not in self.readonly) or
                     (table.name in self.readonly) and
                     (column not in self.readonly[table.name])):
@@ -363,7 +365,7 @@  class Idl(object):
             raise error.Error("<table-updates> is not an object",
                               table_updates)
 
-        for table_name, table_update in table_updates.iteritems():
+        for table_name, table_update in six.iteritems(table_updates):
             table = self.tables.get(table_name)
             if not table:
                 raise error.Error('<table-updates> includes unknown '
@@ -373,7 +375,7 @@  class Idl(object):
                 raise error.Error('<table-update> for table "%s" is not '
                                   'an object' % table_name, table_update)
 
-            for uuid_string, row_update in table_update.iteritems():
+            for uuid_string, row_update in six.iteritems(table_update):
                 if not ovs.ovsuuid.is_valid_string(uuid_string):
                     raise error.Error('<table-update> for table "%s" '
                                       'contains bad UUID "%s" as member '
@@ -441,7 +443,7 @@  class Idl(object):
 
     def __row_update(self, table, row, row_json):
         changed = False
-        for column_name, datum_json in row_json.iteritems():
+        for column_name, datum_json in six.iteritems(row_json):
             column = table.columns.get(column_name)
             if not column:
                 # XXX rate-limit
@@ -469,7 +471,7 @@  class Idl(object):
 
     def __create_row(self, table, uuid):
         data = {}
-        for column in table.columns.itervalues():
+        for column in six.itervalues(table.columns):
             data[column.name] = ovs.db.data.Datum.default(column.type)
         row = table.rows[uuid] = Row(self, table, uuid, data)
         return row
@@ -610,7 +612,7 @@  class Row(object):
     @classmethod
     def from_json(cls, idl, table, uuid, row_json):
         data = {}
-        for column_name, datum_json in row_json.iteritems():
+        for column_name, datum_json in six.iteritems(row_json):
             column = table.columns.get(column_name)
             if not column:
                 # XXX rate-limit
@@ -831,7 +833,7 @@  class Transaction(object):
     def __disassemble(self):
         self.idl.txn = None
 
-        for row in self._txn_rows.itervalues():
+        for row in six.itervalues(self._txn_rows):
             if row._changes is None:
                 row._table.rows[row.uuid] = row
             elif row._data is None:
@@ -910,7 +912,7 @@  class Transaction(object):
                                "lock": self.idl.lock_name})
 
         # Add prerequisites and declarations of new rows.
-        for row in self._txn_rows.itervalues():
+        for row in six.itervalues(self._txn_rows):
             if row._prereqs:
                 rows = {}
                 columns = []
@@ -927,7 +929,7 @@  class Transaction(object):
 
         # Add updates.
         any_updates = False
-        for row in self._txn_rows.itervalues():
+        for row in six.itervalues(self._txn_rows):
             if row._changes is None:
                 if row._table.is_root:
                     operations.append({"op": "delete",
@@ -953,7 +955,7 @@  class Transaction(object):
                 row_json = {}
                 op["row"] = row_json
 
-                for column_name, datum in row._changes.iteritems():
+                for column_name, datum in six.iteritems(row._changes):
                     if row._data is not None or not datum.is_default():
                         row_json[column_name] = (
                                 self._substitute_uuids(datum.to_json()))
@@ -1181,7 +1183,7 @@  class Transaction(object):
                     else:
                         hard_errors = True
 
-                for insert in self._inserted_rows.itervalues():
+                for insert in six.itervalues(self._inserted_rows):
                     if not self.__process_insert_reply(insert, ops):
                         hard_errors = True
 
@@ -1381,7 +1383,7 @@  class SchemaHelper(object):
 
         if not self._all:
             schema_tables = {}
-            for table, columns in self._tables.iteritems():
+            for table, columns in six.iteritems(self._tables):
                 schema_tables[table] = (
                     self._keep_table_columns(schema, table, columns))
 
diff --git a/python/ovs/db/schema.py b/python/ovs/db/schema.py
index 263907e..399129e 100644
--- a/python/ovs/db/schema.py
+++ b/python/ovs/db/schema.py
@@ -15,6 +15,8 @@ 
 import re
 import sys
 
+import six
+
 from ovs.db import error
 import ovs.db.parser
 import ovs.db.types
@@ -40,7 +42,7 @@  class DbSchema(object):
         # backward compatibility, if the root set is empty then assume that
         # every table is in the root set.
         if self.__root_set_size() == 0:
-            for table in self.tables.itervalues():
+            for table in six.itervalues(self.tables):
                 table.is_root = True
 
         # Find the "ref_table"s referenced by "ref_table_name"s.
@@ -48,15 +50,15 @@  class DbSchema(object):
         # Also force certain columns to be persistent, as explained in
         # __check_ref_table().  This requires 'is_root' to be known, so this
         # must follow the loop updating 'is_root' above.
-        for table in self.tables.itervalues():
-            for column in table.columns.itervalues():
+        for table in six.itervalues(self.tables):
+            for column in six.itervalues(table.columns):
                 self.__follow_ref_table(column, column.type.key, "key")
                 self.__follow_ref_table(column, column.type.value, "value")
 
     def __root_set_size(self):
         """Returns the number of tables in the schema's root set."""
         n_root = 0
-        for table in self.tables.itervalues():
+        for table in six.itervalues(self.tables):
             if table.is_root:
                 n_root += 1
         return n_root
@@ -76,7 +78,7 @@  class DbSchema(object):
                               % version)
 
         tables = {}
-        for tableName, tableJson in tablesJson.iteritems():
+        for tableName, tableJson in six.iteritems(tablesJson):
             _check_id(tableName, json)
             tables[tableName] = TableSchema.from_json(tableJson, tableName)
 
@@ -90,7 +92,7 @@  class DbSchema(object):
         default_is_root = self.__root_set_size() == len(self.tables)
 
         tables = {}
-        for table in self.tables.itervalues():
+        for table in six.itervalues(self.tables):
             tables[table.name] = table.to_json(default_is_root)
         json = {"name": self.name, "tables": tables}
         if self.version:
@@ -191,7 +193,7 @@  class TableSchema(object):
             raise error.Error("table must have at least one column", json)
 
         columns = {}
-        for column_name, column_json in columns_json.iteritems():
+        for column_name, column_json in six.iteritems(columns_json):
             _check_id(column_name, json)
             columns[column_name] = ColumnSchema.from_json(column_json,
                                                           column_name)
@@ -230,7 +232,7 @@  class TableSchema(object):
             json["isRoot"] = self.is_root
 
         json["columns"] = columns = {}
-        for column in self.columns.itervalues():
+        for column in six.itervalues(self.columns):
             if not column.name.startswith("_"):
                 columns[column.name] = column.to_json()
 
diff --git a/python/ovs/json.py b/python/ovs/json.py
index 07fd9c1..d5f9703 100644
--- a/python/ovs/json.py
+++ b/python/ovs/json.py
@@ -16,6 +16,7 @@  import re
 import StringIO
 import sys
 
+import six
 from six.moves import range
 
 __pychecker__ = 'no-stringiter'
@@ -73,7 +74,7 @@  class _Serializer(object):
             if self.sort_keys:
                 items = sorted(obj.items())
             else:
-                items = obj.iteritems()
+                items = six.iteritems(obj)
             for i, (key, value) in enumerate(items):
                 if i > 0:
                     self.stream.write(u",")
diff --git a/python/ovs/poller.py b/python/ovs/poller.py
index 76234dc..20be801 100644
--- a/python/ovs/poller.py
+++ b/python/ovs/poller.py
@@ -85,7 +85,7 @@  class _SelectSelect(object):
             events_dict[fd] = events_dict.get(fd, 0) | (POLLERR |
                                                         POLLHUP |
                                                         POLLNVAL)
-        return events_dict.items()
+        return list(events_dict.items())
 
 
 SelectPoll = _SelectSelect
diff --git a/python/ovs/stream.py b/python/ovs/stream.py
index 42bc4cc..a555a76 100644
--- a/python/ovs/stream.py
+++ b/python/ovs/stream.py
@@ -16,6 +16,8 @@  import errno
 import os
 import socket
 
+import six
+
 import ovs.poller
 import ovs.socket_util
 import ovs.vlog
@@ -58,7 +60,7 @@  class Stream(object):
 
     @staticmethod
     def _find_method(name):
-        for method, cls in Stream._SOCKET_METHODS.items():
+        for method, cls in six.iteritems(Stream._SOCKET_METHODS):
             if name.startswith(method):
                 return cls
         return None
diff --git a/python/ovs/vlog.py b/python/ovs/vlog.py
index 6dcccbb..d164900 100644
--- a/python/ovs/vlog.py
+++ b/python/ovs/vlog.py
@@ -22,6 +22,7 @@  import socket
 import sys
 import threading
 
+import six
 from six.moves import range
 
 import ovs.dirs
@@ -80,7 +81,7 @@  class Vlog(object):
         msg_num = Vlog.__msg_num
         Vlog.__msg_num += 1
 
-        for f, f_level in Vlog.__mfl[self.name].iteritems():
+        for f, f_level in six.iteritems(Vlog.__mfl[self.name]):
             f_level = LEVELS.get(f_level, logging.CRITICAL)
             if level_num >= f_level:
                 msg = self._build_message(message, f, level, msg_num)
@@ -184,7 +185,7 @@  class Vlog(object):
 
     def __is_enabled(self, level):
         level = LEVELS.get(level.lower(), logging.DEBUG)
-        for f, f_level in Vlog.__mfl[self.name].iteritems():
+        for f, f_level in six.iteritems(Vlog.__mfl[self.name]):
             f_level = LEVELS.get(f_level, logging.CRITICAL)
             if level >= f_level:
                 return True
@@ -266,12 +267,12 @@  class Vlog(object):
             return
 
         if module == "any":
-            modules = Vlog.__mfl.keys()
+            modules = list(Vlog.__mfl.keys())
         else:
             modules = [module]
 
         if destination == "any":
-            destinations = DESTINATIONS.keys()
+            destinations = list(DESTINATIONS.keys())
         else:
             destinations = [destination]