Drop all the calls to .keys() when iterating on the keys of a dict
authorPierre-Yves Chibon <pingou@pingoured.fr>
Tue, 28 Jul 2015 11:19:49 +0000 (11:19 +0000)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Tue, 11 Aug 2015 10:08:50 +0000 (12:08 +0200)
When browsing the keys of a dictionary, you can use the ``.keys()`` method but
that is in fact only really useful if you want to store the list of keys first
and act on them (like sorting them or so).
If you just want to iterate through all the keys, no matter the order, then it
is much much faster to just do: ``for key in dict``

Some stats about this can be found there:
http://blog.pingoured.fr/index.php?post/2012/03/12/Python-notes-to-self

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
Reviewed-by: Simo Sorce <simo@redhat.com>
ipsilon/install/ipsilon-client-install
ipsilon/install/ipsilon-server-install
ipsilon/providers/saml2/rest.py
ipsilon/util/data.py
ipsilon/util/policy.py
tests/helpers/common.py
tests/testmapping.py
tests/testnameid.py

index 452c7e0..2c6df8e 100755 (executable)
@@ -281,7 +281,7 @@ def parse_config_profile(args):
             if g in globals():
                 globals()[g] = val
             else:
-                for k in globals().keys():
+                for k in globals():
                     if k.lower() == g.lower():
                         globals()[k] = val
                         break
index 6c7c6dd..80bd274 100755 (executable)
@@ -313,7 +313,7 @@ def parse_config_profile(args):
             if g in globals():
                 globals()[g] = val
             else:
-                for k in globals().keys():
+                for k in globals():
                     if k.lower() == g.lower():
                         globals()[k] = val
                         break
index 6887ba8..c332bf9 100644 (file)
@@ -70,7 +70,7 @@ class SPS(RestProviderBase):
         else:
             data = idp.get_data()
 
-        for idval in data.keys():
+        for idval in data:
             result = dict(provider=data[idval].get('name'),
                           metadata=data[idval].get('metadata'),)
             results.append(result)
index e0cd6e1..3c116bb 100644 (file)
@@ -23,7 +23,7 @@ class SqlStore(Log):
 
     @classmethod
     def get_connection(cls, name):
-        if name not in cls.__instances.keys():
+        if name not in cls.__instances:
             if cherrypy.config.get('db.conn.log', False):
                 logging.debug('SqlStore new: %s', name)
             cls.__instances[name] = SqlStore(name)
index 8f70673..f1915cc 100644 (file)
@@ -77,7 +77,7 @@ class Policy(Log):
         # If ignore_case is True,
         # then PD translates case insensitively prefixes
         PD = dict()
-        for k in attributes.keys():
+        for k in attributes:
             if ignore_case:
                 # note duplicates that differ only by case
                 # will be lost here, beware!
index cbd516b..eadfdc3 100755 (executable)
@@ -101,10 +101,10 @@ class IpsilonTestBase(object):
                          nameid='unspecified'):
         newconf = ConfigParser.ConfigParser()
         newconf.add_section('globals')
-        for k in global_opts.keys():
+        for k in global_opts:
             newconf.set('globals', k, global_opts[k])
         newconf.add_section('arguments')
-        for k in args_opts.keys():
+        for k in args_opts:
             newconf.set('arguments', k, args_opts[k])
 
         profile = io.BytesIO()
index 6c3ae7d..64a31cd 100755 (executable)
@@ -85,7 +85,7 @@ def check_info_plugin(s, idp_name, urlbase, expected):
     data.pop('MELLON_IDP')
     data.pop('MELLON_NAME_ID')
 
-    for key in expected.keys():
+    for key in expected:
         item = data.pop('MELLON_' + key)
         if item != expected[key]:
             raise ValueError('Expected %s, got %s' % (expected[key], item))
index 4121e11..ffbbeac 100755 (executable)
@@ -55,7 +55,7 @@ def generate_sp_list():
     splist = []
     spport = 45081
 
-    for nameid in SAML2_NAMEID_MAP.keys():
+    for nameid in SAML2_NAMEID_MAP:
         nameid = nameid
         spdata = {'nameid': nameid, 'addr': '127.0.0.11', 'port': str(spport)}
         splist.append(spdata)