b3j0f.annotation.core module

This module defines the Annotation class.

Such Annotation is close to the reflective paradigm in having himself its own
lifecycle independently from its annotated elements (called commonly
target).

Such Annotation:

  • can annotate all python object, from classes to class instances.
  • have their own business behaviour.
  • are reusable to annotate several objects.
  • can propagate their annotation capability to class inheritance tree.
class b3j0f.annotation.core.Annotation(on_bind_target=None, propagate=True, override=False, ttl=None, in_memory=False)[source]

Bases: object

Base class for all annotations defined in this library.

It contains functions to override in order to catch initialisation of this Annotation and annotated elements binding (also called commonly target in the context of Annotation).

All annotations which inherit from this are registered to target objects and are accessibles through the static method Annotation.get_annotations.

Instance methods to override are: - __init__: set parameters during its instantiation. - _bind_target: called to bind target to this. - on_bind_target: fired when the annotated element is bound to this.

And properties are: - propagate: (default True) determines if an annotation is propagated to all sub target elements. - override: (default False) exclude previous annotation of the same type as self class. - _ttl: (default None) self time to leave. - _in_memory: (default False) save instance in a global dictionary.

It is also possible to set on_bind_target, propagate and override in
the constructor.
IN_MEMORY = '_in_memory'

attribute name for in_memory

OVERRIDE = 'override'

attribute name for overriding base annotations.

PROPAGATE = 'propagate'

attribute name for annotation propagatation on sub targets.

TARGETS = 'targets'

atribute name for target

TTL = '_ttl'

attribute name for annotation ttl

bind_target(target, ctx=None)[source]

Bind self annotation to target.

Parameters:
  • target – target to annotate.
  • ctx – target ctx.
Returns:

bound target.

classmethod free_memory(exclude=None)[source]

Free global annotation memory.

classmethod get_annotated_fields(instance, select=<function <lambda>>)[source]

Get dict of {annotated fields: annotations} by cls of input instance.

Returns:a set of (annotated fields, annotations).
Return type:dict
classmethod get_annotations(target, exclude=None, ctx=None, select=<function <lambda>>, mindepth=0, maxdepth=0, followannotated=True, public=True, _history=None)[source]

Returns all input target annotations of cls type sorted by definition order.

Parameters:
  • cls (type) – type of annotation to get from target.
  • target – target from where get annotations.
  • exclude (tuple/type) – annotation types to remove from selection.
  • ctx – target ctx.
  • select – bool function which select annotations after applying previous type filters. Takes a target, a ctx and an annotation in parameters. True by default.
  • mindepth (int) – minimal depth for searching annotations (default 0)
  • maxdepth (int) – maximal depth for searching annotations (default 0)
  • followannotated (bool) – if True (default) follow deeply only annotated members.
  • public (bool) – if True (default) follow public members.
  • _history (list) – private parameter which save parsed elements.
Return type:

Annotation

classmethod get_local_annotations(target, exclude=None, ctx=None, select=<function <lambda>>)[source]
Get a list of local target annotations in the order of their
definition.
Parameters:
  • cls (type) – type of annotation to get from target.
  • target – target from where get annotations.
  • exclude (tuple/type) – annotation types to exclude from selection.
  • ctx – target ctx.
  • select – selection function which takes in parameters a target, a ctx and an annotation and returns True if the annotation has to be selected. True by default.
Returns:

target local annotations.

Return type:

list

classmethod get_memory_annotations(exclude=None)[source]

Get annotations in memory which inherits from cls.

Parameters:exclude (tuple/type) – annotation type(s) to exclude from search.
Returns:found annotations which inherits from cls.
Return type:set
in_memory
Returns:True if self is in a global memory of annotations.
on_bind_target(target, ctx=None)[source]

Fired after target is bound to self.

Parameters:
  • target – newly bound target.
  • ctx – target ctx.
override
propagate
classmethod remove(target, exclude=None, ctx=None, select=<function <lambda>>)[source]

Remove from target annotations which inherit from cls.

Parameters:
  • target – target from where remove annotations which inherits from cls.
  • exclude (tuple/type) – annotation types to exclude from selection.
  • ctx – target ctx.
  • select – annotation selection function which takes in parameters a target, a ctx and an annotation and return True if the annotation has to be removed.
remove_from(target, ctx=None)[source]

Remove self annotation from target annotations.

Parameters:
  • target – target from where remove self annotation.
  • ctx – target ctx.
targets
ttl

Get actual ttl in seconds.

Returns:actual ttl.
Return type:float
class b3j0f.annotation.core.StopPropagation(*annotation_types, **kwargs)[source]

Bases: b3j0f.annotation.core.Annotation

Stop propagation for annotation types.

ANNOTATION_TYPES = 'annotation_types'
annotation_types
override
propagate
targets
class b3j0f.annotation.core.RoutineAnnotation(routine=None, params=None, result=None, *args, **kwargs)[source]

Bases: b3j0f.annotation.core.Annotation

Dedicated to add information on any routine, routine parameters or routine result.

PARAMS = 'params'

parameters attribute name

RESULT = 'result'

result attribute name

ROUTINE = 'routine'

routine attribute name

override
params
propagate
result
routine
targets