summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoss Brattain <ross.b.brattain@intel.com>2017-08-15 15:24:30 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-08-15 15:24:30 +0000
commit42c7c490703248d571c9c4dc97610155b32d003a (patch)
tree38aa8711d7422e72c54b0183b0fea47c67c39798
parentd9c0b9e1c2e0a11500fa09c10ba559d4944b720c (diff)
parent605a15e8d226b7ecb663ca9a088e544b58060d06 (diff)
Merge "deepgetitem: try string index before int"
-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)