Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Core: Add backwards lookup and setdefault to FuncDict
Browse files Browse the repository at this point in the history
  • Loading branch information
mawildoer committed Sep 26, 2024
1 parent 7e2da80 commit 139b3fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/faebryk/libs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,9 @@ def __repr__(self) -> str:
f"{self.__class__.__name__}"
f"({repr(list(self.items()))}, hasher={repr(self._hasher)})"
)

def backwards_lookup(self, item: U) -> T:
for key, value in self.items():
if value == item:
return key
raise KeyError(item)
15 changes: 15 additions & 0 deletions test/libs/test_util_func_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ def test_func_dict_hash_collision():
assert a[2] == "b"


def test_func_dict_backwards_lookup():
a = FuncDict()
a[1] = "a"
a[2] = "b"
assert a.backwards_lookup("a") == 1
assert a.backwards_lookup("b") == 2


def test_func_dict_setdefault():
a = FuncDict()
assert 1 not in a
assert a.setdefault(1, "a") == "a"
assert a[1] == "a"


def test_func_set_contains():
a = FuncSet([1, 2, 3, FuncDict, FuncSet])
assert 1 in a
Expand Down

0 comments on commit 139b3fc

Please sign in to comment.