b3j0f.utils.path module

Tools for managing path resolution of python objects.

b3j0f.utils.path.clearcache(path=None)[source]

Clear cache memory for input path.

Parameters:
  • path (str) – element path to remove from cache. If None clear all cache
  • cache (dict) – cache to clear. Default is __LOOKUP_CACHE.
Example:
>>> incache('b3j0f.utils')
False
>>> lookup('b3j0f.utils')
>>> incache('b3j0f.utils')
True
>>> clearcache('b3j0f.path')
>>> incache('b3j0f.utils')
False
>>> lookup('b3j0f.utils')
>>> incache('b3j0f.utils')
True
>>> clearcache()
>>> incache('b3j0f.utils')
False
b3j0f.utils.path.incache(path)[source]

Check if input path is in cache.

Returns:True if path is in cache
Return type:bool
Example:
>>> incache('b3j0f.utils')
False
>>> lookup('b3j0f.utils')
>>> incache('b3j0f.utils')
True
b3j0f.utils.path.lookup(path, cache=True, scope=None, safe=False)[source]

Get element reference from input element.

The element can be a builtin/globals/scope object or is resolved from the current execution stack.

Limitations:

it does not resolve class methods or static values such as True, False, numbers, string and keywords.

Parameters:
  • path (str) – full path to a python element.
  • cache (bool) – if True (default), permits to reduce time complexity for lookup resolution in using cache memory to save resolved elements.
  • scope (dict) – object scrope from where find path. For example, this scope can be locals(). Default is globals().
  • safe (bool) – use lookup in a safe context. A safe context avoid to reach builtins function with I/O consequences.
Returns:

python object which is accessible through input path or raise an exception if the path is wrong.

Return type:

object

Raises:

ImportError – if path is wrong

b3j0f.utils.path.getpath(element)[source]

Get full path of a given element such as the opposite of the resolve_path behaviour.

Parameters:element – must be directly defined into a module or a package and has the attribute ‘__name__’.
Returns:element absolute path.
Return type:str
Raises:AttributeError – if element has not the attribute __name__.
Example:
>>> getpath(getpath)
b3j0f.utils.path.getpath
b3j0f.utils.path.alias(_id)[source]

Decorator dedicated to make an alias of a decorated element in order to register it in the lookup cache.

Parameters:_id (str) – alias identifier.
Example:
>>> alias('halfsonofzeus', 'hercules')
'hercules'
>>> lookup('halfsonofzeus')
'hercules'
>>> @alias('cube')
>>> def cube(value): return value ** value ** value
>>> lookpath('cube')(2)
8