whitematteranalysis.utils package#
Subpackages#
Submodules#
whitematteranalysis.utils.opt_pckg module#
Methods to support optional packages.
- whitematteranalysis.utils.opt_pckg.optional_package(name, trip_msg=None)#
Return package-like thing and module setup for package
name
.- namestr
Package name.
- trip_msgNone or str
Message to be shown when the specified package cannot be imported.
- pckgpackage, module or
TripWire
instance If the package can be imported, return it. Otherwise, return an object raising an error when accessed.
- have_pkgbool
True if import for package was successful, False otherwise.
- module_setupfunction
Callable usually set as
setup_module
in calling namespace, to allow skipping tests.
Typical use:
>>> from whitematteranalysis.utils.opt_pckg import optional_package >>> pckg, have_pckg, _setup_module = optional_package("not_a_package")
In this case the package doesn’t exist, and therefore:
>>> have_pckg False
and
>>> pckg.some_function() Traceback (most recent call last): ... TripWireError: We need package not_a_package for these functions, but ``import not_a_package`` raised an ImportError
If the module does exist - we get the module
>>> pckg, _, _ = optional_package("os") >>> hasattr(pckg, "path") True
Or a submodule if that’s what we asked for
>>> subpckg, _, _ = optional_package("os.path") >>> hasattr(subpckg, "dirname") True
whitematteranalysis.utils.tripwire module#
- class whitematteranalysis.utils.tripwire.TripWire(msg)#
Bases:
object
Class raising error if used.
Standard use is to proxy modules that could not be imported.
Examples
>>> try: ... import not_a_package ... except ImportError: ... not_a_package_name = TripWire("Do not have not_a_package_name") >>> not_a_package_name.do_something("with argument") Traceback (most recent call last): ... TripWireError: Do not have not_a_package_name
- exception whitematteranalysis.utils.tripwire.TripWireError#
Bases:
AttributeError
Class to raise an exception for missing modules or other misfortunes..
- whitematteranalysis.utils.tripwire.is_tripwire(obj)#
Returns True if
obj
appears to be aTripWire
object.Examples
>>> is_tripwire(object()) False >>> is_tripwire(TripWire("some message")) True