コード例 #1
0
 def test_get_argspec_class_object(self):
     '''Tests get_argspec returns an ArgSpec for class constructors'''
     class BasicClass(object):
         ''' basic class for testing '''
         def __init__(self, myarg, mykwarg=None, *args, **kwargs):
             ''' Instantiates the BasicClass class '''
             pass
     
     try:
         get_argspec(BasicClass)
     except TypeError as err:
         self.fail("Class check failed: get_argspec" " raised '%s'" % err)
コード例 #2
0
    def test_get_argspec_class_object(self):
        '''Tests get_argspec returns an ArgSpec for class constructors'''
        class BasicClass(object):
            ''' basic class for testing '''
            def __init__(self, myarg, mykwarg=None, *args, **kwargs):
                ''' Instantiates the BasicClass class '''
                pass

        try:
            get_argspec(BasicClass)
        except TypeError as err:
            self.fail("Class check failed: get_argspec" " raised '%s'" % err)
コード例 #3
0
    def test_getargspec_callable_class(self):
        '''Tests get_argspec returns an ArgSpec for callable class instances'''
        class CallableClass(object):
            ''' CallableClass used for testing '''
            def __call__(self, myarg, mykwarg=None, *args, **kwargs):
                pass

        instance = CallableClass()

        try:
            get_argspec(instance)
        except TypeError as err:
            self.fail("Callable class check failed: get_argspec"
                      " raised '%s'" % err)
コード例 #4
0
 def test_getargspec_callable_class(self):
     '''Tests get_argspec returns an ArgSpec for callable class instances'''
     
     class CallableClass(object):
         ''' CallableClass used for testing '''
         def __call__(self, myarg, mykwarg=None, *args, **kwargs):
             pass
     
     instance = CallableClass()
     
     try:
         get_argspec(instance)
     except TypeError as err:
         self.fail("Callable class check failed: get_argspec"
                   " raised '%s'" % err)
コード例 #5
0
def validate_function_args(func, args, kwargs):
    ''' Make sure all required and keyword parameters are passed in
        to the constructor of the checkpoint class.
        Python 2.7's  inspect module has a getcallargs() function that
        does what this function does.  So, when we migrate to
        Python 2.7 or higher, this function can be replaced with the one from
        Python 2.7. '''

    # make a copy of kwargs so this function can manipulate the copy
    # instead of the acutal dictionary
    kwargs_copy = kwargs.copy()

    spec = get_argspec(func)

    num_spec_args = len(spec.args)
    arg_value = dict()

    # fill with expected arguments
    arg_value.update(izip(spec.args, args))

    # in case there are expected arguments that a user has
    # specified by name, fill them in here.
    for arg_ in spec.args:
        if arg_ in kwargs_copy:
            if arg_ in arg_value:
                raise TypeError(arg_ + "is specified twice in the arguments")
            else:
                arg_value[arg_] = kwargs_copy.pop(arg_)

    default_val = {}
    if spec.defaults is not None:
        # match the defaults with the keywords
        default_val = {}
        i = 1
        for default in reversed(spec.defaults):
            key = spec.args[num_spec_args - i]
            i += 1
            default_val[key] = default

    # fill in any missing values with the defaults
    for key_ in default_val.keys():
        if key_ not in arg_value:
            arg_value[key_] = default_val[key_]

    # Go through all the required arguments, they should all have values now
    for arg_ in spec.args:
        if arg_ not in arg_value:
            raise TypeError("Argument %s is required, but not specified. \
                            spec = %s, args = %s, kwargs = %s" % \
                            (arg_, spec, args, kwargs))

    # Make sure variable keyword arguments are not specified if not expected

    var_kw_len = len(kwargs_copy)

    if (spec.keywords is None) and (var_kw_len != 0):
        # We don't expect any variable keywords, but some is supplied
        raise TypeError("Variable keyword argument not expected, but "
                        "%d unexpected keyword argument supplied. Unexpected "
                        "keywords are: %s" % (var_kw_len, str(kwargs_copy)))
コード例 #6
0
def validate_function_args(func, args, kwargs):
    ''' Make sure all required and keyword parameters are passed in
        to the constructor of the checkpoint class.
        Python 2.7's  inspect module has a getcallargs() function that
        does what this function does.  So, when we migrate to
        Python 2.7 or higher, this function can be replaced with the one from
        Python 2.7. '''

    # make a copy of kwargs so this function can manipulate the copy
    # instead of the acutal dictionary
    kwargs_copy = kwargs.copy()

    spec = get_argspec(func)

    num_spec_args = len(spec.args)
    arg_value = dict()

    # fill with expected arguments
    arg_value.update(izip(spec.args, args))

    # in case there are expected arguments that a user has
    # specified by name, fill them in here.
    for arg_ in spec.args:
        if arg_ in kwargs_copy:
            if arg_ in arg_value:
                raise TypeError(arg_ + "is specified twice in the arguments")
            else:
                arg_value[arg_] = kwargs_copy.pop(arg_)

    default_val = {}
    if spec.defaults is not None:
        # match the defaults with the keywords
        default_val = {}
        i = 1
        for default in reversed(spec.defaults):
            key = spec.args[num_spec_args - i]
            i += 1
            default_val[key] = default

    # fill in any missing values with the defaults
    for key_ in default_val.keys():
        if key_ not in arg_value:
            arg_value[key_] = default_val[key_]

    # Go through all the required arguments, they should all have values now
    for arg_ in spec.args:
        if arg_ not in arg_value:
            raise TypeError("Argument %s is required, but not specified. \
                            spec = %s, args = %s, kwargs = %s"                                                               % \
                            (arg_, spec, args, kwargs))

    # Make sure variable keyword arguments are not specified if not expected

    var_kw_len = len(kwargs_copy)

    if (spec.keywords is None) and (var_kw_len != 0):
        # We don't expect any variable keywords, but some is supplied
        raise TypeError("Variable keyword argument not expected, but "
                        "%d unexpected keyword argument supplied. Unexpected "
                        "keywords are: %s" % (var_kw_len, str(kwargs_copy)))
コード例 #7
0
    def test_get_argspec_function(self):
        '''Tests get_argspec returns an ArgSpec for functions'''
        def basic_function(myarg, mykwarg=None, *args, **kwargs):
            ''' basic function for testing '''
            pass

        lambda_func = lambda x: x * 2

        try:
            get_argspec(basic_function)
        except TypeError as err:
            self.fail("basic_function check failed:"
                      " get_argspec raised '%s'" % err)

        try:
            get_argspec(lambda_func)
        except TypeError as err:
            self.fail("lamda check failed: get_argspec" " raised '%s'" % err)
コード例 #8
0
 def test_get_argspec_function(self):
     '''Tests get_argspec returns an ArgSpec for functions'''
     
     def basic_function(myarg, mykwarg=None, *args, **kwargs):
         ''' basic function for testing '''
         pass
     
     lambda_func = lambda x: x * 2
     
     try:
         get_argspec(basic_function)
     except TypeError as err:
         self.fail("basic_function check failed:"
                   " get_argspec raised '%s'" % err)
     
     try:
         get_argspec(lambda_func)
     except TypeError as err:
         self.fail("lamda check failed: get_argspec"
                   " raised '%s'" % err)