diff options
30 files changed, 65 insertions, 53 deletions
diff --git a/HACKING.rst b/HACKING.rst index 35493e55b..54e3b3275 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -29,6 +29,18 @@ General mylist = Foo().list() # OKAY, does not shadow built-in +- 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 ------- - Do not import objects, only modules (*) diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 48b0f632f..8628d0852 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -280,7 +280,7 @@ class CloudController(object): host_services = {} for service in enabled_services: zone_hosts.setdefault(service['availability_zone'], []) - if not service['host'] in zone_hosts[service['availability_zone']]: + if service['host'] not in zone_hosts[service['availability_zone']]: zone_hosts[service['availability_zone']].append( service['host']) @@ -527,7 +527,7 @@ class CloudController(object): def _rule_args_to_dict(self, context, kwargs): rules = [] - if not 'groups' in kwargs and not 'ip_ranges' in kwargs: + if 'groups' not in kwargs and 'ip_ranges' not in kwargs: rule = self._rule_dict_last_step(context, **kwargs) if rule: rules.append(rule) @@ -1545,11 +1545,11 @@ class CloudController(object): if attribute != 'launchPermission': raise exception.EC2APIError(_('attribute not supported: %s') % attribute) - if not 'user_group' in kwargs: + if 'user_group' not in kwargs: raise exception.EC2APIError(_('user or group not specified')) if len(kwargs['user_group']) != 1 and kwargs['user_group'][0] != 'all': raise exception.EC2APIError(_('only group "all" is supported')) - if not operation_type in ['add', 'remove']: + if operation_type not in ['add', 'remove']: msg = _('operation_type must be add or remove') raise exception.EC2APIError(msg) LOG.audit(_("Updating image %s publicity"), image_id, context=context) diff --git a/nova/api/openstack/__init__.py b/nova/api/openstack/__init__.py index d812cef18..a76b74324 100644 --- a/nova/api/openstack/__init__.py +++ b/nova/api/openstack/__init__.py @@ -102,7 +102,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'] diff --git a/nova/api/openstack/compute/contrib/aggregates.py b/nova/api/openstack/compute/contrib/aggregates.py index 91d138be4..84b0358a3 100644 --- a/nova/api/openstack/compute/contrib/aggregates.py +++ b/nova/api/openstack/compute/contrib/aggregates.py @@ -106,7 +106,7 @@ class AggregateController(object): raise exc.HTTPBadRequest for key in updates.keys(): - if not key in ["name", "availability_zone"]: + if key not in ["name", "availability_zone"]: raise exc.HTTPBadRequest try: diff --git a/nova/api/openstack/compute/contrib/availability_zone.py b/nova/api/openstack/compute/contrib/availability_zone.py index 6cde5ca64..98c508bd7 100644 --- a/nova/api/openstack/compute/contrib/availability_zone.py +++ b/nova/api/openstack/compute/contrib/availability_zone.py @@ -110,7 +110,7 @@ class AvailabilityZoneController(wsgi.Controller): host_services = {} for service in enabled_services: zone_hosts.setdefault(service['availability_zone'], []) - if not service['host'] in zone_hosts[service['availability_zone']]: + if service['host'] not in zone_hosts[service['availability_zone']]: zone_hosts[service['availability_zone']].append( service['host']) diff --git a/nova/api/openstack/compute/contrib/flavorextraspecs.py b/nova/api/openstack/compute/contrib/flavorextraspecs.py index 84f157b6a..12cc7d9ed 100644 --- a/nova/api/openstack/compute/contrib/flavorextraspecs.py +++ b/nova/api/openstack/compute/contrib/flavorextraspecs.py @@ -84,7 +84,7 @@ class FlavorExtraSpecsController(object): context = req.environ['nova.context'] authorize(context) self._check_body(body) - if not id in body: + if id not in body: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) if len(body) > 1: diff --git a/nova/api/openstack/compute/contrib/floating_ips_bulk.py b/nova/api/openstack/compute/contrib/floating_ips_bulk.py index f5b8d24dd..db506a15d 100644 --- a/nova/api/openstack/compute/contrib/floating_ips_bulk.py +++ b/nova/api/openstack/compute/contrib/floating_ips_bulk.py @@ -80,13 +80,13 @@ class FloatingIPBulkController(object): context = req.environ['nova.context'] authorize(context) - if not 'floating_ips_bulk_create' in body: + if 'floating_ips_bulk_create' not in body: raise webob.exc.HTTPUnprocessableEntity() params = body['floating_ips_bulk_create'] LOG.debug(params) - if not 'ip_range' in params: + if 'ip_range' not in params: raise webob.exc.HTTPUnprocessableEntity() ip_range = params['ip_range'] diff --git a/nova/api/openstack/compute/contrib/simple_tenant_usage.py b/nova/api/openstack/compute/contrib/simple_tenant_usage.py index 8502e93c4..2313c00ac 100644 --- a/nova/api/openstack/compute/contrib/simple_tenant_usage.py +++ b/nova/api/openstack/compute/contrib/simple_tenant_usage.py @@ -159,7 +159,7 @@ class SimpleTenantUsageController(object): info['uptime'] = delta.days * 24 * 3600 + delta.seconds - if not info['tenant_id'] in rval: + if info['tenant_id'] not in rval: summary = {} summary['tenant_id'] = info['tenant_id'] if detailed: diff --git a/nova/api/openstack/compute/image_metadata.py b/nova/api/openstack/compute/image_metadata.py index 1a467f3a7..7e78d6324 100644 --- a/nova/api/openstack/compute/image_metadata.py +++ b/nova/api/openstack/compute/image_metadata.py @@ -76,7 +76,7 @@ class Controller(object): expl = _('Incorrect request body format') raise exc.HTTPBadRequest(explanation=expl) - if not id in meta: + if id not in meta: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) if len(meta) > 1: @@ -105,7 +105,7 @@ class Controller(object): def delete(self, req, image_id, id): context = req.environ['nova.context'] image = self._get_image(context, image_id) - if not id in image['properties']: + if id not in image['properties']: msg = _("Invalid metadata key") raise exc.HTTPNotFound(explanation=msg) image['properties'].pop(id) diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 90d4c37b3..ac4ebd293 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -751,7 +751,7 @@ class Controller(wsgi.Controller): server_dict = body['server'] password = self._get_server_admin_password(server_dict) - if not 'name' in server_dict: + if 'name' not in server_dict: msg = _("Server name is not defined") raise exc.HTTPBadRequest(explanation=msg) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index a6f255081..f68eff2a7 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -150,7 +150,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 content_type = self.content_type diff --git a/nova/availability_zones.py b/nova/availability_zones.py index 711eee1fa..97faccc9f 100644 --- a/nova/availability_zones.py +++ b/nova/availability_zones.py @@ -71,7 +71,7 @@ def get_availability_zones(context): available_zones = [] for zone in [service['availability_zone'] for service in enabled_services]: - if not zone in available_zones: + if zone not in available_zones: available_zones.append(zone) not_available_zones = [] diff --git a/nova/compute/vm_mode.py b/nova/compute/vm_mode.py index 26e5ad8a0..cc1ca6978 100644 --- a/nova/compute/vm_mode.py +++ b/nova/compute/vm_mode.py @@ -52,7 +52,7 @@ def get_from_instance(instance): if mode == "hv": mode = HVM - if not mode in ALL: + if mode not in ALL: raise exception.Invalid("Unknown vm mode '%s'" % mode) return mode diff --git a/nova/scheduler/filters/trusted_filter.py b/nova/scheduler/filters/trusted_filter.py index 302d2b3a8..14f1a37b0 100644 --- a/nova/scheduler/filters/trusted_filter.py +++ b/nova/scheduler/filters/trusted_filter.py @@ -269,7 +269,7 @@ class ComputeAttestationCache(object): def get_host_attestation(self, host): """Check host's trust level.""" - if not host in self.compute_nodes: + if host not in self.compute_nodes: self._init_cache_entry(host) if not self._cache_valid(host): self._update_cache() diff --git a/nova/service.py b/nova/service.py index 2daceba80..3d556a202 100644 --- a/nova/service.py +++ b/nova/service.py @@ -621,7 +621,7 @@ class WSGIService(object): """ fl = '%s_manager' % self.name - if not fl in CONF: + if fl not in CONF: return None manager_class_name = CONF.get(fl, None) diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index 03fc87ac5..857644498 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -372,7 +372,7 @@ def create_info_cache(nw_cache): 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] diff --git a/nova/tests/compute/test_resource_tracker.py b/nova/tests/compute/test_resource_tracker.py index 53d92a13f..eaa0df5bf 100644 --- a/nova/tests/compute/test_resource_tracker.py +++ b/nova/tests/compute/test_resource_tracker.py @@ -391,7 +391,7 @@ class BaseTrackerTestCase(BaseTestCase): if tracker is None: tracker = self.tracker - if not field in tracker.compute_node: + if field not in tracker.compute_node: raise test.TestingException( "'%(field)s' not in compute node." % locals()) x = tracker.compute_node[field] diff --git a/nova/tests/fakeguestfs.py b/nova/tests/fakeguestfs.py index 33ca49c33..ff006db68 100644 --- a/nova/tests/fakeguestfs.py +++ b/nova/tests/fakeguestfs.py @@ -50,7 +50,7 @@ class GuestFS(object): self.mounts.append((options, device, mntpoint)) def mkdir_p(self, path): - if not path in self.files: + if path not in self.files: self.files[path] = { "isdir": True, "gid": 100, @@ -59,7 +59,7 @@ class GuestFS(object): } def read_file(self, path): - if not path in self.files: + if path not in self.files: self.files[path] = { "isdir": False, "content": "Hello World", @@ -71,7 +71,7 @@ class GuestFS(object): return self.files[path]["content"] def write(self, path, content): - if not path in self.files: + if path not in self.files: self.files[path] = { "isdir": False, "content": "Hello World", @@ -83,7 +83,7 @@ class GuestFS(object): self.files[path]["content"] = content def write_append(self, path, content): - if not path in self.files: + if path not in self.files: self.files[path] = { "isdir": False, "content": "Hello World", @@ -95,13 +95,13 @@ class GuestFS(object): self.files[path]["content"] = self.files[path]["content"] + content def stat(self, path): - if not path in self.files: + if path not in self.files: raise RuntimeError("No such file: " + path) return self.files[path]["mode"] def chown(self, uid, gid, path): - if not path in self.files: + if path not in self.files: raise RuntimeError("No such file: " + path) if uid != -1: @@ -110,7 +110,7 @@ class GuestFS(object): self.files[path]["gid"] = gid def chmod(self, mode, path): - if not path in self.files: + if path not in self.files: raise RuntimeError("No such file: " + path) self.files[path]["mode"] = mode diff --git a/nova/tests/integrated/api/client.py b/nova/tests/integrated/api/client.py index a072b3128..958a5500b 100644 --- a/nova/tests/integrated/api/client.py +++ b/nova/tests/integrated/api/client.py @@ -155,7 +155,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: diff --git a/nova/tests/integrated/integrated_helpers.py b/nova/tests/integrated/integrated_helpers.py index f17dc025f..90e9a806e 100644 --- a/nova/tests/integrated/integrated_helpers.py +++ b/nova/tests/integrated/integrated_helpers.py @@ -58,7 +58,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) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 72c886529..6e7986b0e 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -3848,7 +3848,7 @@ class IptablesFirewallTestCase(test.TestCase): in_rules = filter(lambda l: not l.startswith('#'), self.in_rules) for rule in in_rules: - if not 'nova' in rule: + if 'nova' not in rule: self.assertTrue(rule in self.out_rules, 'Rule went missing: %s' % rule) diff --git a/nova/tests/test_virt_disk_vfs_localfs.py b/nova/tests/test_virt_disk_vfs_localfs.py index af4571dd2..096a75964 100644 --- a/nova/tests/test_virt_disk_vfs_localfs.py +++ b/nova/tests/test_virt_disk_vfs_localfs.py @@ -46,7 +46,7 @@ def fake_execute(*args, **kwargs): elif args[0] == "chown": owner = args[1] path = args[2] - if not path in files: + if path not in files: raise Exception("No such file: " + path) sep = owner.find(':') @@ -72,7 +72,7 @@ def fake_execute(*args, **kwargs): elif args[0] == "chgrp": group = args[1] path = args[2] - if not path in files: + if path not in files: raise Exception("No such file: " + path) if group == "users": @@ -83,13 +83,13 @@ def fake_execute(*args, **kwargs): elif args[0] == "chmod": mode = args[1] path = args[2] - if not path in files: + if path not in files: raise Exception("No such file: " + path) files[path]["mode"] = int(mode, 8) elif args[0] == "cat": path = args[1] - if not path in files: + if path not in files: files[path] = { "content": "Hello World", "gid": 100, @@ -104,7 +104,7 @@ def fake_execute(*args, **kwargs): else: path = args[1] append = False - if not path in files: + if path not in files: files[path] = { "content": "Hello World", "gid": 100, diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index aa640810b..c480d5c5f 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -595,7 +595,7 @@ class XenAPIVMTestCase(stubs.XenAPITestBase): def _check_vdis(self, start_list, end_list): for vdi_ref in end_list: - if not vdi_ref in start_list: + if vdi_ref not in start_list: vdi_rec = xenapi_fake.get_record('VDI', vdi_ref) # If the cache is turned on then the base disk will be # there even after the cleanup @@ -1949,7 +1949,7 @@ class XenAPIDom0IptablesFirewallTestCase(stubs.XenAPITestBase): in_rules = filter(lambda l: not l.startswith('#'), self._in_rules) for rule in in_rules: - if not 'nova' in rule: + if 'nova' not in rule: self.assertTrue(rule in self._out_rules, 'Rule went missing: %s' % rule) diff --git a/nova/virt/baremetal/driver.py b/nova/virt/baremetal/driver.py index 9904fdcd4..631a9a8c4 100644 --- a/nova/virt/baremetal/driver.py +++ b/nova/virt/baremetal/driver.py @@ -140,7 +140,7 @@ class BareMetalDriver(driver.ComputeDriver): keyval[0] = keyval[0].strip() keyval[1] = keyval[1].strip() extra_specs[keyval[0]] = keyval[1] - if not 'cpu_arch' in extra_specs: + if 'cpu_arch' not in extra_specs: LOG.warning( _('cpu_arch is not found in instance_type_extra_specs')) extra_specs['cpu_arch'] = '' diff --git a/nova/virt/baremetal/volume_driver.py b/nova/virt/baremetal/volume_driver.py index 0a05dfedd..e92325b97 100644 --- a/nova/virt/baremetal/volume_driver.py +++ b/nova/virt/baremetal/volume_driver.py @@ -210,7 +210,7 @@ class LibvirtVolumeDriver(VolumeDriver): def _volume_driver_method(self, method_name, connection_info, *args, **kwargs): driver_type = connection_info.get('driver_volume_type') - if not driver_type in self.volume_drivers: + if driver_type not in self.volume_drivers: raise exception.VolumeDriverNotFound(driver_type=driver_type) driver = self.volume_drivers[driver_type] method = getattr(driver, method_name) diff --git a/nova/virt/fake.py b/nova/virt/fake.py index 04eeded72..5a5bb7b13 100644 --- a/nova/virt/fake.py +++ b/nova/virt/fake.py @@ -125,7 +125,7 @@ class FakeDriver(driver.ComputeDriver): self.instances[name] = fake_instance def snapshot(self, context, instance, name, update_task_state): - if not instance['name'] in self.instances: + if instance['name'] not in self.instances: raise exception.InstanceNotRunning(instance_id=instance['uuid']) update_task_state(task_state=task_states.IMAGE_UPLOADING) @@ -209,7 +209,7 @@ class FakeDriver(driver.ComputeDriver): def attach_volume(self, connection_info, instance, mountpoint): """Attach the disk to the instance at mountpoint using info.""" instance_name = instance['name'] - if not instance_name in self._mounts: + if instance_name not in self._mounts: self._mounts[instance_name] = {} self._mounts[instance_name][mountpoint] = connection_info return True diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 9ed7a054c..585074f1f 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -673,7 +673,7 @@ class LibvirtDriver(driver.ComputeDriver): def volume_driver_method(self, method_name, connection_info, *args, **kwargs): driver_type = connection_info.get('driver_volume_type') - if not driver_type in self.volume_drivers: + if driver_type not in self.volume_drivers: raise exception.VolumeDriverNotFound(driver_type=driver_type) driver = self.volume_drivers[driver_type] method = getattr(driver, method_name) diff --git a/nova/virt/libvirt/imagecache.py b/nova/virt/libvirt/imagecache.py index 8f677b482..3609205ce 100644 --- a/nova/virt/libvirt/imagecache.py +++ b/nova/virt/libvirt/imagecache.py @@ -305,7 +305,7 @@ class ImageCacheManager(object): backing_path = os.path.join(CONF.instances_path, CONF.base_dir_name, backing_file) - if not backing_path in inuse_images: + if backing_path not in inuse_images: inuse_images.append(backing_path) if backing_path in self.unexplained_images: @@ -555,7 +555,7 @@ class ImageCacheManager(object): # Elements remaining in unexplained_images might be in use inuse_backing_images = self._list_backing_images() for backing_path in inuse_backing_images: - if not backing_path in self.active_base_files: + if backing_path not in self.active_base_files: self.active_base_files.append(backing_path) # Anything left is an unknown base image diff --git a/nova/virt/xenapi/fake.py b/nova/virt/xenapi/fake.py index 666e46754..bdadfbc38 100644 --- a/nova/virt/xenapi/fake.py +++ b/nova/virt/xenapi/fake.py @@ -89,7 +89,7 @@ def reset(): def reset_table(table): - if not table in _CLASSES: + if table not in _CLASSES: return _db_content[table] = {} @@ -417,7 +417,7 @@ class SessionBase(object): def VBD_add_to_other_config(self, _1, vbd_ref, key, value): db_ref = _db_content['VBD'][vbd_ref] - if not 'other_config' in db_ref: + if 'other_config' not in db_ref: db_ref['other_config'] = {} if key in db_ref['other_config']: raise Failure(['MAP_DUPLICATE_KEY', 'VBD', 'other_config', @@ -426,7 +426,7 @@ class SessionBase(object): def VBD_get_other_config(self, _1, vbd_ref): db_ref = _db_content['VBD'][vbd_ref] - if not 'other_config' in db_ref: + if 'other_config' not in db_ref: return {} return db_ref['other_config'] @@ -497,14 +497,14 @@ class SessionBase(object): def VM_remove_from_xenstore_data(self, _1, vm_ref, key): db_ref = _db_content['VM'][vm_ref] - if not 'xenstore_data' in db_ref: + if 'xenstore_data' not in db_ref: return if key in db_ref['xenstore_data']: del db_ref['xenstore_data'][key] def VM_add_to_xenstore_data(self, _1, vm_ref, key, value): db_ref = _db_content['VM'][vm_ref] - if not 'xenstore_data' in db_ref: + if 'xenstore_data' not in db_ref: db_ref['xenstore_data'] = {} db_ref['xenstore_data'][key] = value @@ -513,14 +513,14 @@ class SessionBase(object): def VDI_remove_from_other_config(self, _1, vdi_ref, key): db_ref = _db_content['VDI'][vdi_ref] - if not 'other_config' in db_ref: + if 'other_config' not in db_ref: return if key in db_ref['other_config']: del db_ref['other_config'][key] def VDI_add_to_other_config(self, _1, vdi_ref, key, value): db_ref = _db_content['VDI'][vdi_ref] - if not 'other_config' in db_ref: + if 'other_config' not in db_ref: db_ref['other_config'] = {} if key in db_ref['other_config']: raise Failure(['MAP_DUPLICATE_KEY', 'VDI', 'other_config', diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index fe4ce9409..6c4c3d4bc 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -1560,7 +1560,7 @@ def _find_iso_sr(session): if not sr_rec['content_type'] == 'iso': LOG.debug(_("ISO: not iso content")) continue - if not 'i18n-key' in sr_rec['other_config']: + if 'i18n-key' not in sr_rec['other_config']: LOG.debug(_("ISO: iso content_type, no 'i18n-key' key")) continue if not sr_rec['other_config']['i18n-key'] == 'local-storage-iso': |