Package: terse
This module provides a collection of function decorators to handle
common procedures done on the entry and exit points.InstallationThis library can be installed using pip with the following command.pipinstallterseDirectoryIn DirectoryBefore the function is executed, the current working directory is saved
and the directory is changed to the providedpath. After the
function has completed, the directory is changed to the saved working
directory.Caution: This is intended for single threaded usage. Threads
should use absolute paths.fromterseimportin_dirfromglobimportglobimportos# Returns all files and directories at file system's root.@in_dir('/')defroot_files():returnglob('*')# Prints current working directory.print(os.getcwd())# Prints fils gathered from root.print(root_files())# Demonstrates current working directory is preserved.print(os.getcwd())InvokeMainSets the function up to be the file’s main function. If the file ismain, then the function is executed. The return value will attempt
to be converted to a value that can be passed to sys.exit; however if no
value can be converted, it’s assumed the function exited successfully.Traditionally in Python you do the following to create the main entry
point in the file.defmain():print("Hello World!")if__name__=='__main__':main()This can be replaced by the following.fromterseimportmain@maindefmain_impl():print("Hello World!")On ExitNo RaiseDecorator will catch all exceptions leaked or produced by callee. If
provided withcallbackfunction, then the callback will only be
invoked when an exception occurs. If provided withinstead_return,
then the value will be returned when an exception occurs.fromterseimportno_exceptdeflog(function,exception):print('LOG:%s'%exception)# If exception is given, raise given exception.# Otherwise, return True@no_raise(instead_return=False,callback=log)defexample(exception=None):ifexception:raiseexceptionreturnTrue# Example with no parameters raises no exceptions,# makes no calls to log, returns True.assertexample()==True# Example with parameter raises given exception,# makes a call to log, surpresses exception and# returns False.assertexample(False)==FalseOn ReturnedDecorator will invokecallbackwhenever a value inargsis
returned by function.fromterseimporton_returnedfromenumimportEnumclassStatus(Enum):SUCCESS=0FAILED=1CONNECTION_FAILED=2DISK_FAILED=3deflog(function,returned):print("LOG:%s"%returned)# The following are examples for two uses of on_returned.# First is for any return value and the second is for a# set of return values.# ANY RETURN VALUE# Function example_any returns any value passed in.# on_returned will invoke log for any return value.@on_returned(log)defexample_any(val):returnval# Log invoked: prints "LOG: Status.SUCCESS"assertexample_any(Status.SUCCESS)==Status.SUCCESS# Log invoked: prints "LOG: Status.FAILED"assertexample_any(Status.FAILED)==Status.FAILED# Log invoked: prints "LOG: None"assertexample_any(None)==None# Log invoked: prints "LOG:1"assertexample_any(1)==1# SET OF RETURN VALUES# Function example_set returns any value passed in.# on_returned will invoke log whenever values FAILED,# CONNECTION_FAILED or DISK_FAILED from enum Status are# returned by example.@on_returned(log,Status.FAILED,Status.CONNECTION_FAILED,Status.DISK_FAILED)defexample_set(val):returnval# log is not invoked.assertexample_set(Status.SUCCESS)==Status.SUCCESS# Log invoked: prints "LOG: Status.FAILED"assertexample_set(Status.FAILED)==Status.FAILED# Log invoked: prints "LOG: Status.CONNECTION_FAILURE"assertexample_set(Status.CONNECTION_FAILED)==Status.CONNECTION_FAILED# Log invoked: prints "LOG: Status.DISK_FAILURE"assertexample_set(Status.DISK_FAILED)==Status.DISK_FAILEDOn RaisedDecorator will invokecallbackwhenever exception of type inargsis raised by function.fromterseimporton_exceptiondeflog(function,exception):print(returned)# The following are examples for two uses of on_raised.# First is for any exception and the second is a set of# exceptions.# ANY EXCEPTION EXAMPLE# If exception is given, raise given exception.# Otherwise, return True# on_raised will invoke log for all exceptions# raised by example_any.@on_raised(log)defexample_any(exception=None)ifexception:raiseexceptionreturnTrue# example_any is given no exceptions to throw.# Therefore, it will raise nothing and return True.assertexample_any()==True# example_any is given ZeroDivisionError exception.# It will raise ZeroDivisionError instance.# on_raised detects exception raised and invokes log.try:example_any(ZeroDivisionError())assertFalseexceptZeroDivisionError:pass# example_any is given ValueError exception.# It will raise ValueError instance.# on_raised detects exception raised and invokes log.try:example_any(ValueError())assertFalseexceptValueError:pass# example_any is given KeyError exception.# It will raise KeyError instance.# on_raised detects exception raised and invokes log.try:example_any(KeyError())assertFalseexceptValueError:pass# SET OF EXCEPTIONS EXAMPLE# If exception is given, raise given exception.# Otherwise, return True.# on_raised will only invoke log for ZeroDivisionError# and ValueError. All other exceptions are ignored by# on_raised.@on_raised(log,ZeroDivisionError,ValueError)defexample_set(exception=None)ifexception:raiseexceptionreturnTrue# example_set is given no exceptions to throw.# Therefore, it will raise nothing and return True.assertexample_set()==True# example_set is given ZeroDivisionError exception.# It will raise ZeroDivisionError instance.# on_raised detects exception raised and invokes log.try:example_set(ZeroDivisionError())assertFalseexceptZeroDivisionError:pass# example_set is given ValueError exception.# It will raise ValueError instance.# on_raised detects exception raised and invokes log.try:example_set(ValueError())assertFalseexceptValueError:pass# example_set is given KeyError exception.# It will raise KeyError instance.# example_set detects exception, but it will not# invoke log because KeyError is not in the set of# exceptions to be tracked.try:example_set(KeyError())assertFalseexceptValueError:pass