habitat.utils.dynamicloader

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:

Further to that, functions expectisclass(), expectisfunction(), etc., 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 (i.e., 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.

Functions

expecthasattr(*args, **kwargs) does thing have an attribute named attr?
expecthasmethod(*args, **kwargs) is loadable.name callable?
expecthasnumargs(*args, **kwargs) does thing have num arguments?
expectiscallable(*args, **kwargs) is loadable a method, function or callable class?
expectisclass(*args, **kwargs) is thing a class?
expectisfunction(*args, **kwargs) is thing a function? (either normal or generator)
expectisgeneratorfunction(*args, **kwargs) is thing a generator function?
expectisstandardfunction(*args, **kwargs) is thing a normal function (i.e., not a generator)
expectissubclass(*args, **kwargs) is thing a subclass of the other thing?
fullname(loadable) Determines the full name in module.module.class form
hasattr(thing, attr) does thing have an attribute named attr?
hasmethod(loadable, name) is loadable.name callable?
hasnumargs(thing, num) does thing have num arguments?
iscallable(loadable) is loadable a method, function or callable class?
isclass(thing) is thing a class?
isfunction(thing) is thing a function? (either normal or generator)
isgeneratorfunction(thing) is thing a generator function?
isstandardfunction(thing) is thing a normal function (i.e., not a generator)
issubclass(thing, the_other_thing) is thing a subclass of the other thing?
load(loadable[, force_reload]) Attempts to dynamically load loadable
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.isclass(thing)[source]

is thing a class?

habitat.utils.dynamicloader.isfunction(thing)[source]

is thing a function? (either normal or generator)

habitat.utils.dynamicloader.isgeneratorfunction(thing)[source]

is thing a generator function?

habitat.utils.dynamicloader.issubclass(thing, the_other_thing)[source]

is thing a subclass of the other thing?

habitat.utils.dynamicloader.hasattr(thing, attr)[source]

does thing have an attribute named attr?

habitat.utils.dynamicloader.isstandardfunction(thing)[source]

is thing a normal function (i.e., not a generator)

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

does thing have 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]

is loadable.name callable?

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

is loadable 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)

is thing a class?

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

is thing a function? (either normal or generator)

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

is thing a generator function?

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

is thing a normal function (i.e., not a generator)

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

is loadable 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)

is thing a subclass of the other thing?

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

does thing have 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)

is loadable.name callable?

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

does thing have an attribute named attr?