xotl.tools.modules – Utilities for working with modules¶
Modules utilities.
-
xotl.tools.modules.copy_members(source=None, target=None)[source]¶ Copy module members from
sourcetotarget.It’s common in
xotl.toolspackage to extend Python modules with the same name, for examplexotl.tools.datetimehas all public members of Python’sdatetime.copy_members()can be used to copy all members from the original module to the extended one.Parameters: - source –
string with source module name or module itself.
If not given, is assumed as the last module part name of
target. - target –
string with target module name or module itself.
If not given, target name is looked in the stack of caller module.
Returns: Source module.
Return type: ModuleTypeWarning
Implementation detail
Function used to inspect the stack is not guaranteed to exist in all implementations of Python.
- source –
-
xotl.tools.modules.customize(module, custom_attrs=None, meta=None)[source]¶ Replaces a
moduleby a custom one.Injects all kwargs into the newly created module’s class. This allows to have module into which we may have properties or other type of descriptors.
Parameters: - module – The module object to customize.
- custom_attrs –
A dictionary of custom attributes that should be injected in the customized module.
New in version 1.4.2: Changes the API, no longer uses the
**kwargsidiom for custom attributes. - meta – The metaclass of the module type. This should be a subclass
of
type. We will actually subclass this metaclass to properly injectcustom_attrsin our own internal metaclass.
Returns: A tuple of
(module, customized, class)with the module in the first place,customizedwill be True only if the module was created (i.ecustomize()is idempotent), and the third item will be the class of the module (the first item).
-
xotl.tools.modules.force_module(ref=None)[source]¶ Load a module from a string or return module if already created.
If
refis not specified (or integer) calling module is assumed looking in the stack.Note
Implementation detail
Function used to inspect the stack is not guaranteed to exist in all implementations of Python.
-
xotl.tools.modules.get_module_path(module)[source]¶ Gets the absolute path of a
module.Parameters: module – Either module object or a (dotted) string for the module. Returns: The path of the module. If the module is a package, returns the directory path (not the path to the
__init__).If
moduleis a string and it’s not absolute, raises a TypeError.
-
xotl.tools.modules.modulemethod(func)[source]¶ Decorator that defines a module-level method.
Simply a module-level method, will always receive a first argument
selfwith the module object.
-
xotl.tools.modules.moduleproperty(getter, setter=None, deleter=None, doc=None, base=<class 'property'>)[source]¶ Decorator that creates a module-level property.
The module of the
getteris replaced by a custom implementation of the module, and the property is injected to the custom module’s class.The parameter
baseserves the purpose of changing the base for the property. For instance, this allows you to havememoized_propertiesat the module-level:def memoized(self): return self memoized = moduleproperty(memoized, base=memoized_property)
New in version 1.6.1: Added the
baseparameter.