]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fixes 'not in' operator usage
authorZhongyue Luo <zhongyue.nah@intel.com>
Thu, 31 Jan 2013 06:58:50 +0000 (14:58 +0800)
committerZhongyue Luo <zhongyue.nah@intel.com>
Mon, 4 Feb 2013 02:33:43 +0000 (10:33 +0800)
Change-Id: Id4c83e32f6dcb4f710c5a8d8b6f130038cf07648

19 files changed:
HACKING.rst
cinder/api/contrib/types_extra_specs.py
cinder/api/openstack/__init__.py
cinder/api/openstack/wsgi.py
cinder/api/v1/snapshots.py
cinder/api/v1/volumes.py
cinder/api/v2/snapshots.py
cinder/api/v2/volumes.py
cinder/service.py
cinder/tests/api/fakes.py
cinder/tests/integrated/api/client.py
cinder/tests/integrated/integrated_helpers.py
cinder/tests/integrated/test_volumes.py
cinder/tests/test_storwize_svc.py
cinder/tests/windows/mockproxy.py
cinder/volume/drivers/lvm.py
cinder/volume/drivers/netapp.py
cinder/volume/drivers/rbd.py
cinder/volume/drivers/sheepdog.py

index b67867ee99597b91b1aa6af20e80d55f3ac35d4d..eecb286b51fe6d0f2b9ea6f96162e1ee723b9b29 100644 (file)
@@ -27,6 +27,25 @@ General
 
     mylist = Foo().list() # OKAY, does not shadow built-in
 
+- Use the "is not" operator when testing for unequal identities. Example::
+
+    if not X is Y:  # BAD, intended behavior is ambiguous
+        pass
+
+    if X is not Y:  # OKAY, intuitive
+        pass
+
+- Use the "not in" operator for evaluating membership in a collection. Example::
+
+    if not X in Y:  # BAD, intended behavior is ambiguous
+        pass
+
+    if X not in Y:  # OKAY, intuitive
+        pass
+
+    if not (X in Y or X in Z):  # OKAY, still better than all those 'not's
+        pass
+
 
 Imports
 -------
index 5c298d95d4ddfceb49c49da190c13229d492c4fb..e3a4804309bb53233b620e822f54e10f8dff53e3 100644 (file)
@@ -98,7 +98,7 @@ class VolumeTypeExtraSpecsController(wsgi.Controller):
             expl = _('Request body empty')
             raise webob.exc.HTTPBadRequest(explanation=expl)
         self._check_type(context, type_id)
-        if not id in body:
+        if id not in body:
             expl = _('Request body and URI mismatch')
             raise webob.exc.HTTPBadRequest(explanation=expl)
         if len(body) > 1:
index ae2dfeb07179b578723a273e0a5237d9258e63e8..4a6702d9d33a05956062bc64a96510a967f52cde 100644 (file)
@@ -41,7 +41,7 @@ class APIMapper(routes.Mapper):
 
 class ProjectMapper(APIMapper):
     def resource(self, member_name, collection_name, **kwargs):
-        if not ('parent_resource' in kwargs):
+        if 'parent_resource' not in kwargs:
             kwargs['path_prefix'] = '{project_id}/'
         else:
             parent_resource = kwargs['parent_resource']
