From 16ab5d7c1781626293ec67b57f66dbfcc3cd2176 Mon Sep 17 00:00:00 2001 From: ankitagrawal Date: Wed, 5 Aug 2015 05:20:51 -0700 Subject: [PATCH] Use dictionary literal for dictionary creation Dictionary creation could be rewritten as a dictionary literal. for example: filters = {} filter['namespace'] = namespace could be rewritten as filters = {'namespace': namespace} Closes-Bug: 1482586 Change-Id: I860483e958296da248a579a6032c14370843d9e4 --- glance/api/v1/images.py | 9 ++-- glance/api/v2/metadef_resource_types.py | 6 +-- glance/api/v2/tasks.py | 44 ++++++++++--------- glance/common/auth.py | 10 ++--- glance/common/client.py | 4 +- glance/common/utils.py | 15 ++++--- glance/image_cache/drivers/xattr.py | 4 +- .../v2/test_metadef_resourcetypes.py | 6 +-- ...test_artifact_type_definition_framework.py | 6 +-- glance/tests/unit/test_glance_replicator.py | 9 ++-- glance/tests/unit/utils.py | 9 ++-- glance/tests/unit/v1/test_api.py | 10 +++-- 12 files changed, 65 insertions(+), 67 deletions(-) diff --git a/glance/api/v1/images.py b/glance/api/v1/images.py index 7de66a97d2..b1a8eba00a 100644 --- a/glance/api/v1/images.py +++ b/glance/api/v1/images.py @@ -663,10 +663,11 @@ class Controller(controller.BaseController): :param image_id: Opaque image identifier :param location_data: Location of where Glance stored this image """ - image_meta = {} - image_meta['location'] = location_data['url'] - image_meta['status'] = 'active' - image_meta['location_data'] = [location_data] + image_meta = { + 'location': location_data['url'], + 'status': 'active', + 'location_data': [location_data] + } try: s = from_state diff --git a/glance/api/v2/metadef_resource_types.py b/glance/api/v2/metadef_resource_types.py index 0e3129d7aa..ef64870cb9 100644 --- a/glance/api/v2/metadef_resource_types.py +++ b/glance/api/v2/metadef_resource_types.py @@ -48,8 +48,7 @@ class ResourceTypeController(object): def index(self, req): try: - filters = {} - filters['namespace'] = None + filters = {'namespace': None} rs_type_repo = self.gateway.get_metadef_resource_type_repo( req.context) db_resource_type_list = rs_type_repo.list(filters=filters) @@ -70,8 +69,7 @@ class ResourceTypeController(object): def show(self, req, namespace): try: - filters = {} - filters['namespace'] = namespace + filters = {'namespace': namespace} rs_type_repo = self.gateway.get_metadef_resource_type_repo( req.context) db_resource_type_list = rs_type_repo.list(filters=filters) diff --git a/glance/api/v2/tasks.py b/glance/api/v2/tasks.py index 1a9a7992f0..84a546e806 100644 --- a/glance/api/v2/tasks.py +++ b/glance/api/v2/tasks.py @@ -245,35 +245,37 @@ class ResponseSerializer(wsgi.JSONResponseSerializer): return '/v2/tasks/%s' % task.task_id def _format_task(self, schema, task): - task_view = {} - task_view['id'] = task.task_id - task_view['input'] = task.task_input - task_view['type'] = task.type - task_view['status'] = task.status - task_view['owner'] = task.owner - task_view['message'] = task.message - task_view['result'] = task.result + task_view = { + 'id': task.task_id, + 'input': task.task_input, + 'type': task.type, + 'status': task.status, + 'owner': task.owner, + 'message': task.message, + 'result': task.result, + 'created_at': timeutils.isotime(task.created_at), + 'updated_at': timeutils.isotime(task.updated_at), + 'self': self._get_task_location(task), + 'schema': '/v2/schemas/task' + } if task.expires_at: task_view['expires_at'] = timeutils.isotime(task.expires_at) - task_view['created_at'] = timeutils.isotime(task.created_at) - task_view['updated_at'] = timeutils.isotime(task.updated_at) - task_view['self'] = self._get_task_location(task) - task_view['schema'] = '/v2/schemas/task' task_view = schema.filter(task_view) # domain return task_view def _format_task_stub(self, schema, task): - task_view = {} - task_view['id'] = task.task_id - task_view['type'] = task.type - task_view['status'] = task.status - task_view['owner'] = task.owner + task_view = { + 'id': task.task_id, + 'type': task.type, + 'status': task.status, + 'owner': task.owner, + 'created_at': timeutils.isotime(task.created_at), + 'updated_at': timeutils.isotime(task.updated_at), + 'self': self._get_task_location(task), + 'schema': '/v2/schemas/task' + } if task.expires_at: task_view['expires_at'] = timeutils.isotime(task.expires_at) - task_view['created_at'] = timeutils.isotime(task.created_at) - task_view['updated_at'] = timeutils.isotime(task.updated_at) - task_view['self'] = self._get_task_location(task) - task_view['schema'] = '/v2/schemas/task' task_view = schema.filter(task_view) # domain return task_view diff --git a/glance/common/auth.py b/glance/common/auth.py index caed11b0f6..1c457ce306 100644 --- a/glance/common/auth.py +++ b/glance/common/auth.py @@ -150,9 +150,10 @@ class KeystoneStrategy(BaseStrategy): def _v1_auth(self, token_url): creds = self.creds - headers = {} - headers['X-Auth-User'] = creds['username'] - headers['X-Auth-Key'] = creds['password'] + headers = { + 'X-Auth-User': creds['username'], + 'X-Auth-Key': creds['password'] + } tenant = creds.get('tenant') if tenant: @@ -202,8 +203,7 @@ class KeystoneStrategy(BaseStrategy): } } - headers = {} - headers['Content-Type'] = 'application/json' + headers = {'Content-Type': 'application/json'} req_body = jsonutils.dumps(creds) resp, resp_body = self._do_request( diff --git a/glance/common/client.py b/glance/common/client.py index 3c69388290..31a6b8ff48 100644 --- a/glance/common/client.py +++ b/glance/common/client.py @@ -228,10 +228,8 @@ class BaseClient(object): self.connect_kwargs = self.get_connect_kwargs() def get_connect_kwargs(self): - connect_kwargs = {} - # Both secure and insecure connections have a timeout option - connect_kwargs['timeout'] = self.timeout + connect_kwargs = {'timeout': self.timeout} if self.use_ssl: if self.key_file is None: diff --git a/glance/common/utils.py b/glance/common/utils.py index 03210eb0a1..e91b6c1463 100644 --- a/glance/common/utils.py +++ b/glance/common/utils.py @@ -731,12 +731,13 @@ def stash_conf_values(): Allows determining if any of these values have changed when the config is reloaded. """ - conf = {} - conf['bind_host'] = CONF.bind_host - conf['bind_port'] = CONF.bind_port - conf['tcp_keepidle'] = CONF.cert_file - conf['backlog'] = CONF.backlog - conf['key_file'] = CONF.key_file - conf['cert_file'] = CONF.cert_file + conf = { + 'bind_host': CONF.bind_host, + 'bind_port': CONF.bind_port, + 'tcp_keepidle': CONF.cert_file, + 'backlog': CONF.backlog, + 'key_file': CONF.key_file, + 'cert_file': CONF.cert_file + } return conf diff --git a/glance/image_cache/drivers/xattr.py b/glance/image_cache/drivers/xattr.py index d29e9fc070..38e4bee473 100644 --- a/glance/image_cache/drivers/xattr.py +++ b/glance/image_cache/drivers/xattr.py @@ -152,9 +152,7 @@ class Driver(base.Driver): for path in get_all_regular_files(self.base_dir): image_id = os.path.basename(path) - entry = {} - entry['image_id'] = image_id - + entry = {'image_id': image_id} file_info = os.stat(path) entry['last_modified'] = file_info[stat.ST_MTIME] entry['last_accessed'] = file_info[stat.ST_ATIME] diff --git a/glance/tests/functional/v2/test_metadef_resourcetypes.py b/glance/tests/functional/v2/test_metadef_resourcetypes.py index a3af1bfade..9fd0ececc5 100644 --- a/glance/tests/functional/v2/test_metadef_resourcetypes.py +++ b/glance/tests/functional/v2/test_metadef_resourcetypes.py @@ -48,8 +48,7 @@ class ResourceTypeController(object): def index(self, req): try: - filters = {} - filters['namespace'] = None + filters = {'namespace': None} rs_type_repo = self.gateway.get_metadef_resource_type_repo( req.context) db_resource_type_list = rs_type_repo.list(filters=filters) @@ -68,8 +67,7 @@ class ResourceTypeController(object): def show(self, req, namespace): try: - filters = {} - filters['namespace'] = namespace + filters = {'namespace': namespace} rs_type_repo = self.gateway.get_metadef_resource_type_repo( req.context) db_resource_type_list = rs_type_repo.list(filters=filters) diff --git a/glance/tests/unit/test_artifact_type_definition_framework.py b/glance/tests/unit/test_artifact_type_definition_framework.py index e79187285f..44de4e3471 100644 --- a/glance/tests/unit/test_artifact_type_definition_framework.py +++ b/glance/tests/unit/test_artifact_type_definition_framework.py @@ -461,14 +461,12 @@ class TestDeclarativeProperties(test_utils.BaseTestCase): max_value=99)}) tt = TestType() - tt.props = {"foo": "FOO"} - tt.props["bar"] = False + tt.props = {"foo": "FOO", "bar": False} self.assertRaises(exc.InvalidArtifactPropertyValue, tt.props.__setitem__, "bar", 123) self.assertRaises(exc.InvalidArtifactPropertyValue, tt.props.__setitem__, "extra", "value") - tt.fixed = {"name": "Alex"} - tt.fixed["age"] = 42 + tt.fixed = {"name": "Alex", "age": 42} self.assertRaises(exc.InvalidArtifactPropertyValue, tt.fixed.__setitem__, "age", 120) diff --git a/glance/tests/unit/test_glance_replicator.py b/glance/tests/unit/test_glance_replicator.py index fb59472c0e..f27919a218 100644 --- a/glance/tests/unit/test_glance_replicator.py +++ b/glance/tests/unit/test_glance_replicator.py @@ -211,10 +211,11 @@ class ImageServiceTestCase(test_utils.BaseTestCase): c = glance_replicator.ImageService(FakeHTTPConnection(), 'noauth') image_body = 'THISISANIMAGEBODYFORSURE!' - image_meta_with_proto = {} - image_meta_with_proto['x-auth-token'] = 'noauth' - image_meta_with_proto['Content-Type'] = 'application/octet-stream' - image_meta_with_proto['Content-Length'] = len(image_body) + image_meta_with_proto = { + 'x-auth-token': 'noauth', + 'Content-Type': 'application/octet-stream', + 'Content-Length': len(image_body) + } for key in IMG_RESPONSE_ACTIVE: image_meta_with_proto[ diff --git a/glance/tests/unit/utils.py b/glance/tests/unit/utils.py index a1863cb070..d2d7d6a8b0 100644 --- a/glance/tests/unit/utils.py +++ b/glance/tests/unit/utils.py @@ -229,10 +229,11 @@ class FakeNotifier(object): self.log = [] def _notify(self, event_type, payload, level): - log = {} - log['notification_type'] = level - log['event_type'] = event_type - log['payload'] = payload + log = { + 'notification_type': level, + 'event_type': event_type, + 'payload': payload + } self.log.append(log) def warn(self, event_type, payload): diff --git a/glance/tests/unit/v1/test_api.py b/glance/tests/unit/v1/test_api.py index bb930f6198..d036896e2e 100644 --- a/glance/tests/unit/v1/test_api.py +++ b/glance/tests/unit/v1/test_api.py @@ -1574,10 +1574,12 @@ class TestGlanceAPI(base.IsolatedUnitTest): call_sequence = [] # use this to determine if we are within a db session i.e. atomic # operation, that is setting our active state. - test_status = {'activate_session_started': False} - # We want first status check to be 'queued' so we get past the first - # guard. - test_status['queued_guard_passed'] = False + # We want first status check to be 'queued' so we get past the + # first guard. + test_status = { + 'activate_session_started': False, + 'queued_guard_passed': False + } state_changes = []