habitat.utils.dynamicloader: a generic dynamic module loader

A generic dynamic python module loader.

The main function to call is load(). In addition, several functions to quickly test the loaded object for certain conditions are provided:

  • isclass
  • isfunction
  • isgeneratorfunction
  • isstandardfunction (isfunction and not isgeneratorfunction)
  • iscallable
  • issubclass
  • hasnumargs
  • hasmethod
  • hasattr

Further to that, functions expectisclass, expectisfunction, e.t.c, are provided which are identical to the above except they raise either a ValueError or a TypeError where the original function would have returned false.

Example use:

def loadsomething(loadable):
    loadable = dynamicloader.load(loadable)
    expectisstandardfunction(loadable)
    expecthasattr(loadable, 2)

If you use expectiscallable note that you may get either a function or a class, an object of which is callable (ie. the class has __call__(self, ...)). In that case you may need to create an object:

if isclass(loadable):
    loadable = loadable()

Of course if you’ve used expectisclass then you will be creating an object anyway. Note that classes are technically “callable” in that calling them creates objects. expectiscallable ignores this.

A lot of the provided tests are imported straight from inspect and are therefore not documented here. The ones implemented as a part of this module are.

habitat.utils.dynamicloader.load(loadable, force_reload=False)[source]

Attempts to dynamically load loadable

loadable: a class, a function, a module, or a string that is a dotted-path to one a class function or module

Some examples:

load(MyClass) # returns MyClass
load(MyFunction) # returns MyFunction
load("mypackage") # returns the mypackage module
load("packagea.packageb") # returns the packageb module
load("packagea.packageb.aclass") # returns aclass
habitat.utils.dynamicloader.fullname(loadable)[source]

Determines the full name in module.module.class form

loadable: a class, module or function.

If fullname is given a string it will load() it in order to resolve it to its true full name.

habitat.utils.dynamicloader.hasnumargs(thing, num)[source]

Returns true if thing has num arguments.

If thing is a function, the positional arguments are simply counted up. If thing is a method, the positional arguments are counted up and one is subtracted in order to account for method(self, ...) If thing is a class, the positional arguments of cls.__call__ are counted up and one is subtracted (self), giving the number of arguments a callable object created from that class would have.

habitat.utils.dynamicloader.hasmethod(loadable, name)[source]

Returns true if loadable.*name* is callable

habitat.utils.dynamicloader.iscallable(loadable)[source]

Returns true if loadable is a method, function or callable class.

For loadable to be a callable class, an object created from it must be callable (i.e., it has a __call__ method)

habitat.utils.dynamicloader.expectisclass(*args, **kwargs)

Return true if the object is a class.

Class objects provide these attributes:
__doc__ documentation string __module__ name of module in which this class was defined
habitat.utils.dynamicloader.expectisfunction(*args, **kwargs)

Return true if the object is a user-defined function.

Function objects provide these attributes:
__doc__ documentation string __name__ name with which this function was defined func_code code object containing compiled function bytecode func_defaults tuple of any default values for arguments func_doc (same as __doc__) func_globals global namespace in which this function was defined func_name (same as __name__)
habitat.utils.dynamicloader.expectisgeneratorfunction(*args, **kwargs)

Return true if the object is a user-defined generator function.

Generator function objects provides same attributes as functions.

See isfunction.__doc__ for attributes listing.

habitat.utils.dynamicloader.expectiscallable(*args, **kwargs)

Returns true if loadable is a method, function or callable class.

For loadable to be a callable class, an object created from it must be callable (i.e., it has a __call__ method)

habitat.utils.dynamicloader.expectissubclass(*args, **kwargs)

issubclass(C, B) -> bool

Return whether class C is a subclass (i.e., a derived class) of class B. When using a tuple as the second argument issubclass(X, (A, B, ...)), is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).

habitat.utils.dynamicloader.expecthasnumargs(*args, **kwargs)

Returns true if thing has num arguments.

If thing is a function, the positional arguments are simply counted up. If thing is a method, the positional arguments are counted up and one is subtracted in order to account for method(self, ...) If thing is a class, the positional arguments of cls.__call__ are counted up and one is subtracted (self), giving the number of arguments a callable object created from that class would have.

habitat.utils.dynamicloader.expecthasmethod(*args, **kwargs)

Returns true if loadable.*name* is callable

habitat.utils.dynamicloader.expecthasattr(*args, **kwargs)

hasattr(object, name) -> bool

Return whether the object has an attribute with the given name. (This is done by calling getattr(object, name) and catching exceptions.)

habitat.utils.dynamicloader.isclass(object)

Return true if the object is a class.

Class objects provide these attributes:
__doc__ documentation string __module__ name of module in which this class was defined
habitat.utils.dynamicloader.isfunction(object)

Return true if the object is a user-defined function.

Function objects provide these attributes:
__doc__ documentation string __name__ name with which this function was defined func_code code object containing compiled function bytecode func_defaults tuple of any default values for arguments func_doc (same as __doc__) func_globals global namespace in which this function was defined func_name (same as __name__)
habitat.utils.dynamicloader.isgeneratorfunction(object)

Return true if the object is a user-defined generator function.

Generator function objects provides same attributes as functions.

See isfunction.__doc__ for attributes listing.

habitat.utils.dynamicloader.issubclass()

issubclass(C, B) -> bool

Return whether class C is a subclass (i.e., a derived class) of class B. When using a tuple as the second argument issubclass(X, (A, B, ...)), is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).

habitat.utils.dynamicloader.hasattr()

hasattr(object, name) -> bool

Return whether the object has an attribute with the given name. (This is done by calling getattr(object, name) and catching exceptions.)

Previous topic

habitat.utils.crashmat: Error handling and process termination

Next topic

habitat.utils.filtertools: Useful tools for filters to use

This Page