index 70b9e36a9c44983332e650ecbd9e80547110febb..688fed86f7db48e4712da0e7c13f17dc9b8b08f9 100644 (file)
@@ -89,7 +89,7 @@ class Request(webob.Request):
         Does not do any body introspection, only checks header
 
         """
-        if not "Content-Type" in self.headers:
+        if "Content-Type" not in self.headers:
             return None
 
         allowed_types = SUPPORTED_CONTENT_TYPES
index 9685a095d23c39f64f3e0a9be853e067d6e3f8e1..ac044bacfc9c4debc04b1307c413365b769afb43 100644 (file)
@@ -188,7 +188,7 @@ class SnapshotsController(wsgi.Controller):
         if not body:
             raise exc.HTTPUnprocessableEntity()
 
-        if not 'snapshot' in body:
+        if 'snapshot' not in body:
             raise exc.HTTPUnprocessableEntity()
 
         snapshot = body['snapshot']
index 2e7c8e8d01d1b6c5aa8aa542ba4f8247a0510dda..b695d78d0e93bd9ad248c75a36c63bc4972fbaa3 100644 (file)
@@ -378,7 +378,7 @@ class VolumeController(wsgi.Controller):
         if not body:
             raise exc.HTTPUnprocessableEntity()
 
-        if not 'volume' in body:
+        if 'volume' not in body:
             raise exc.HTTPUnprocessableEntity()
 
         volume = body['volume']
index 5853a0180c8a176af6b14948f9ad7a55faf7ca0d..b8e00d93c4b2752617bd3df6b92b3f4e63fd3c4b 100644 (file)
@@ -188,7 +188,7 @@ class SnapshotsController(wsgi.Controller):
         if not body:
             raise exc.HTTPUnprocessableEntity()
 
-        if not 'snapshot' in body:
+        if 'snapshot' not in body:
             raise exc.HTTPUnprocessableEntity()
 
         snapshot = body['snapshot']
index c1d897fa57a937fcf9d484b6eccd45443c1d47bb..8f53b6905404478e5206b34a7487ce7c7517e0d1 100644 (file)
@@ -302,7 +302,7 @@ class VolumeController(wsgi.Controller):
         if not body:
             raise exc.HTTPUnprocessableEntity()
 
-        if not 'volume' in body:
+        if 'volume' not in body:
             raise exc.HTTPUnprocessableEntity()
 
         volume = body['volume']
index d4fbfc2027cda366e94a4e7c418e4b0ca99a1552..8cbe0c252cfc2aaef20c2a4c0ea71847bc208116 100644 (file)
@@ -351,7 +351,7 @@ class WSGIService(object):
 
         """
         fl = '%s_manager' % self.name
-        if not fl in FLAGS:
+        if fl not in FLAGS:
             return None
 
         manager_class_name = FLAGS.get(fl, None)
index e2bbe21e2353a6ebfe0f163e868ec7b7774ae542..3fd21cfbaf5ee29d35091b944c85b67e85ce4df0 100644 (file)
@@ -185,6 +185,6 @@ class FakeRateLimiter(object):
 
 
 def get_fake_uuid(token=0):
-    if not token in FAKE_UUIDS:
+    if token not in FAKE_UUIDS:
         FAKE_UUIDS[token] = str(uuid.uuid4())
     return FAKE_UUIDS[token]
index 2df9ba912638dadd7d7eb0e0ded5218b1b0027c4..516a2a0e70536ec235f9ee16c82d760652bdbd8a 100644 (file)
@@ -150,7 +150,7 @@ class TestOpenStackClient(object):
         LOG.debug(_("%(relative_uri)s => code %(http_status)s") % locals())
 
         if check_response_status:
-            if not http_status in check_response_status:
+            if http_status not in check_response_status:
                 if http_status == 404:
                     raise OpenStackApiNotFoundException(response=response)
                 elif http_status == 401:
index f7ed84eea28a52ce4b0d362755a569624d55e581..ead8bc1dd671a91c2840ea6547e0286d593c7973 100644 (file)
@@ -51,7 +51,7 @@ def generate_new_element(items, prefix, numeric=False):
             candidate = prefix + generate_random_numeric(8)
         else:
             candidate = prefix + generate_random_alphanumeric(8)
-        if not candidate in items:
+        if candidate not in items:
             return candidate
         LOG.debug("Random collision on %s" % candidate)
 
index e239b9db2088079c383788bcd7a57d6da4d14cfb..1ba3d6ea0ec63b3a2d9603362dfec2abfb5d6cd9 100755 (executable)
@@ -72,7 +72,7 @@ class VolumesTest(integrated_helpers._IntegratedTestBase):
 
             self.assertEqual(volume_id, found_volume['id'])
 
-            if not found_volume['status'] in continue_states:
+            if found_volume['status'] not in continue_states:
                 break
 
             time.sleep(1)
index a2102ce8039f3c0022f112b34f5ff9d5e41d8c51..567dee6424e7dbbca97b9eb66a0834124b4ab5b5 100644 (file)
@@ -391,7 +391,7 @@ class StorwizeSVCManagementSimulator:
             return self._errors["CMMVC5701E"]
         vol_name = kwargs["obj"].strip('\'\"')
 
-        if not vol_name in self._volumes_list:
+        if vol_name not in self._volumes_list:
             return self._errors["CMMVC5753E"]
 
         if force == 0:
@@ -661,10 +661,10 @@ class StorwizeSVCManagementSimulator:
             return self._errors["CMMVC5707E"]
         mapping_info["vol"] = kwargs["obj"].strip('\'\"')
 
