Skip to content

Commit 6652981

Browse files
committed
Add more dict-like interface to Tree
Signed-off-by: Cristian Le <cristian.le@mpsd.mpg.de>
1 parent 7fd6d4d commit 6652981

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

fmf/base.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, data: dict[str, Any] | str, name: str |
5858
"""
5959

6060
# Bail out if no data and no parent given
61-
if not data and not parent:
61+
if not data and parent is None:
6262
raise utils.GeneralError(
6363
"No data or parent provided to initialize the tree.")
6464

@@ -822,3 +822,18 @@ def __getitem__(self, key: str) -> Any:
822822
return self.children[key[1:]]
823823
else:
824824
return self.data[key]
825+
826+
def __len__(self) -> int:
827+
return len(self.children) + len(self.data)
828+
829+
def __iter__(self) -> Iterator[Any]:
830+
for c in self.children:
831+
yield f"/{c}"
832+
for d in self.data:
833+
yield d
834+
835+
def __contains__(self, item: str):
836+
if item.startswith("/"):
837+
return item[1:] in self.children
838+
else:
839+
return item in self.data

0 commit comments

Comments
 (0)