bronx.syntax.externalcode¶
A handy class that checks that an external code import worked properly.
If not, a decorator is provided to disable portions of code.
Example of a simple use:
from bronx.syntax.externalcode import ExternalCodeImportChecker
echecker = ExternalCodeImportChecker('anypackage')
with echecker:
import anypackage
print('Did it work ? {!s}'.format(echecker.is_available()))
@echecker.disabled_if_unavailable
def my_function_based_on_anypackage()
return anypackage.sayhi()
@echecker.disabled_if_unavailable
class MyClassBasedOnAnypackage(object)
def sayhi(self)
return anypackage.sayhi()
# A call to my_function_based_on_anypackage or any attempt to create a
# MyClassBasedOnAnypackage object will lead to a ExternalCodeUnavailableError
# error if the import statement failed.
Example of that also checks a version number:
from bronx.syntax.externalcode import ExternalCodeImportChecker
echecker = ExternalCodeImportChecker('anypackage')
with echecker as echecker_register:
import anypackage
echecker_register.update(version=anypackage.__version__,
otherthing=anypackage.otherthing)
# Ensure that the package is here, that version >= 1.0.0 and otherthings==1
@echecker.disabled_if_unavailable(version='1.0.0', otherthing=1)
def my_function_based_on_anypackage()
return anypackage.sayhi()
Classes¶
- class bronx.syntax.externalcode.ExternalCodeImportChecker(nickname='external')[source]¶
Bases:
objectCatches any import error and allow for the developer to test whether it succeeded or not.
See the example above.
- Parameters:
nickname (str) – The name of the external code to be imported
This decorator disables the provided object if the external code is unavailable.
- Parameters:
kwargs – A dictionary of requirements to check for
Exceptions¶
Bases:
ExceptionRaised by the decorated function/class whenever the import did not succeed.