john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

decorator skip print parameters

# 2013-03-06 johnpfeiffer

from functools import wraps

def skip( ExceptionsToCheck , logger = None ):
    """Skip and Log if one of the Exceptions provided is raised
    :ExceptionsToCheck: tuple of exceptions to check
    :logger: logger to use. If None, print
    """
    def deco_skip( f ):

        @wraps( f )
        def f_skip( *args , **kwargs ):
            try:
                return f( *args , **kwargs )
            except ExceptionsToCheck as error:
                message = "SKIPPING due to %s" % ( str( error ) )
                if logger:
                    logger.warning( message )
                else:
                    print message

            return

        return f_skip  # true decorator

    return deco_skip

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 2013-03-06 johnpfeiffer

from functools import wraps
import o2lib

def skip_apisystemsexception_duplicate( logger = None ):
    """Skip and Log if one of the Exceptions provided is raised
    :ExceptionsToCheck: tuple of exceptions to check
    :logger: logger to use. If None, print
    """
    def deco_skip( f ):

        @wraps( f )
        def f_skip( *args , **kwargs ):
            try:
                return f( *args , **kwargs )
            except o2lib.ApiSystemsException as error:
                if error.errorCode == 2:
                    error_detail = o2lib.ApiSystemsErrorCode._VALUES_TO_NAMES[ error.errorCode ]
                    message = "SKIPPING: %s due to %s %s" % ( error_detail , str( error ) , repr( args ) )
                    if logger:
                        logger.warning( message )
                    else:
                        print message
                    return      # skip continuing the function

            raise error # if reached then propogate the error back up

        return f_skip  # true decorator

    return deco_skip

  • « decorator retry
  • decorator retry test »

Published

Mar 6, 2013

Category

python

~174 words

Tags

  • decorator 8
  • parameters 15
  • print 4
  • python 180
  • skip 5