Skip to content

Commit 4cc7849

Browse files
committed
feat(QueryList): Add .get()
1 parent 91b59dd commit 4cc7849

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libtmux/_internal/query_list.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def __call__(
2727

2828
T = TypeVar("T", Any, Any)
2929

30+
no_arg = object()
31+
3032

3133
def keygetter(
3234
obj: "Mapping[str, Any]",
@@ -347,3 +349,18 @@ def val_match(obj: Union[str, List[Any]]) -> bool:
347349
_filter = filter_lookup
348350

349351
return self.__class__(k for k in self if _filter(k))
352+
353+
def get(
354+
self,
355+
matcher: Optional[Union[Callable[[T], bool], T]] = None,
356+
default: Optional[Any] = no_arg,
357+
**kwargs: Any,
358+
) -> Optional[T]:
359+
objs = self.filter(matcher=matcher, **kwargs)
360+
if len(objs) > 1:
361+
raise Exception("Multiple objects returned")
362+
elif len(objs) == 0:
363+
if default == no_arg:
364+
raise Exception("No objects found")
365+
return default
366+
return objs[0]

0 commit comments

Comments
 (0)