Skip to content

Commit

Permalink
Add get() to Inventory.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 586037474
  • Loading branch information
isingoo authored and copybara-github committed Nov 28, 2023
1 parent a5f450c commit 27199b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions nisaba/scripts/natural_translit/utils/inventory2.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ def make_supp(self, alias: str, value: ...) -> None:
"""Adds the value as a supplement."""
if self._add_field(alias, value):
self.supp_aliases.append(alias)

def get(self, alias: str, default: ... = ty.MISSING) -> ...:
if alias in self.item_aliases or alias in self.supp_aliases:
return log.dbg_return(
getattr(self, alias, default), 'for alias ' + alias
)
return default
12 changes: 12 additions & 0 deletions nisaba/scripts/natural_translit/utils/inventory2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,17 @@ def test_add_item_recurring_item(self):
_i2.add_item(_T_CB_STR, 'value')
self.assertLen(_i2, 2)

def test_get_item(self):
self.assertEqual(_i2.get('b'), _B_STR)

def test_get_supp(self):
self.assertEqual(_i2.get('d'), _D_INT)

def test_get_out_of_inventory(self):
self.assertEqual(_i2.get('x'), ty.MISSING)

def test_get_out_of_inventory_default(self):
self.assertIsNone(_i2.get('x', None))

if __name__ == '__main__':
absltest.main()

0 comments on commit 27199b1

Please sign in to comment.