Example #1
0
def test_make_signature():
    # See GH 17608
    # Case where the func does not have default kwargs
    sig = make_signature(validate_kwargs)
    assert sig == (['fname', 'kwargs', 'compat_args'],
                   ['fname', 'kwargs', 'compat_args'])

    # Case where the func does have default kwargs
    sig = make_signature(deprecate_kwarg)
    assert sig == (['old_arg_name', 'new_arg_name',
                    'mapping=None', 'stacklevel=2'],
                   ['old_arg_name', 'new_arg_name', 'mapping', 'stacklevel'])
def test_make_signature():
    # See GH 17608
    # Case where the func does not have default kwargs
    sig = make_signature(validate_kwargs)
    assert sig == (['fname', 'kwargs',
                    'compat_args'], ['fname', 'kwargs', 'compat_args'])

    # Case where the func does have default kwargs
    sig = make_signature(deprecate_kwarg)
    assert sig == ([
        'old_arg_name', 'new_arg_name', 'mapping=None', 'stacklevel=2'
    ], ['old_arg_name', 'new_arg_name', 'mapping', 'stacklevel'])
Example #3
0
File: base.py Project: zkw03/pandas
def whitelist_method_generator(base, klass, whitelist):
    """
    Yields all GroupBy member defs for DataFrame/Series names in whitelist.

    Parameters
    ----------
    base : class
        base class
    klass : class
        class where members are defined.
        Should be Series or DataFrame
    whitelist : list
        list of names of klass methods to be constructed

    Returns
    -------
    The generator yields a sequence of strings, each suitable for exec'ing,
    that define implementations of the named methods for DataFrameGroupBy
    or SeriesGroupBy.

    Since we don't want to override methods explicitly defined in the
    base class, any such name is skipped.
    """

    method_wrapper_template = \
        """def %(name)s(%(sig)s) :
    \"""
    %(doc)s
    \"""
    f = %(self)s.__getattr__('%(name)s')
    return f(%(args)s)"""
    property_wrapper_template = \
        """@property
def %(name)s(self) :
    \"""
    %(doc)s
    \"""
    return self.__getattr__('%(name)s')"""

    for name in whitelist:
        # don't override anything that was explicitly defined
        # in the base class
        if hasattr(base, name):
            continue
        # ugly, but we need the name string itself in the method.
        f = getattr(klass, name)
        doc = f.__doc__
        doc = doc if type(doc) == str else ''
        if isinstance(f, types.MethodType):
            wrapper_template = method_wrapper_template
            decl, args = make_signature(f)
            # pass args by name to f because otherwise
            # GroupBy._make_wrapper won't know whether
            # we passed in an axis parameter.
            args_by_name = ['{0}={0}'.format(arg) for arg in args[1:]]
            params = {
                'name': name,
                'doc': doc,
                'sig': ','.join(decl),
                'self': args[0],
                'args': ','.join(args_by_name)
            }
        else:
            wrapper_template = property_wrapper_template
            params = {'name': name, 'doc': doc}
        yield wrapper_template % params
Example #4
0
def test_make_signature(func, expected):
    # see gh-17608
    assert make_signature(func) == expected
Example #5
0
def whitelist_method_generator(base, klass, whitelist):
    """
    Yields all GroupBy member defs for DataFrame/Series names in whitelist.

    Parameters
    ----------
    base : class
        base class
    klass : class
        class where members are defined.
        Should be Series or DataFrame
    whitelist : list
        list of names of klass methods to be constructed

    Returns
    -------
    The generator yields a sequence of strings, each suitable for exec'ing,
    that define implementations of the named methods for DataFrameGroupBy
    or SeriesGroupBy.

    Since we don't want to override methods explicitly defined in the
    base class, any such name is skipped.
    """

    method_wrapper_template = \
        """def %(name)s(%(sig)s) :
    \"""
    %(doc)s
    \"""
    f = %(self)s.__getattr__('%(name)s')
    return f(%(args)s)"""
    property_wrapper_template = \
        """@property
def %(name)s(self) :
    \"""
    %(doc)s
    \"""
    return self.__getattr__('%(name)s')"""

    for name in whitelist:
        # don't override anything that was explicitly defined
        # in the base class
        if hasattr(base, name):
            continue
        # ugly, but we need the name string itself in the method.
        f = getattr(klass, name)
        doc = f.__doc__
        doc = doc if type(doc) == str else ''
        if isinstance(f, types.MethodType):
            wrapper_template = method_wrapper_template
            decl, args = make_signature(f)
            # pass args by name to f because otherwise
            # GroupBy._make_wrapper won't know whether
            # we passed in an axis parameter.
            args_by_name = ['{0}={0}'.format(arg) for arg in args[1:]]
            params = {'name': name,
                      'doc': doc,
                      'sig': ','.join(decl),
                      'self': args[0],
                      'args': ','.join(args_by_name)}
        else:
            wrapper_template = property_wrapper_template
            params = {'name': name, 'doc': doc}
        yield wrapper_template % params
Example #6
0
def test_make_signature(func, expected):
    # see gh-17608
    assert make_signature(func) == expected