From 0ee91a5854830736c8e6ea5d140bcf3ce965fa3c Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Wed, 24 Dec 2014 23:05:48 +0800 Subject: Use dict comprehensions instead of dict constructor PEP-0274 introduced dict comprehensions to replace dict constructor with a sequence of key-value pair, these are benefits copied from [1]: The dictionary constructor approach has two distinct disadvantages from the proposed syntax though. First, it isn't as legible as a dict comprehension. Second, it forces the programmer to create an in-core list object first, which could be expensive. There is deep dive about PEP-0274[2] and basic tests about performance[3]. Note: This commit doesn't handle dict constructor with kwagrs. [1]http://legacy.python.org/dev/peps/pep-0274/ [2]http://doughellmann.com/2012/11/12/the-performance-impact-of-using-dict-instead-of-in-cpython-2-7-2.html [3]http://paste.openstack.org/show/154798/ Change-Id: I45d0c289ecaf63a343fc9ad935cf2893d67d938a --- trove/common/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'trove/common/utils.py') diff --git a/trove/common/utils.py b/trove/common/utils.py index 87071de0..c01d2ad3 100644 --- a/trove/common/utils.py +++ b/trove/common/utils.py @@ -69,14 +69,14 @@ def create_method_args_string(*args, **kwargs): def stringify_keys(dictionary): if dictionary is None: return None - return dict((str(key), value) for key, value in dictionary.iteritems()) + return {str(key): value for key, value in dictionary.iteritems()} def exclude(key_values, *exclude_keys): if key_values is None: return None - return dict((key, value) for key, value in key_values.iteritems() - if key not in exclude_keys) + return {key: value for key, value in key_values.iteritems() + if key not in exclude_keys} def generate_uuid(): -- cgit v1.2.1