heat: replace dict key checking with .get() 09/29409/1
authorRoss Brattain <ross.b.brattain@intel.com>
Mon, 27 Feb 2017 02:57:46 +0000 (18:57 -0800)
committerRoss Brattain <ross.b.brattain@intel.com>
Mon, 27 Feb 2017 03:34:48 +0000 (19:34 -0800)
We init all the fields to None, so the default value
is None, which is what .get() returns when the key
is not present it the dictionary.

Replace

  if key in dict:
    self.val = dict[key]

with

  self.val = dict.get(key)

This also has the added beneifit of re-initializing
default values to None.

In idiomatic Python we do not check a dictionary twice
if we can avoid it.  Either use dict.get() with a default
value or catch the KeyError and do the correct thing.

Change-Id: If8d1caeb7288f888f02622d62e8db9f59f50717a
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
yardstick/benchmark/contexts/heat.py

index c5c1279..479548b 100644 (file)
@@ -62,22 +62,19 @@ class HeatContext(Context):
         """initializes itself from the supplied arguments"""
         self.name = attrs["name"]
 
-        if "user" in attrs:
-            self._user = attrs["user"]
+        self._user = attrs.get("user")
 
-        if "heat_template" in attrs:
-            self.template_file = attrs["heat_template"]
-            self.heat_parameters = attrs.get("heat_parameters", None)
+        self.template_file = attrs.get("heat_template")
+        if self.template_file:
+            self.heat_parameters = attrs.get("heat_parameters")
             return
 
         self.keypair_name = self.name + "-key"
         self.secgroup_name = self.name + "-secgroup"
 
-        if "image" in attrs:
-            self._image = attrs.get("image")
+        self._image = attrs.get("image")
 
-        if "flavor" in attrs:
-            self._flavor = attrs.get("flavor")
+        self._flavor = attrs.get("flavor")
 
         self.placement_groups = [PlacementGroup(name, self, pgattrs["policy"])
                                  for name, pgattrs in attrs.get(