b3j0f.utils.property module

Library which aims to bind named properties on any element at runtime.

This module can bind a named property on any element but None methods.

In preserving binding from inheritance and automatical mechanisms which prevent to set any attribute on any elements.

When you are looking for an element bound property, the result is a dictionary of the shape {element, {property name, property}}.

b3j0f.utils.property.get_properties(elt, keys=None, ctx=None)[source]

Get elt properties.

Parameters:
  • elt – properties elt. Not None methods or unhashable types.
  • keys (list or str) – key(s) of properties to get from elt. If None, get all properties.
  • ctx – elt ctx from where get properties. Equals elt if None. It allows to get function properties related to a class or instance if related function is defined in base class.
Returns:

list of properties by elt and name.

Return type:

list

b3j0f.utils.property.get_first_property(elt, key, default=None, ctx=None)[source]

Get first property related to one input key.

Parameters:
  • elt – first property elt. Not None methods.
  • key (str) – property key to get.
  • default – default value to return if key does not exist in elt. properties
  • ctx – elt ctx from where get properties. Equals elt if None. It allows to get function properties related to a class or instance if related function is defined in base class.
b3j0f.utils.property.get_first_properties(elt, keys=None, ctx=None)[source]

Get first properties related to one input key.

Parameters:
  • elt – first property elt. Not None methods.
  • keys (list) – property keys to get.
  • ctx – elt ctx from where get properties. Equals elt if None. It allows to get function properties related to a class or instance if related function is defined in base class.
Returns:

dict of first values of elt properties.

b3j0f.utils.property.get_local_property(elt, key, default=None, ctx=None)[source]

Get one local property related to one input key or default value if key is not found.

Parameters:
  • elt – local property elt. Not None methods.
  • key (str) – property key to get.
  • default – default value to return if key does not exist in elt properties.
  • ctx – elt ctx from where get properties. Equals elt if None. It allows to get function properties related to a class or instance if related function is defined in base class.
Returns:

dict of properties by name.

Return type:

dict

b3j0f.utils.property.get_local_properties(elt, keys=None, ctx=None)[source]

Get local elt properties (not defined in elt type or base classes).

Parameters:
  • elt – local properties elt. Not None methods.
  • keys – keys of properties to get from elt.
  • ctx – elt ctx from where get properties. Equals elt if None. It allows to get function properties related to a class or instance if related function is defined in base class.
Returns:

dict of properties by name.

Return type:

dict

b3j0f.utils.property.put_properties(elt, properties, ttl=None, ctx=None)[source]

Put properties in elt.

Parameters:
  • elt – properties elt to put. Not None methods.
  • ttl (number) – If not None, property time to leave.
  • ctx – elt ctx from where put properties. Equals elt if None. It allows to get function properties related to a class or instance if related function is defined in base class.
  • properties (dict) – properties to put in elt. elt and ttl are exclude.
Returns:

Timer if ttl is not None.

Return type:

Timer

b3j0f.utils.property.del_properties(elt, keys=None, ctx=None)[source]

Delete elt property.

Parameters:
  • elt – properties elt to del. Not None methods.
  • keys – property keys to delete from elt. If empty, delete all properties.
b3j0f.utils.property.firsts(properties)[source]
Transform a dictionary of {name: [(elt, value)+]} (resulting from
get_properties) to a dictionary of {name, value} where names are first encountered in input properties.
Parameters:properties (dict) – properties to firsts.
Returns:dictionary of parameter values by names.
Return type:dict
b3j0f.utils.property.remove_ctx(properties)[source]
Transform a dictionary of {name: [(elt, value)+]} into a dictionary of
{name: [value+]}.
Parameters:properties (dict) – properties from where get only values.
Returns:dictionary of parameter values by names.
Return type:dict
b3j0f.utils.property.setdefault(elt, key, default, ctx=None)[source]
Get a local property and create default value if local property does not
exist.
Parameters:
  • elt – local proprety elt to get/create. Not None methods.
  • key (str) – proprety name.
  • default – property value to set if key no in local properties.
Returns:

property value or default if property does not exist.

b3j0f.utils.property.free_cache(ctx, *elts)[source]

Free properties bound to input cached elts. If empty, free the whole cache.

b3j0f.utils.property.find_ctx(elt)[source]

Get the right ctx related to input elt.

In order to keep safe memory as much as possible, it is important to find the right context element. For example, instead of putting properties on a function at the level of an instance, it is important to save such property on the instance because the function.__dict__ is shared with instance class function, and so, if the instance is deleted from memory, the property is still present in the class memory. And so on, it is impossible to identify the right context in such case if all properties are saved with the same key in the same function which is the function.

b3j0f.utils.property.addproperties(names, bfget=None, afget=None, enableget=True, bfset=None, afset=None, enableset=True, bfdel=None, afdel=None, enabledel=True)[source]

Decorator in charge of adding python properties to cls.

{a/b}fget, {a/b}fset and {a/b}fdel are applied to all properties matching names in taking care to not forget default/existing properties. The prefixes a and b are respectively for after and before default/existing property getter/setter/deleter execution.

These getter, setter and deleter functions are called before existing or default getter, setter and deleters. Default getter, setter and deleters are functions which uses an attribute with a name starting with ‘_’ and finishing with the property name (like the python language convention).

See also

_protectedattrname(name)

Parameters:
  • names (str(s)) – property name(s) to add.
  • bfget – getter function to apply to all properties before default/existing getter execution. Parameters are a decorated cls instance and a property name.
  • afget – fget function to apply to all properties after default/existing getter execution. Parameters are a decorated cls instance and a property name.
  • enableget (bool) – if True (default), enable existing or default getter . Otherwise, use only fget if given.
  • bfset – fset function to apply to all properties before default/existing setter execution. Parameters are a decorated cls instance and a property name.
  • afset – fset function to apply to all properties after default/existing setter execution. Parameters are a decorated cls instance and a property name.
  • enableset (bool) – if True (default), enable existing or default setter . Otherwise, use only fset if given.
  • bfdel – fdel function to apply to all properties before default/existing deleter execution. Parameters are a decorated cls instance and a property name.
  • bfdel – fdel function to apply to all properties after default/existing deleter execution. Parameters are a decorated cls instance and a property name.
  • enabledel (bool) – if True (default), enable existing or default deleter. Otherwise, use only fdel if given.
Returns:

cls decorator.