Skip to content

Commit

Permalink
Update get_key to support multiple keys (#999)
Browse files Browse the repository at this point in the history
* update get_key

* Update CHANGELOG.md

* Update test_resources.py

* doc string

* doc

---------

Co-authored-by: Ryan Ly <[email protected]>
  • Loading branch information
mavaylon1 and rly authored Dec 9, 2023
1 parent 97260bc commit f95b1ef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Minor Improvements
- Updated `__gather_columns` to ignore the order of bases when generating columns from the super class. @mavaylon1 [#991](https://github.com/hdmf-dev/hdmf/pull/991)
- Update `get_key` to return all the keys if there are multiple within a `HERD` instance. @mavaylon1 [#999](https://github.com/hdmf-dev/hdmf/pull/999)
- Improve HTML rendering of tables. @bendichter [#998](https://github.com/hdmf-dev/hdmf/pull/998)
- Improved issue and PR templates. @rly [#1004](https://github.com/hdmf-dev/hdmf/pull/1004)

Expand Down
5 changes: 3 additions & 2 deletions src/hdmf/common/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ def get_key(self, **kwargs):
If container, relative_path, and field are provided, the Key that corresponds to the given name of the key
for the given container, relative_path, and field is returned.
If there are multiple matches, a list of all matching keys will be returned.
"""
key_name, container, relative_path, field = popargs('key_name', 'container', 'relative_path', 'field', kwargs)
key_idx_matches = self.keys.which(key=key_name)
Expand All @@ -510,8 +512,7 @@ def get_key(self, **kwargs):
# the key has never been used before
raise ValueError("key '%s' does not exist" % key_name)
elif len(key_idx_matches) > 1:
msg = "There are more than one key with that name. Please search with additional information."
raise ValueError(msg)
return [self.keys.row[x] for x in key_idx_matches]
else:
return self.keys.row[key_idx_matches[0]]

Expand Down
10 changes: 6 additions & 4 deletions tests/unit/common/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ class TestHERDGetKey(TestCase):
def setUp(self):
self.er = HERD()

def test_get_key_error_more_info(self):
def test_get_key_multiple(self):
self.er.add_ref(file=HERDManagerContainer(name='file'),
container=Container(name='Container'),
key='key1',
Expand All @@ -1145,9 +1145,11 @@ def test_get_key_error_more_info(self):
entity_id="id12",
entity_uri='url21')

msg = "There are more than one key with that name. Please search with additional information."
with self.assertRaisesWith(ValueError, msg):
_ = self.er.get_key(key_name='key1')
keys = self.er.get_key(key_name='key1')
self.assertIsInstance(keys[0], Key)
self.assertIsInstance(keys[1], Key)
self.assertEqual(keys[0].idx, 0)
self.assertEqual(keys[1].idx, 1)

def test_get_key(self):
self.er.add_ref(file=HERDManagerContainer(name='file'),
Expand Down

0 comments on commit f95b1ef

Please sign in to comment.