]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Use "if dict.get(key):" instead "if key in dict and dict[key]:"
authorWei Wang <wangwei@unitedstack.com>
Thu, 7 Aug 2014 08:16:37 +0000 (16:16 +0800)
committerarmando-migliaccio <armamig@gmail.com>
Mon, 12 Jan 2015 22:05:54 +0000 (14:05 -0800)
Use "if dict.get(key):" instead of "if key in dict and dict[key]:"
which makes code more clear and intelligible. Note this patch doesn't
change judging conditions, all "is not None" are retained.

Change-Id: Ieed57a21eb4b08c6f9a25b180a3625154a0d5fde

neutron/db/db_base_plugin_v2.py
neutron/db/l3_db.py
neutron/plugins/vmware/plugins/base.py
neutron/tests/unit/nuage/fake_nuageclient.py
neutron/tests/unit/test_db_plugin.py
neutron/tests/unit/test_l3_plugin.py
neutron/tests/unit/vmware/extensions/test_networkgw.py
neutron/tests/unit/vmware/test_nsx_plugin.py

index d9f56a3efd510b1386056052ce506b29517e58ac..99dbe76b4cbce4261059856b42a77d5345a9e2d0 100644 (file)
@@ -1233,7 +1233,7 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
         s['id'] = db_subnet.id
         self._validate_subnet(context, s, cur_subnet=db_subnet)
 
-        if 'gateway_ip' in s and s['gateway_ip'] is not None:
+        if s.get('gateway_ip') is not None:
             allocation_pools = [{'start': p['first_ip'], 'end': p['last_ip']}
                                 for p in db_subnet.allocation_pools]
             self._validate_gw_out_of_pools(s["gateway_ip"], allocation_pools)
index 6a3d1e118591444ef043414abccc00b9790afd12..0ef0b89e5f6de3116df94ebfb1a2a67906772f70 100644 (file)
@@ -730,7 +730,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase):
             raise n_exc.BadRequest(resource='floatingip', msg=msg)
 
         internal_subnet_id = None
-        if 'fixed_ip_address' in fip and fip['fixed_ip_address']:
+        if fip.get('fixed_ip_address'):
             internal_ip_address = fip['fixed_ip_address']
             for ip in internal_port['fixed_ips']:
                 if ip['ip_address'] == internal_ip_address:
@@ -775,11 +775,10 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase):
 
     def _check_and_get_fip_assoc(self, context, fip, floatingip_db):
         port_id = internal_ip_address = router_id = None
-        if (('fixed_ip_address' in fip and fip['fixed_ip_address']) and
-            not ('port_id' in fip and fip['port_id'])):
+        if fip.get('fixed_ip_address') and not fip.get('port_id'):
             msg = _("fixed_ip_address cannot be specified without a port_id")
             raise n_exc.BadRequest(resource='floatingip', msg=msg)
-        if 'port_id' in fip and fip['port_id']:
+        if fip.get('port_id'):
             port_id, internal_ip_address, router_id = self.get_assoc_data(
                 context,
                 fip,
index 1c81ff451f8a3c0ca2da9540aa6763d970a6e828..4ca8ff11854fcef5120b349cd8b6fe38155aa09c 100644 (file)
@@ -1823,12 +1823,11 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
                                           ips_to_remove=nsx_floating_ips)
 
     def _get_fip_assoc_data(self, context, fip, floatingip_db):
-        if (('fixed_ip_address' in fip and fip['fixed_ip_address']) and
-            not ('port_id' in fip and fip['port_id'])):
+        if fip.get('fixed_ip_address') and not fip.get('port_id'):
             msg = _("fixed_ip_address cannot be specified without a port_id")
             raise n_exc.BadRequest(resource='floatingip', msg=msg)
         port_id = internal_ip = router_id = None
-        if 'port_id' in fip and fip['port_id']:
+        if fip.get('port_id'):
             fip_qry = context.session.query(l3_db.FloatingIP)
             port_id, internal_ip, router_id = self.get_assoc_data(
                 context,
index 463f16438d67ef250270f839c3c20f17f3ef56d9..e8099a6bb041400e12a2504aa480c9532c46ebd3 100644 (file)
@@ -141,7 +141,7 @@ class FakeNuageClient(object):
                 return result
 
     def get_nuage_port_by_id(self, params):
-        if 'nuage_fip_id' in params and params['nuage_fip_id'] == '1':
+        if params.get('nuage_fip_id') == '1':
             domain_id = uuidutils.generate_uuid()
         else:
             if 'nuage_router_id' in params:
index 3bce709d593cd08255730104e95e635b7f476bd1..4fdbe64835068ccd616675c6ab07100ae44c91d5 100644 (file)
@@ -306,7 +306,7 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase,
                     'dns_nameservers', 'host_routes',
                     'shared', 'ipv6_ra_mode', 'ipv6_address_mode'):
             # Arg must be present and not null (but can be false)
-            if arg in kwargs and kwargs[arg] is not None:
+            if kwargs.get(arg) is not None:
                 data['subnet'][arg] = kwargs[arg]
 
         if ('gateway_ip' in kwargs and
index 24f82fec236bf0e35da7604a31d3fec9d476f50c..33a77eae67c5dc10945da23a0066d8e0f926a6c0 100644 (file)
@@ -337,7 +337,7 @@ class L3NatTestCaseMixin(object):
             data['router']['admin_state_up'] = admin_state_up
         for arg in (('admin_state_up', 'tenant_id') + (arg_list or ())):
             # Arg must be present and not empty
-            if arg in kwargs and kwargs[arg]:
+            if kwargs.get(arg):
                 data['router'][arg] = kwargs[arg]
         router_req = self.new_create_request('routers', data, fmt)
         if set_context and tenant_id:
index 58ec9ce8c4441562436298122908d33c3c3b02c2..5aaebbc1776021c5700f0855a7b0e518668a8548 100644 (file)
@@ -372,7 +372,7 @@ class NetworkGatewayDbTestCase(test_db_plugin.NeutronDbPluginV2TestCase):
             data[self.gw_resource]['name'] = name
         for arg in arg_list or ():
             # Arg must be present and not empty
-            if arg in kwargs and kwargs[arg]:
+            if kwargs.get(arg):
                 data[self.gw_resource][arg] = kwargs[arg]
         nw_gw_req = self.new_create_request(networkgw.NETWORK_GATEWAYS,
                                             data, fmt)
index 251ec1256e2240ff699aa5c2cfb9c8adfa2c1ce4..4dd422ba14daa07529dc90392cb246580bf9d11e 100644 (file)
@@ -77,7 +77,7 @@ class NsxPluginV2TestCase(test_plugin.NeutronDbPluginV2TestCase):
         for arg in (('admin_state_up', 'tenant_id', 'shared') +
                     (arg_list or ())):
             # Arg must be present and not empty
-            if arg in kwargs and kwargs[arg]:
+            if kwargs.get(arg):
                 data['network'][arg] = kwargs[arg]
         network_req = self.new_create_request('networks', data, fmt)
         if (kwargs.get('set_context') and 'tenant_id' in kwargs):