front.state¶
-
exception
front.state.
ForbiddenOverwrite
[source]¶ Error to raise when a writes to existing keys are not allowed
-
class
front.state.
GetterSetter
(*args, **kwargs)[source]¶ The type of an object
obj
that has the operationsv = obj[k]
andobj[ k] = v
-
front.state.
mk_binder
(state: Optional[NewType.<locals>.new_type] = None, allowed_ids: Optional[Union[Iterable[NewType.<locals>.new_type], str]] = None, bound_val_factory: Callable = <class 'front.state.BoundVal'>)[source]¶ >>> Binder = mk_binder() >>> d = dict() >>> b = Binder(d)
If I ask for
b.foo
(or any valid python identifier I want) it’ll be inserted as an “descriptor” attribute ofBinder
, but it’s value will be special valueValueNotSet
.>>> b.foo ValueNotSet >>> 'foo' in dir(Binder) True
Let’s set the value of
foo`
:>>> b.foo = 42 >>> b.foo 42
So
b.foo
is now set, but the real point is that this assignment was “registered” in the state we give theBinder
:>>> d {'foo': 42}
Wanna see that again?
>>> b.foo = "I'm bound" >>> b.foo "I'm bound" >>> d {'foo': "I'm bound"}
And same with
b.bar
:>>> b.bar ValueNotSet >>> b.bar = "me too" >>> b.bar 'me too' >>> d {'foo': "I'm bound", 'bar': 'me too'}
A
Binder
will also have some useful mapping methods that are linked to the underlyingstate
.>>> Binder = mk_binder(allowed_ids=['the', 'variables', 'I', 'want']) >>> state = dict() >>> b = Binder(state) >>> list(b) [] >>> b.want # I see a want, but no value is set ValueNotSet >>> list(b) # list still gives me nothing [] >>> b.want = 42 # but if I set a value for want >>> list(b) # I see want in the list ['want'] >>> 'want' in b # I can do this too True >>> 'not_in_there' in b False >>> 'variables' in b # 'variables' not "there" because not set False