summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoss Brattain <ross.b.brattain@intel.com>2017-08-14 20:30:39 -0700
committerRoss Brattain <ross.b.brattain@intel.com>2017-08-15 05:13:38 +0000
commit605a15e8d226b7ecb663ca9a088e544b58060d06 (patch)
tree88546df449be77d0d51289f9ae6edbb4db4a0662
parent0e21cfca2c8cd9cf9d06480d0f6e0b8e7535808d (diff)
deepgetitem: try string index before int
Change-Id: I3b353c5887db0ebbe33e37db505e72b85167b54e Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
-rw-r--r--tests/unit/network_services/vnf_generic/test_vnfdgen.py13
-rw-r--r--yardstick/network_services/vnf_generic/vnfdgen.py13
2 files changed, 22 insertions, 4 deletions
diff --git a/tests/unit/network_services/vnf_generic/test_vnfdgen.py b/tests/unit/network_services/vnf_generic/test_vnfdgen.py
index be51e4a43..44d9058dc 100644
--- a/tests/unit/network_services/vnf_generic/test_vnfdgen.py
+++ b/tests/unit/network_services/vnf_generic/test_vnfdgen.py
@@ -193,6 +193,14 @@ class TestVnfdGen(unittest.TestCase):
d = {'a': 1, 'b': 2}
self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
+ def test_dict_flatten_str_int_key_first(self):
+ d = {'0': 1, 0: 24, 'b': 2}
+ self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)
+
+ def test_dict_flatten_int_key_fallback(self):
+ d = {0: 1, 'b': 2}
+ self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)
+
def test_dict_flatten_list(self):
d = {'a': 1, 'b': list(range(2))}
self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)
@@ -201,6 +209,11 @@ class TestVnfdGen(unittest.TestCase):
d = {'a': 1, 'b': {x: x for x in list(range(2))}}
self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)
+ def test_dict_flatten_only_str_key(self):
+ d = {'0': 1, 0: 24, 'b': 2}
+ self.assertRaises(AttributeError, vnfdgen.deepgetitem, d, 0)
+
+
def test_generate_tp_single_var(self):
""" Function to verify traffic profile generation with imix """
diff --git a/yardstick/network_services/vnf_generic/vnfdgen.py b/yardstick/network_services/vnf_generic/vnfdgen.py
index 0120b493e..474403dee 100644
--- a/yardstick/network_services/vnf_generic/vnfdgen.py
+++ b/yardstick/network_services/vnf_generic/vnfdgen.py
@@ -73,19 +73,24 @@ def deepgetitem(obj, item, default=None):
add try_int to work with sequences
- >>> d = {'snl_final': {'about': {'_icsd': {'icsd_id': 1, 'fr': [2, 3]}}}}
+ >>> d = {'snl_final': {'about': {'_icsd': {'icsd_id': 1, 'fr': [2, 3], '0': 24, 0: 4}}}}
>>> deepgetitem(d, 'snl_final.about._icsd.icsd_id')
1
>>> deepgetitem(d, 'snl_final.about._sandbox.sbx_id')
>>>
>>> deepgetitem(d, 'snl_final.about._icsd.fr.1')
3
+ >>> deepgetitem(d, 'snl_final.about._icsd.0')
+ 24
"""
def getitem(obj, name):
- # if integer then list index
- name = try_int(name)
+ # try string then convert to int
try:
return obj[name]
except (KeyError, TypeError, IndexError):
- return default
+ name = try_int(name)
+ try:
+ return obj[name]
+ except (KeyError, TypeError, IndexError):
+ return default
return reduce(getitem, item.split('.'), obj)