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(annotation_type, exclude=None)[source]

Free global annotation memory.

classmethod get_annotated_fields(annotation_type, instance, select=<function <lambda> at 0x2488398>)[source]

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

Returns:a set of (annotated fields, annotations)
Return type:dict
classmethod get_annotations(annotation_type, target, exclude=None, ctx=None, select=<function <lambda> at 0x24882a8>)[source]

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

Parameters:
  • annotation_type (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.
classmethod get_local_annotations(annotation_type, target, exclude=None, ctx=None, select=<function <lambda> at 0x24880c8>)[source]
Get a list of local target annotations in the order of their
definition.
Parameters:
  • annotation_type (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(annotation_type, exclude=None)[source]

Get annotations in memory which inherits from annotation_type.

Parameters:exclude (tuple/type) – annotation type(s) to exclude from search
Returns:found annotations which inherits from annotation_type.
Return type:set
in_memory[source]
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(annotation_type, target, exclude=None, ctx=None, select=<function <lambda> at 0x24881b8>)[source]

Remove from target annotations which inherit from annotation_type

Parameters:
  • target – target from where remove annotations which inherits from annotation_type.
  • 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[source]

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

Previous topic

b3j0f.annotation.check module

Next topic

b3j0f.annotation.interception module

This Page