[ARVADOS] updated: 2.1.0-1982-ga480283b6

Git user git at public.arvados.org
Tue Mar 1 23:11:19 UTC 2022


Summary of changes:
 sdk/python/arvados/vocabulary.py    | 49 ++++++++++++++--------------
 sdk/python/tests/test_vocabulary.py | 64 +++++++++++++++++++++++++++++++++----
 2 files changed, 82 insertions(+), 31 deletions(-)

       via  a480283b6fe33bd0df12d879c835427e1b71489a (commit)
       via  2b288a6027b06e969461ebfe2104249302bb0a72 (commit)
       via  a38501985bf9540c0f1f9a67a3e491141c387c63 (commit)
      from  13c8401a89d34cc412e76ade8f112a31b9988e4f (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit a480283b6fe33bd0df12d879c835427e1b71489a
Author: Lucas Di Pentima <lucas.dipentima at curii.com>
Date:   Tue Mar 1 20:10:14 2022 -0300

    18574: Fixes value list edge cases (with tests). Also avoids duplicated code.
    
    Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas.dipentima at curii.com>

diff --git a/sdk/python/arvados/vocabulary.py b/sdk/python/arvados/vocabulary.py
index a3344a17b..ae03ef42d 100644
--- a/sdk/python/arvados/vocabulary.py
+++ b/sdk/python/arvados/vocabulary.py
@@ -38,49 +38,42 @@ class Vocabulary(object):
     def convert_to_identifiers(self, obj={}):
         """Translate key/value pairs to machine readable identifiers.
         """
-        if not isinstance(obj, dict):
-            raise ValueError("obj must be a dict")
-        r = {}
-        for k, v in obj.items():
-            k_id, v_id = k, v
-            try:
-                k_id = self[k].identifier
-                try:
-                    if isinstance(v, list):
-                        v_id = [self[k][x].identifier for x in v]
-                    else:
-                        v_id = self[k][v].identifier
-                except KeyError:
-                    if self[k].strict:
-                        raise ValueError("value '%s' not found for key '%s'" % (v, k))
-            except KeyError:
-                if self.strict_keys:
-                    raise KeyError("key '%s' not found" % k)
-            r[k_id] = v_id
-        return r
+        return self._convert_to_what(obj, 'identifier')
 
     def convert_to_labels(self, obj={}):
         """Translate key/value pairs to human readable labels.
         """
+        return self._convert_to_what(obj, 'preferred_label')
+
+    def _convert_to_what(self, obj={}, what=None):
         if not isinstance(obj, dict):
             raise ValueError("obj must be a dict")
+        if what not in ['preferred_label', 'identifier']:
+            raise ValueError("what attr must be 'preferred_label' or 'identifier'")
         r = {}
         for k, v in obj.items():
-            k_lbl, v_lbl = k, v
+            k_what, v_what = k, v
             try:
-                k_lbl = self[k].preferred_label
-                try:
-                    if isinstance(v, list):
-                        v_lbl = [self[k][x].preferred_label for x in v]
-                    else:
-                        v_lbl = self[k][v].preferred_label
-                except KeyError:
-                    if self[k].strict:
-                        raise ValueError("value '%s' not found for key '%s'" % (v, k))
+                k_what = getattr(self[k], what)
+                if isinstance(v, list):
+                    v_what = []
+                    for x in v:
+                        try:
+                            v_what.append(getattr(self[k][x], what))
+                        except KeyError:
+                            if self[k].strict:
+                                raise ValueError("value '%s' not found for key '%s'" % (x, k))
+                            v_what.append(x)
+                else:
+                    try:
+                        v_what = getattr(self[k][v], what)
+                    except KeyError:
+                        if self[k].strict:
+                            raise ValueError("value '%s' not found for key '%s'" % (v, k))
             except KeyError:
                 if self.strict_keys:
                     raise KeyError("key '%s' not found" % k)
-            r[k_lbl] = v_lbl
+            r[k_what] = v_what
         return r
 
 class VocabularyData(object):
diff --git a/sdk/python/tests/test_vocabulary.py b/sdk/python/tests/test_vocabulary.py
index 1d2077782..56647e824 100644
--- a/sdk/python/tests/test_vocabulary.py
+++ b/sdk/python/tests/test_vocabulary.py
@@ -180,6 +180,18 @@ class VocabularyTest(unittest.TestCase):
         with self.assertRaises(ValueError):
             self.voc.convert_to_identifiers({'Priority': 'foo'})
 
+    def test_convert_to_identifiers_unknown_value_list(self):
+        # Non-strict key
+        self.assertEqual(self.voc['animal'].strict, False)
+        self.assertEqual(
+            self.voc.convert_to_identifiers({'Animal': ['foo', 'loxodonta']}),
+            {'IDTAGANIMALS': ['foo', 'IDVALANIMAL2']}
+        )
+        # Strict key
+        self.assertEqual(self.voc['priority'].strict, True)
+        with self.assertRaises(ValueError):
+            self.voc.convert_to_identifiers({'Priority': ['foo', 'bar']})
+
     def test_convert_to_labels(self):
         cases = [
             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
@@ -241,6 +253,18 @@ class VocabularyTest(unittest.TestCase):
         with self.assertRaises(ValueError):
             self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})
 
