diff --git a/nisaba/scripts/natural_translit/utils/inventory2.py b/nisaba/scripts/natural_translit/utils/inventory2.py index 8987bb24..21620a05 100644 --- a/nisaba/scripts/natural_translit/utils/inventory2.py +++ b/nisaba/scripts/natural_translit/utils/inventory2.py @@ -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 diff --git a/nisaba/scripts/natural_translit/utils/inventory2_test.py b/nisaba/scripts/natural_translit/utils/inventory2_test.py index 01315f3d..76dee29c 100644 --- a/nisaba/scripts/natural_translit/utils/inventory2_test.py +++ b/nisaba/scripts/natural_translit/utils/inventory2_test.py @@ -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()