File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ def __call__(
2727
2828T = TypeVar ("T" , Any , Any )
2929
30+ no_arg = object ()
31+
3032
3133def 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 ]
You can’t perform that action at this time.
0 commit comments