From f55d762c640d8074bdbc3da390bc7040c997ee5b Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 8 Jul 2024 02:44:02 +0200 Subject: [PATCH] Add env-binding function decorator. --- wrw/env.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/wrw/env.py b/wrw/env.py index ed313a3..dd86007 100644 --- a/wrw/env.py +++ b/wrw/env.py @@ -52,7 +52,7 @@ class binding(object): __slots__ = ["bindings"] def __init__(self, bindings): if isinstance(bindings, dict): - bindings = bindings.items() + bindings = list(bindings.items()) self.bindings = bindings def __enter__(self): @@ -84,3 +84,21 @@ class var(object): def binding(self, val): return binding([(self, val)]) + +def boundvars(bindings, dynamic=[]): + if isinstance(bindings, dict): + bindings = list(bindings.items()) + if isinstance(dynamic, dict): + dynamic = list(dynamic.items()) + def dec(fun): + def wrapper(*args, **kwargs): + calc = bindings + if dynamic: + calc = list(calc) + for var, val in dynamic: + calc.append((var, val())) + with binding(bindings): + return fun(*args, **kwargs) + wrapper.__wrapped__ = callable + return wrapper + return dec -- 2.11.0