Skip to content

Commit

Permalink
Add more dict-like interface to Tree
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Aug 9, 2023
1 parent d42a16e commit 059f6d9
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion fmf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, data: dict[str, Any] | str, name: str |
"""

# Bail out if no data and no parent given
if not data and not parent:
if not data and parent is None:
raise utils.GeneralError(
"No data or parent provided to initialize the tree.")

Expand Down Expand Up @@ -822,3 +822,18 @@ def __getitem__(self, key: str) -> Any:
return self.children[key[1:]]
else:
return self.data[key]

def __len__(self) -> int:
return len(self.children) + len(self.data)

def __iter__(self) -> Iterator[Any]:
for c in self.children:
yield f"/{c}"
for d in self.data:
yield d

def __contains__(self, item: str):
if item.startswith("/"):
return item[1:] in self.children
else:
return item in self.data

0 comments on commit 059f6d9

Please sign in to comment.