traits.observation
Package¶
traits.observation.exception_handling
Module¶
- class traits.observation.exception_handling.ObserverExceptionHandler(handler, reraise_exceptions)[source]¶
Bases:
object
State for an exception handler.
- Parameters:
handler (callable(event) or None) – A callable to handle an event, in the context of an exception. If None, the exceptions will be logged.
reraise_exceptions (boolean) – Whether to reraise the exception.
- class traits.observation.exception_handling.ObserverExceptionHandlerStack[source]¶
Bases:
object
A stack of exception handlers.
- Parameters:
handlers (list of ObserverExceptionHandler) – The last item is the current handler.
- push_exception_handler(handler=None, reraise_exceptions=False)[source]¶
Push a new exception handler into the stack. Making it the current exception handler.
- Parameters:
handler (callable(event) or None) – A callable to handle an event, in the context of an exception. If None, the exceptions will be logged.
reraise_exceptions (boolean) – Whether to reraise the exception.
- pop_exception_handler()[source]¶
Pop the current exception handler from the stack.
- Raises:
IndexError – If there are no handlers to pop.
- traits.observation.exception_handling.push_exception_handler(handler=None, reraise_exceptions=False)¶
Push a new exception handler into the stack. Making it the current exception handler.
- Parameters:
handler (callable(event) or None) – A callable to handle an event, in the context of an exception. If None, the exceptions will be logged.
reraise_exceptions (boolean) – Whether to reraise the exception.
- traits.observation.exception_handling.pop_exception_handler()¶
Pop the current exception handler from the stack.
- Raises:
IndexError – If there are no handlers to pop.
traits.observation.exceptions
Module¶
traits.observation.api
Module¶
traits.observation.expression
Module¶
- class traits.observation.expression.ObserverExpression[source]¶
Bases:
object
ObserverExpression is an object for describing what traits are being observed for change notifications. It can be passed directly to
HasTraits.observe
method or theobserve
decorator.An ObserverExpression is typically created using one of the top-level functions provided in this module, e.g.
trait
.- __or__(expression)[source]¶
Create a new expression that matches this expression OR the given expression.
e.g.
trait("age") | trait("number")
will match either trait age or trait number on an object.- Parameters:
expression (ObserverExpression)
- Returns:
new_expression
- Return type:
- then(expression)[source]¶
Create a new expression by extending this expression with the given expression.
e.g.
trait("child").then(trait("age") | trait("number"))
on an object matcheschild.age
orchild.number
on the object.- Parameters:
expression (ObserverExpression)
- Returns:
new_expression
- Return type:
- match(filter, notify=True)[source]¶
Create a new expression for observing traits using the given filter.
Events emitted (if any) will be instances of
TraitChangeEvent
.- Parameters:
filter (callable(str, CTrait) -> bool) – A callable that receives the name of a trait and the corresponding trait definition. The returned bool indicates whether the trait is observed. In order to remove an existing observer with the equivalent filter, the filter callables must compare equally. The callable must also be hashable.
notify (bool, optional) – Whether to notify for changes. Default is to notify.
- Returns:
new_expression
- Return type:
- anytrait(notify=True)[source]¶
Create a new expression for observing all traits.
Events emitted (if any) will be instances of
TraitChangeEvent
.- Parameters:
notify (bool, optional) – Whether to notify for changes. Default is to notify.
- Returns:
new_expression
- Return type:
- metadata(metadata_name, notify=True)[source]¶
Return a new expression for observing traits where the given metadata is not None.
Events emitted (if any) will be instances of
TraitChangeEvent
.e.g.
metadata("age")
matches traits whose ‘age’ attribute has a non-None value.- Parameters:
- Returns:
new_expression
- Return type:
- dict_items(notify=True, optional=False)[source]¶
Create a new expression for observing items inside a dict.
Events emitted (if any) will be instances of
DictChangeEvent
.If an expression with
dict_items
is further extended, the values of the dict will be given to the next item in the expression. For example, the following observes a trait named “number” on any object that is one of the values in the dict named “mapping”:trait("mapping").dict_items().trait("number")
- Parameters:
- Returns:
new_expression
- Return type:
- list_items(notify=True, optional=False)[source]¶
Create a new expression for observing items inside a list.
Events emitted (if any) will be instances of
ListChangeEvent
.e.g.
trait("containers").list_items()
for observing mutations to a list namedcontainers
.e.g.
trait("containers").list_items().trait("value")
for observing the traitvalue
on any items in the listcontainers
.- Parameters:
- Returns:
new_expression
- Return type:
- set_items(notify=True, optional=False)[source]¶
Create a new expression for observing items inside a set.
Events emitted (if any) will be instances of
SetChangeEvent
.- Parameters:
- Returns:
new_expression
- Return type:
- trait(name, notify=True, optional=False)[source]¶
Create a new expression for observing a trait with the exact name given.
Events emitted (if any) will be instances of
TraitChangeEvent
.- Parameters:
- Returns:
new_expression
- Return type:
- class traits.observation.expression.SingleObserverExpression(observer)[source]¶
Bases:
ObserverExpression
Container of ObserverExpression for wrapping a single observer.
- class traits.observation.expression.SeriesObserverExpression(first, second)[source]¶
Bases:
ObserverExpression
Container of ObserverExpression for joining expressions in series.
- Parameters:
first (ObserverExpression) – Left expression to be joined in series.
second (ObserverExpression) – Right expression to be joined in series.
- class traits.observation.expression.ParallelObserverExpression(left, right)[source]¶
Bases:
ObserverExpression
Container of ObserverExpression for joining expressions in parallel.
- Parameters:
left (ObserverExpression) – Left expression to be joined in parallel.
right (ObserverExpression) – Right expression to be joined in parallel.
- traits.observation.expression.join(*expressions)[source]¶
Convenient function for joining many expressions in series using
ObserverExpression.then
- Parameters:
*expressions (iterable of ObserverExpression)
- Returns:
new_expression – Joined expression.
- Return type:
- traits.observation.expression.dict_items(notify=True, optional=False)[source]¶
Create a new expression for observing items inside a dict.
Events emitted (if any) will be instances of
DictChangeEvent
.If an expression with
dict_items
is further extended, the values of the dict will be given to the next item in the expression. For example, the following observes a trait named “number” on any object that is one of the values in the dict named “mapping”:trait("mapping").dict_items().trait("number")
- Parameters:
- Returns:
new_expression
- Return type:
- traits.observation.expression.list_items(notify=True, optional=False)[source]¶
Create a new expression for observing items inside a list.
Events emitted (if any) will be instances of
ListChangeEvent
.e.g.
trait("containers").list_items()
for observing mutations to a list namedcontainers
.e.g.
trait("containers").list_items().trait("value")
for observing the traitvalue
on any items in the listcontainers
.- Parameters:
- Returns:
new_expression
- Return type:
- traits.observation.expression.match(filter, notify=True)[source]¶
Create a new expression for observing traits using the given filter.
Events emitted (if any) will be instances of
TraitChangeEvent
.- Parameters:
filter (callable(str, CTrait) -> bool) – A callable that receives the name of a trait and the corresponding trait definition. The returned bool indicates whether the trait is observed. In order to remove an existing observer with the equivalent filter, the filter callables must compare equally. The callable must also be hashable.
notify (bool, optional) – Whether to notify for changes.
- Returns:
new_expression
- Return type:
- traits.observation.expression.anytrait(notify=True)[source]¶
Create a new expression for observing all traits on an object.
Events emitted (if any) will be instances of
TraitChangeEvent
.- Parameters:
notify (bool, optional) – Whether to notify for changes.
- traits.observation.expression.metadata(metadata_name, notify=True)[source]¶
Return a new expression for observing traits where the given metadata is not None.
Events emitted (if any) will be instances of
TraitChangeEvent
.e.g.
metadata("age")
matches traits whose ‘age’ attribute has a non-None value.- Parameters:
- Returns:
new_expression
- Return type:
- traits.observation.expression.set_items(notify=True, optional=False)[source]¶
Create a new expression for observing items inside a set.
Events emitted (if any) will be instances of
SetChangeEvent
.- Parameters:
- Returns:
new_expression
- Return type:
- traits.observation.expression.trait(name, notify=True, optional=False)[source]¶
Create a new expression for observing a trait with the exact name given.
Events emitted (if any) will be instances of
TraitChangeEvent
.- Parameters:
- Returns:
new_expression
- Return type:
- traits.observation.expression.compile_expr(expr)[source]¶
Compile an ObserverExpression to a list of ObserverGraphs.
- Parameters:
expr (ObserverExpression)
- Return type:
list of ObserverGraph
traits.observation.events
Module¶
Event objects received by change handlers added using observe.
- class traits.observation.events.DictChangeEvent(*, object, removed, added)[source]¶
Event object to represent mutations on a dict.
Note that the API is different from the
TraitDictEvent
emitted via the “name _items” trait. In particular, the attributechanged
is not defined here.The interface of this object is provisional as of version 6.1.
- object¶
The dict being mutated.
- removed¶
Keys and values for removed or updated items. If keys are found in
added
as well, they refer to updated items and the values are old.- Type:
- class traits.observation.events.ListChangeEvent(*, object, index, removed, added)[source]¶
Event object to represent mutations to a list.
The interface of this object is provisional as of version 6.1.
- object¶
The list being mutated.
- class traits.observation.events.SetChangeEvent(*, object, removed, added)[source]¶
Event object to represent mutations on a set.
The interface of this object is provisional as of version 6.1.
- object¶
The set being mutated.
traits.observation.parsing
Module¶
- traits.observation.parsing.parse(text)[source]¶
Top-level function for parsing user’s text to an ObserverExpression.
- Parameters:
text (str) – Text to be parsed.
- Returns:
expression
- Return type:
- Raises:
ValueError – If the text is not a valid observe expression.