xotl.tools.modules – Utilities for working with modules

Modules utilities.

xotl.tools.modules.copy_members(source=None, target=None)[source]

Copy module members from source to target.

It’s common in xotl.tools package to extend Python modules with the same name, for example xotl.tools.datetime has all public members of Python’s datetime. copy_members() can be used to copy all members from the original module to the extended one.

  • 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.


Source module.

Return type:



Implementation detail

Function used to inspect the stack is not guaranteed to exist in all implementations of Python.

xotl.tools.modules.customize(module, custom_attrs=None, meta=None)[source]

Replaces a module by 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.

  • 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 **kwargs idiom 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 inject custom_attrs in our own internal metaclass.

A tuple of (module, customized, class) with the module in the first place, customized will be True only if the module was created (i.e customize() is idempotent), and the third item will be the class of the module (the first item).


Load a module from a string or return module if already created.

If ref is not specified (or integer) calling module is assumed looking in the stack.


Implementation detail

Function used to inspect the stack is not guaranteed to exist in all implementations of Python.


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 module is a string and it’s not absolute, raises a TypeError.


Decorator that defines a module-level method.

Simply a module-level method, will always receive a first argument self with 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 getter is replaced by a custom implementation of the module, and the property is injected to the custom module’s class.

The parameter base serves the purpose of changing the base for the property. For instance, this allows you to have memoized_properties at the module-level:

def memoized(self):
    return self
memoized = moduleproperty(memoized, base=memoized_property)

New in version 1.6.1: Added the base parameter.