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__(
27
27
28
28
T = TypeVar ("T" , Any , Any )
29
29
30
+ no_arg = object ()
31
+
30
32
31
33
def keygetter (
32
34
obj : "Mapping[str, Any]" ,
@@ -347,3 +349,18 @@ def val_match(obj: Union[str, List[Any]]) -> bool:
347
349
_filter = filter_lookup
348
350
349
351
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