From: Fredrik Tolf Date: Mon, 8 Jul 2024 00:44:02 +0000 (+0200) Subject: Add env-binding function decorator. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=f55d762c640d8074bdbc3da390bc7040c997ee5b;p=wrw.git Add env-binding function decorator. --- 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