From 0c13e1261dffe71588a80f0af3f702cea194efc1 Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Mon, 28 Feb 2022 15:50:37 -0300 Subject: [PATCH] 18574: Adds 'preferred_label' virtual attribute. Avoids storing the voc twice. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- sdk/python/arvados/vocabulary.py | 8 ++++++-- sdk/python/tests/test_vocabulary.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sdk/python/arvados/vocabulary.py b/sdk/python/arvados/vocabulary.py index 643e167e6b..1791566b58 100644 --- a/sdk/python/arvados/vocabulary.py +++ b/sdk/python/arvados/vocabulary.py @@ -15,8 +15,7 @@ def load_vocabulary(api_client=api('v1')): class Vocabulary(object): def __init__(self, voc_definition={}): - self._definition = voc_definition - self.strict_keys = self._definition.get('strict_tags', False) + self.strict_keys = voc_definition.get('strict_tags', False) self.key_aliases = {} for key_id, val in voc_definition.get('tags', {}).items(): @@ -39,6 +38,11 @@ class VocabularyData(object): self.identifier = identifier self.aliases = aliases + def __getattribute__(self, name): + if name == 'preferred_label': + return self.aliases[0] + return super(VocabularyData, self).__getattribute__(name) + class VocabularyValue(VocabularyData): def __init__(self, identifier, aliases=[]): super(VocabularyValue, self).__init__(identifier, aliases) diff --git a/sdk/python/tests/test_vocabulary.py b/sdk/python/tests/test_vocabulary.py index 49fd601262..ccaa7fe88d 100644 --- a/sdk/python/tests/test_vocabulary.py +++ b/sdk/python/tests/test_vocabulary.py @@ -74,14 +74,18 @@ class VocabularyTest(unittest.TestCase): self.assertEqual(vk.strict, False) self.assertEqual(vk.identifier, 'IDTAGANIMALS') self.assertEqual(vk.aliases, ['Animal', 'Creature']) + self.assertEqual(vk.preferred_label, 'Animal') vv = vk.value_aliases['human'] self.assertEqual(vv.identifier, 'IDVALANIMAL1') self.assertEqual(vv.aliases, ['Human', 'Homo sapiens']) + self.assertEqual(vv.preferred_label, 'Human') self.assertEqual(voc['creature']['human'].identifier, vv.identifier) self.assertEqual(voc['Creature']['Human'].identifier, vv.identifier) self.assertEqual(voc['CREATURE']['HUMAN'].identifier, vv.identifier) + with self.assertRaises(KeyError): + inexistant = voc['foo'] def test_empty_vocabulary(self): voc = vocabulary.Vocabulary() -- 2.30.2