+    def test_convert_to_labels_unknown_value_list(self):
+        # Non-strict key
+        self.assertEqual(self.voc['animal'].strict, False)
+        self.assertEqual(
+            self.voc.convert_to_labels({'IDTAGANIMALS': ['foo', 'IDVALANIMAL1']}),
+            {'Animal': ['foo', 'Human']}
+        )
+        # Strict key
+        self.assertEqual(self.voc['priority'].strict, True)
+        with self.assertRaises(ValueError):
+            self.voc.convert_to_labels({'IDTAGIMPORTANCES': ['foo', 'bar']})
+
     def test_convert_roundtrip(self):
         initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
         converted = self.voc.convert_to_labels(initial)

commit 2b288a6027b06e969461ebfe2104249302bb0a72
Author: Lucas Di Pentima <lucas.dipentima at curii.com>
Date:   Tue Mar 1 19:40:05 2022 -0300

    18574: Adds support for value list conversion.
    
    Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas.dipentima at curii.com>

diff --git a/sdk/python/arvados/vocabulary.py b/sdk/python/arvados/vocabulary.py
index a8c03ebf3..a3344a17b 100644
--- a/sdk/python/arvados/vocabulary.py
+++ b/sdk/python/arvados/vocabulary.py
@@ -46,7 +46,10 @@ class Vocabulary(object):
             try:
                 k_id = self[k].identifier
                 try:
-                    v_id = self[k][v].identifier
+                    if isinstance(v, list):
+                        v_id = [self[k][x].identifier for x in v]
+                    else:
+                        v_id = self[k][v].identifier
                 except KeyError:
                     if self[k].strict:
                         raise ValueError("value '%s' not found for key '%s'" % (v, k))
@@ -67,7 +70,10 @@ class Vocabulary(object):
             try:
                 k_lbl = self[k].preferred_label
                 try:
-                    v_lbl = self[k][v].preferred_label
+                    if isinstance(v, list):
+                        v_lbl = [self[k][x].preferred_label for x in v]
+                    else:
+                        v_lbl = self[k][v].preferred_label
                 except KeyError:
                     if self[k].strict:
                         raise ValueError("value '%s' not found for key '%s'" % (v, k))

commit a38501985bf9540c0f1f9a67a3e491141c387c63
Author: Lucas Di Pentima <lucas.dipentima at curii.com>
Date:   Tue Mar 1 19:36:10 2022 -0300

    18574: Adds tests for value list conversions.
    
    Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas.dipentima at curii.com>

diff --git a/sdk/python/tests/test_vocabulary.py b/sdk/python/tests/test_vocabulary.py
index 003bd3209..1d2077782 100644
--- a/sdk/python/tests/test_vocabulary.py
+++ b/sdk/python/tests/test_vocabulary.py
@@ -147,6 +147,20 @@ class VocabularyTest(unittest.TestCase):
                 "failing test case: {}".format(case)
             )
 
+    def test_convert_to_identifiers_value_lists(self):
+        cases = [
+            {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
+            {'IDTAGIMPORTANCES': ['High', 'Medium']},
+            {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
+            {'priority': ['high', 'medium']},
+        ]
+        for case in cases:
+            self.assertEqual(
+                self.voc.convert_to_identifiers(case),
+                {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
+                "failing test case: {}".format(case)
+            )
+
     def test_convert_to_identifiers_unknown_key(self):
         # Non-strict vocabulary
         self.assertEqual(self.voc.strict_keys, False)
@@ -194,11 +208,19 @@ class VocabularyTest(unittest.TestCase):
                 "failing test case: {}".format(case)
             )
 
-    def test_convert_roundtrip(self):
-        initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
-        converted = self.voc.convert_to_labels(initial)
-        self.assertNotEqual(converted, initial)
-        self.assertEqual(self.voc.convert_to_identifiers(converted), initial)
+    def test_convert_to_labels_value_lists(self):
+        cases = [
+            {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
+            {'IDTAGIMPORTANCES': ['High', 'Medium']},
+            {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
+            {'priority': ['high', 'medium']},
+        ]
+        for case in cases:
+            self.assertEqual(
+                self.voc.convert_to_labels(case),
+                {'Importance': ['High', 'Medium']},
+                "failing test case: {}".format(case)
+            )
 
     def test_convert_to_labels_unknown_key(self):
         # Non-strict vocabulary
@@ -217,4 +239,10 @@ class VocabularyTest(unittest.TestCase):
         # Strict key
         self.assertEqual(self.voc['priority'].strict, True)
         with self.assertRaises(ValueError):
-            self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})
\ No newline at end of file
+            self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})
+
+    def test_convert_roundtrip(self):
+        initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
+        converted = self.voc.convert_to_labels(initial)
+        self.assertNotEqual(converted, initial)
+        self.assertEqual(self.voc.convert_to_identifiers(converted), initial)

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list