From ce2bbec3f2a010cfa26363a91a6224efe484f06f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Tue, 28 Jul 2015 11:19:49 +0000 Subject: [PATCH] Drop all the calls to .keys() when iterating on the keys of a dict 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 Reviewed-by: Simo Sorce --- ipsilon/install/ipsilon-client-install | 2 +- ipsilon/install/ipsilon-server-install | 2 +- ipsilon/providers/saml2/rest.py | 2 +- ipsilon/util/data.py | 2 +- ipsilon/util/policy.py | 2 +- tests/helpers/common.py | 4 ++-- tests/testmapping.py | 2 +- tests/testnameid.py | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ipsilon/install/ipsilon-client-install b/ipsilon/install/ipsilon-client-install index 452c7e0..2c6df8e 100755 --- a/ipsilon/install/ipsilon-client-install +++ b/ipsilon/install/ipsilon-client-install @@ -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 diff --git a/ipsilon/install/ipsilon-server-install b/ipsilon/install/ipsilon-server-install index 6c7c6dd..80bd274 100755 --- a/ipsilon/install/ipsilon-server-install +++ b/ipsilon/install/ipsilon-server-install @@ -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 diff --git a/ipsilon/providers/saml2/rest.py b/ipsilon/providers/saml2/rest.py index 6887ba8..c332bf9 100644 --- a/ipsilon/providers/saml2/rest.py +++ b/ipsilon/providers/saml2/rest.py @@ -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) diff --git a/ipsilon/util/data.py b/ipsilon/util/data.py index e0cd6e1..3c116bb 100644 --- a/ipsilon/util/data.py +++ b/ipsilon/util/data.py @@ -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) diff --git a/ipsilon/util/policy.py b/ipsilon/util/policy.py index 8f70673..f1915cc 100644 --- a/ipsilon/util/policy.py +++ b/ipsilon/util/policy.py @@ -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! diff --git a/tests/helpers/common.py b/tests/helpers/common.py index cbd516b..eadfdc3 100755 --- a/tests/helpers/common.py +++ b/tests/helpers/common.py @@ -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() diff --git a/tests/testmapping.py b/tests/testmapping.py index 6c3ae7d..64a31cd 100755 --- a/tests/testmapping.py +++ b/tests/testmapping.py @@ -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)) diff --git a/tests/testnameid.py b/tests/testnameid.py index 4121e11..ffbbeac 100755 --- a/tests/testnameid.py +++ b/tests/testnameid.py @@ -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) -- 2.20.1