Skip to content

Commit ee42962

Browse files
authored
add get() support to nodes (#68)
1 parent 87db9b3 commit ee42962

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

opto/trace/nodes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,11 @@ def keys(self):
22112211

22122212
return ops.keys(self)
22132213

2214+
def get(self, key, default=None):
2215+
import opto.trace.operators as ops
2216+
2217+
return ops.get(self, node(key), node(default))
2218+
22142219
def pop(self, __index=-1):
22152220
# python does hidden type checks
22162221
import opto.trace.operators as ops

opto/trace/operators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ def values(x: Dict):
430430
return [k for k in x.values()]
431431

432432

433+
@bundle()
434+
def get(x: Any, key: Any, default: Any = None):
435+
"""Return the value for key if key is in x, else default."""
436+
if not hasattr(x, "get"):
437+
raise AttributeError(f"{type(x)} object has no attribute 'get'.")
438+
return x.get(key, default)
439+
440+
433441
# dict in-place operators
434442

435443

0 commit comments

Comments
 (0)