front.state

exception front.state.ConditionNotMet[source]

Raised when a value doesn’t meet some condition

exception front.state.Forbidden[source]

To use to indicate that something is not allowed

exception front.state.ForbiddenOverwrite[source]

Error to raise when a writes to existing keys are not allowed

exception front.state.ForbiddenWrite[source]

Error to raise when a write operation is not allowed

class front.state.GetterSetter(*args, **kwargs)[source]

The type of an object obj that has the operations v = obj[k] and obj[ k] = v

class front.state.HasState(*args, **kwargs)[source]
class front.state.IsInstanceOf(class_or_tuple: Union[type, Iterable[type]])[source]
class front.state.State(*args, **kwds)[source]
get(k[, d]) → D[k] if k in D, else d. d defaults to None.[source]
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 of Binder, but it’s value will be special value ValueNotSet.

>>> 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 the Binder:

>>> 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 underlying state.

>>> 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