From 8dd0176878ad1ae38e14e09149bf9dcd812c5ce7 Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Mon, 28 Feb 2022 15:39:49 -0300 Subject: [PATCH] 18574: Fixes key_aliases/value_aliases indexing. Adds dict-style indexing. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- sdk/python/arvados/vocabulary.py | 21 +++++++++++++++++---- sdk/python/tests/test_vocabulary.py | 24 ++++++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/sdk/python/arvados/vocabulary.py b/sdk/python/arvados/vocabulary.py index 8d89746ca9..643e167e6b 100644 --- a/sdk/python/arvados/vocabulary.py +++ b/sdk/python/arvados/vocabulary.py @@ -26,12 +26,18 @@ class Vocabulary(object): for v_id, v_val in val.get('values', {}).items(): labels = [l['label'] for l in v_val.get('labels', [])] values[v_id] = VocabularyValue(v_id, labels) - self.key_aliases[key_id] = VocabularyKey(key_id, key_labels, values, strict) + vk = VocabularyKey(key_id, key_labels, values, strict) + self.key_aliases[key_id] = vk + for alias in vk.aliases: + self.key_aliases[alias.lower()] = vk + + def __getitem__(self, key): + return self.key_aliases[key.lower()] class VocabularyData(object): def __init__(self, identifier, aliases=[]): self.identifier = identifier - self.aliases = set([x.lower() for x in aliases]) + self.aliases = aliases class VocabularyValue(VocabularyData): def __init__(self, identifier, aliases=[]): @@ -40,5 +46,12 @@ class VocabularyValue(VocabularyData): class VocabularyKey(VocabularyData): def __init__(self, identifier, aliases=[], values={}, strict=False): super(VocabularyKey, self).__init__(identifier, aliases) - self.values = values - self.strict = strict \ No newline at end of file + self.strict = strict + self.value_aliases = {} + for v_id, v_val in values.items(): + self.value_aliases[v_id] = v_val + for v_alias in v_val.aliases: + self.value_aliases[v_alias.lower()] = v_val + + def __getitem__(self, key): + return self.value_aliases[key.lower()] \ No newline at end of file diff --git a/sdk/python/tests/test_vocabulary.py b/sdk/python/tests/test_vocabulary.py index 7aea129aff..49fd601262 100644 --- a/sdk/python/tests/test_vocabulary.py +++ b/sdk/python/tests/test_vocabulary.py @@ -64,19 +64,31 @@ class VocabularyTest(unittest.TestCase): def perform_vocabulary_tests(self, voc): self.assertEqual(voc.strict_keys, False) - self.assertEqual(voc.key_aliases.keys(), set(['IDTAGANIMALS', 'IDTAGIMPORTANCE'])) + self.assertEqual( + voc.key_aliases.keys(), + set(['IDTAGANIMALS', 'creature', 'animal', + 'IDTAGIMPORTANCE', 'importance', 'priority']) + ) - self.assertEqual(voc.key_aliases['IDTAGANIMALS'].strict, False) - self.assertEqual(set(voc.key_aliases['IDTAGANIMALS'].aliases), set(['animal', 'creature'])) - self.assertEqual(voc.key_aliases['IDTAGANIMALS'].values.keys(), set(['IDVALANIMAL1', 'IDVALANIMAL2'])) - self.assertEqual(voc.key_aliases['IDTAGANIMALS'].values['IDVALANIMAL1'].aliases, set(['human', 'homo sapiens'])) + vk = voc.key_aliases['creature'] + self.assertEqual(vk.strict, False) + self.assertEqual(vk.identifier, 'IDTAGANIMALS') + self.assertEqual(vk.aliases, ['Animal', 'Creature']) + + vv = vk.value_aliases['human'] + self.assertEqual(vv.identifier, 'IDVALANIMAL1') + self.assertEqual(vv.aliases, ['Human', 'Homo sapiens']) + + self.assertEqual(voc['creature']['human'].identifier, vv.identifier) + self.assertEqual(voc['Creature']['Human'].identifier, vv.identifier) + self.assertEqual(voc['CREATURE']['HUMAN'].identifier, vv.identifier) def test_empty_vocabulary(self): voc = vocabulary.Vocabulary() self.assertEqual(voc.strict_keys, False) self.assertEqual(voc.key_aliases, {}) - def test_load_vocabulary(self): + def test_vocabulary_explicit_instantiation(self): voc = vocabulary.Vocabulary(self.EXAMPLE_VOC) self.perform_vocabulary_tests(voc) -- 2.30.2