def tuple_(self): x, y, z = 11, 22, 33 def mod(a): a[0][1].a = B(y) return a[0], B(x), bind(a[2]).b.set(z) b = List(B(B(0)), B(B(1)), B(B(2))) a = A(b) l1 = lens.GetAttr('b') l2 = lens.GetAttr('b')[0].GetAttr('a') l3 = lens.GetAttr('b')[2] l = lens.Tuple(l1, l2, l3) target = A(List(B(B(x)), B(B(y)), B(B(2), z))) l.modify(mod)(a).should.equal(target)
def _ast_lens(phi, pred, focus_lens): if pred(phi): yield from focus_lens(phi) if phi is None or not phi.children: return if isinstance(phi, Until): child_lenses = [lens.GetAttr('arg1'), lens.GetAttr('arg2')] elif isinstance(phi, NaryOpSTL): child_lenses = [ lens.GetAttr('args')[j] for j, _ in enumerate(phi.args) ] else: child_lenses = [lens.GetAttr('arg')] for l in child_lenses: yield from [l & cl for cl in _ast_lens(l.get()(phi), pred, focus_lens)]
import operator as op from functools import reduce from typing import Iterable, NamedTuple from lenses import lens bot_lens = lens.intervals.Each().bot top_lens = lens.intervals.Each().top intervals_lens = lens.GetAttr('intervals') class Interval(NamedTuple): bot: float top: float def __contains__(self, x): if isinstance(x, Interval): return self.bot <= x.bot and x.top <= self.top return self.bot <= x <= self.top def __and__(self, i2): bot, top = max(i2.bot, self.bot), min(i2.top, self.top) if bot > top: return None return Interval(bot, top) def __or__(self, i2): bot, top = min(i2.bot, self.bot), max(i2.top, self.top) return Interval(bot, top) @property
def focus_lens(leaf): candidates = [lens.const] if isinstance(leaf, LinEq) else [ lens.GetAttr('interval')[0], lens.GetAttr('interval')[1] ] return (x for x in candidates if isinstance(x.get()(leaf), Param))