From d7be5c1982893d2a43b6554add42240ab040e0d1 Mon Sep 17 00:00:00 2001
From: Zhongyue Luo <zhongyue.nah@intel.com>
Date: Thu, 31 Jan 2013 14:18:12 +0800
Subject: [PATCH] Fixes 'not in' operator usage

Fixes bug #1111254

Change-Id: I297829049213732217332a969aa107525ac13600
---
 HACKING.rst                                 | 10 ++++++++++
 glance/api/v1/images.py                     | 10 +++++-----
 glance/api/v2/image_tags.py                 |  2 +-
 glance/api/v2/images.py                     |  2 +-
 glance/common/wsgi.py                       |  2 +-
 glance/db/sqlalchemy/api.py                 |  2 +-
 glance/tests/unit/test_glance_replicator.py |  2 +-
 glance/tests/unit/test_swift_store.py       |  2 +-
 8 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/HACKING.rst b/HACKING.rst
index f0cdf86d9e..afd861c84f 100644
--- a/HACKING.rst
+++ b/HACKING.rst
@@ -13,6 +13,16 @@ General
 - Do not write "except:", use "except Exception:" at the very least
 - Include your name with TODOs as in "#TODO(termie)"
 - Do not name anything the same name as a built-in or reserved word
+- Use the "not in" operator for collection membership evaluation. Example::
+
+    if not X in Y:  # BAD, hard to understand
+        pass
+
+    if X not in Y:  # OKAY, intuitive
+        pass
+
+    if not (X in Y or X is Z):  # OKAY, still better than all those 'not's
+        pass
 
 
 Imports
diff --git a/glance/api/v1/images.py b/glance/api/v1/images.py
index 7faf184832..1d0519c4ca 100644
--- a/glance/api/v1/images.py
+++ b/glance/api/v1/images.py
@@ -68,12 +68,12 @@ def validate_image_meta(req, values):
     container_format = values.get('container_format')
 
     if 'disk_format' in values:
-        if not disk_format in DISK_FORMATS:
+        if disk_format not in DISK_FORMATS:
             msg = "Invalid disk format '%s' for image." % disk_format
             raise HTTPBadRequest(explanation=msg, request=req)
 
     if 'container_format' in values:
-        if not container_format in CONTAINER_FORMATS:
+        if container_format not in CONTAINER_FORMATS:
             msg = "Invalid container format '%s' for image." % container_format
             raise HTTPBadRequest(explanation=msg, request=req)
 
@@ -604,11 +604,11 @@ class Controller(controller.BaseController):
     def _validate_image_for_activation(self, req, id, values):
         """Ensures that all required image metadata values are valid."""
         image = self.get_image_meta_or_404(req, id)
-        if not 'disk_format' in values:
+        if 'disk_format' not in values:
             values['disk_format'] = image['disk_format']
-        if not 'container_format' in values:
+        if 'container_format' not in values:
             values['container_format'] = image['container_format']
-        if not 'name' in values:
+        if 'name' not in values:
             values['name'] = image['name']
 
         values = validate_image_meta(req, values)
diff --git a/glance/api/v2/image_tags.py b/glance/api/v2/image_tags.py
index 12f7360edf..c88f5f2117 100644
--- a/glance/api/v2/image_tags.py
+++ b/glance/api/v2/image_tags.py
@@ -54,7 +54,7 @@ class Controller(object):
         image_repo = self.gateway.get_repo(req.context)
         try:
             image = image_repo.get(image_id)
-            if not tag_value in image.tags:
+            if tag_value not in image.tags:
                 raise webob.exc.HTTPNotFound()
             image.tags.remove(tag_value)
             image_repo.save(image)
diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py
index a6eb832e91..7e66cad17f 100644
--- a/glance/api/v2/images.py
+++ b/glance/api/v2/images.py
@@ -187,7 +187,7 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
 
     def _get_request_body(self, request):
         output = super(RequestDeserializer, self).default(request)
-        if not 'body' in output:
+        if 'body' not in output:
             msg = _('Body expected in request.')
             raise webob.exc.HTTPBadRequest(explanation=msg)
         return output['body']
diff --git a/glance/common/wsgi.py b/glance/common/wsgi.py
index a62af7a0a7..7e803a5da6 100644
--- a/glance/common/wsgi.py
+++ b/glance/common/wsgi.py
@@ -445,7 +445,7 @@ class Request(webob.Request):
 
     def get_content_type(self, allowed_content_types):
         """Determine content type of the request body."""
-        if not "Content-Type" in self.headers:
+        if "Content-Type" not in self.headers:
             raise exception.InvalidContentType(content_type=None)
 
         content_type = self.content_type
diff --git a/glance/db/sqlalchemy/api.py b/glance/db/sqlalchemy/api.py
index 62bcbe13b5..f35a0dee71 100644
--- a/glance/db/sqlalchemy/api.py
+++ b/glance/db/sqlalchemy/api.py
@@ -690,7 +690,7 @@ def _set_properties_for_image(context, image_ref, properties,
 
     if purge_props:
         for key in orig_properties.keys():
-            if not key in properties:
+            if key not in properties:
                 prop_ref = orig_properties[key]
                 image_property_delete(context, prop_ref, session=session)
 
diff --git a/glance/tests/unit/test_glance_replicator.py b/glance/tests/unit/test_glance_replicator.py
index 90d8e57ae5..693971f7f9 100644
--- a/glance/tests/unit/test_glance_replicator.py
+++ b/glance/tests/unit/test_glance_replicator.py
@@ -106,7 +106,7 @@ class FakeHTTPConnection(object):
         hkeys.sort()
         hashable = (method, url, body, ' '.join(hkeys))
 
-        if not hashable in self.reqs:
+        if hashable not in self.reqs:
             options = []
             for h in self.reqs:
                 options.append(repr(h))
diff --git a/glance/tests/unit/test_swift_store.py b/glance/tests/unit/test_swift_store.py
index 1ef4feef59..3ea902cb99 100644
--- a/glance/tests/unit/test_swift_store.py
+++ b/glance/tests/unit/test_swift_store.py
@@ -128,7 +128,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
     def fake_get_object(url, token, container, name, **kwargs):
         # GET returns the tuple (list of headers, file object)
         fixture_key = "%s/%s" % (container, name)
-        if not fixture_key in fixture_headers:
+        if fixture_key not in fixture_headers:
             msg = "Object GET failed"
             raise swiftclient.ClientException(msg,
                                               http_status=httplib.NOT_FOUND)