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, 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.
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
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.
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.
Returns true if loadable.*name* is callable
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)
Return true if the object is a class.
Return true if the object is a user-defined function.
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.
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)
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.).
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.
Returns true if loadable.*name* is callable
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.)
Return true if the object is a class.
Return true if the object is a user-defined function.
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.
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.).
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.)