From 3ac5ebe8c64fa0b6d83fb33b384ffebfd00ec58b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 28 2015 11:19:49 +0000 Subject: 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 --- 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 5c1ef70..c7a1d2d 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)