Diaper patternΒΆ
returns: | Result of the function call if no exception is raised. Otherwise it returns None. |
---|
Usage:
import diaper
# call it directly
diaper(function, *function_args, **function_kwargs)
# as a context manager
with diaper:
do_terrible_things_here()
# see if the diaper caught anything:
with diaper:
raise Exception
assert diaper.smelly
# diapers are automatically changed on use
with diaper:
pass
assert not diaper.smelly
# diapers can be manually changed and (somehow) soiled
diaper.soil()
assert diaper.smelly
diaper.change()
assert not diaper.smelly
# diapers can be manipulated in the context with `as`
with diaper as nappy:
# But, of course, this serves no useful purpose
# Since you can just as easily manipulate diaper
assert nappy is diaper
if something_smells_funny:
nappy.soil()
assert diaper.smelly and nappy.smelly
# diapers can be inspected with redundant properties
assert diaper.clean == not diaper.smelly
assert diaper.dirty == diaper.smelly
# diaper can be used as a decorator
@diaper.wrap
def explode():
raise Exception
explode()
assert diaper.smelly
Note
diapers are probably thread-safe