-        if not mapping_info["vol"] in self._volumes_list:
+        if mapping_info["vol"] not in self._volumes_list:
             return self._errors["CMMVC5753E"]
 
-        if not mapping_info["host"] in self._hosts_list:
+        if mapping_info["host"] not in self._hosts_list:
             return self._errors["CMMVC5754E"]
 
         if mapping_info["vol"] in self._mappings_list:
@@ -689,7 +689,7 @@ class StorwizeSVCManagementSimulator:
             return self._errors["CMMVC5701E"]
         vol = kwargs["obj"].strip('\'\"')
 
-        if not vol in self._mappings_list:
+        if vol not in self._mappings_list:
             return self._errors["CMMVC5753E"]
 
         if self._mappings_list[vol]["host"] != host:
@@ -729,13 +729,13 @@ class StorwizeSVCManagementSimulator:
         if "source" not in kwargs:
             return self._errors["CMMVC5707E"]
         source = kwargs["source"].strip('\'\"')
-        if not source in self._volumes_list:
+        if source not in self._volumes_list:
             return self._errors["CMMVC5754E"]
 
         if "target" not in kwargs:
             return self._errors["CMMVC5707E"]
         target = kwargs["target"].strip('\'\"')
-        if not target in self._volumes_list:
+        if target not in self._volumes_list:
             return self._errors["CMMVC5754E"]
 
         if source == target:
index a612d470b66ceb53bcb77d70fef1e38be0382b57..d39a96300dfda8c016afc2d669d5c0bdeef5c459 100644 (file)
@@ -164,7 +164,7 @@ class MockProxy(object):
         l.append(val)
 
     def _add_recorded_value(self, name, val):
-        if not name in self._recorded_values:
+        if name not in self._recorded_values:
             self._recorded_values[name] = []
         self._recorded_values[name].append(val)
 
index 181eb6ceeb6d2e8c62096a0d3d2756b66c1154c3..d3dc9a2285c82f479bea3f2ee32370329e56fdb0 100644 (file)
@@ -70,7 +70,7 @@ class LVMVolumeDriver(driver.VolumeDriver):
         out, err = self._execute('vgs', '--noheadings', '-o', 'name',
                                  run_as_root=True)
         volume_groups = out.split()
-        if not FLAGS.volume_group in volume_groups:
+        if FLAGS.volume_group not in volume_groups:
             exception_message = (_("volume group %s doesn't exist")
                                  % FLAGS.volume_group)
             raise exception.VolumeBackendAPIException(data=exception_message)
index f674c3573c178b8b8dde26c0cba7e18c271e9a61..d3d1316a3844bd8ed9821cff27bc104a8e7bc688 100644 (file)
@@ -1306,7 +1306,7 @@ class NetAppCmodeISCSIDriver(driver.ISCSIDriver):
 
     def _get_lun_handle(self, name):
         """Get the details for a LUN from our cache table."""
-        if not name in self.lun_table:
+        if name not in self.lun_table:
             LOG.warn(_("Could not find handle for LUN named %s") % name)
             return None
         return self.lun_table[name].handle
index a4241aa551d336de73a2a20c2912053a3673e641..d5880b4aec83796661420a2383ee59154cf1691d 100644 (file)
@@ -55,7 +55,7 @@ class RBDDriver(driver.VolumeDriver):
         """Returns an error if prerequisites aren't met"""
         (stdout, stderr) = self._execute('rados', 'lspools')
         pools = stdout.split("\n")
-        if not FLAGS.rbd_pool in pools:
+        if FLAGS.rbd_pool not in pools:
             exception_message = (_("rbd has no pool %s") %
                                  FLAGS.rbd_pool)
             raise exception.VolumeBackendAPIException(data=exception_message)
index 175686332f213a644d744a90916676352f4565c4..9fb3904823939c83293b2e1a725f9b10a3f77d24 100644 (file)
@@ -37,7 +37,7 @@ class SheepdogDriver(driver.VolumeDriver):
             #  gives short output, but for compatibility reason we won't
             #  use it and just check if 'running' is in the output.
             (out, err) = self._execute('collie', 'cluster', 'info')
-            if not 'running' in out.split():
+            if 'running' not in out.split():
                 exception_message = (_("Sheepdog is not working: %s") % out)
                 raise exception.VolumeBackendAPIException(
                     data=exception_message)