front.state#
Stateful storage protocols and Forbidden errors used by front data bindings.
Defines the State wrapper and the GetterSetter / StateType
protocols an app’s backing store must satisfy, plus a small hierarchy of
Forbidden* exceptions raised when a write breaks the configured policy.
Functions
|
Make a |
Classes
|
Descriptor reading and writing |
|
The type of an object |
|
An object with a |
|
A picklable |
|
A write-policing wrapper around a key-value |
Exceptions
Raised when a value doesn't meet the condition set for its key. |
|
Base of the errors raised when an operation is not allowed. |
|
Raised when writing a different value to an existing key is not allowed. |
|
Raised when writing to a key is not allowed. |
- class front.state.BoundVal(key, *, value_not_set=ValueNotSet)[source]#
Bases:
objectDescriptor reading and writing
keyin the owner’s_statemapping.Reading returns
value_not_set(ValueNotSetby default) while the key is absent.>>> class Obj: ... _state = {} ... x = BoundVal('x') >>> obj = Obj() >>> obj.x ValueNotSet >>> obj.x = 5 >>> obj.x, Obj._state (5, {'x': 5}) >>> Obj.x BoundVal('x')
- exception front.state.ConditionNotMet[source]#
Bases:
ValueErrorRaised when a value doesn’t meet the condition set for its key.
- exception front.state.Forbidden[source]#
Bases:
ExceptionBase of the errors raised when an operation is not allowed.
- exception front.state.ForbiddenOverwrite[source]#
Bases:
ForbiddenRaised when writing a different value to an existing key is not allowed.
- exception front.state.ForbiddenWrite[source]#
Bases:
ForbiddenRaised when writing to a key is not allowed.
- class front.state.GetterSetter(*args, **kwargs)[source]#
Bases:
ProtocolThe type of an object
objsupportingv = obj[k]andobj[k] = v.
- class front.state.HasState(*args, **kwargs)[source]#
Bases:
ProtocolAn object with a
_statemutable mapping, asBoundValdescriptors expect.
- class front.state.IsInstanceOf(class_or_tuple)[source]#
Bases:
objectA picklable
isinstancepredicate:IsInstanceOf(int)(3)is True.
- class front.state.State(state, condition_for_key=(), forbidden_writes=(), forbidden_overwrites=())[source]#
Bases:
MutableMappingA write-policing wrapper around a key-value
state(anyGetterSetter).Reads and the other
MutableMappingoperations forward tostate. Writes are checked first: keys inforbidden_writescan never be written; keys inforbidden_overwritescan be written once (re-writing the same value is allowed); a key incondition_for_keyonly accepts values for which its predicate is true (a type there meansisinstance).>>> state = State( ... state={}, ... forbidden_writes={'foo'}, ... forbidden_overwrites={'apple'}, ... condition_for_key={'apple': list, 'carrot': lambda x: x > 10}, ... ) >>> state['apple'] = [4, 2] >>> state['apple'] = [4, 2] # same value again: fine >>> state['apple'] = [1] Traceback (most recent call last): ... front.state.ForbiddenOverwrite: Not allowed to write under this key more than once: apple >>> state['foo'] = 1 Traceback (most recent call last): ... front.state.ForbiddenWrite: Not allowed to write on foo >>> state['carrot'] = 10 Traceback (most recent call last): ... front.state.ConditionNotMet: The value for the carrot key must satisfy condition <function <lambda> at 0x...> >>> state['carrot'] = 11 >>> dict(state) {'apple': [4, 2], 'carrot': 11}
- Raises:
ForbiddenWrite – On writing a key of
forbidden_writes.ForbiddenOverwrite – On writing a different value to an existing key of
forbidden_overwrites.ConditionNotMet – On writing a value that fails the key’s condition.
- front.state.mk_binder(state=None, allowed_ids=None, bound_val_factory=<class 'front.state.BoundVal'>)[source]#
Make a
Binderclass (or instance) whose attributes read and write a state mapping.Returns a class when
stateis None, and an instance bound tostateotherwise. Withallowed_ids, only those names are bound (asbound_val_factorydescriptors); without, any identifier is bound on first access.>>> 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.foois 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
Binderwill 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