def test_get_func_args(self): def f1(a, b, c): pass def f2(a, b=None, c=None): pass class A(object): def __init__(self, a, b, c): pass def method(self, a, b, c): pass class Callable(object): def __call__(self, a, b, c): pass a = A(1, 2, 3) cal = Callable() self.assertEqual(get_func_args(f1), ['a', 'b', 'c']) self.assertEqual(get_func_args(f2), ['a', 'b', 'c']) self.assertEqual(get_func_args(A), ['a', 'b', 'c']) self.assertEqual(get_func_args(a.method), ['a', 'b', 'c']) self.assertEqual(get_func_args(cal), ['a', 'b', 'c']) self.assertEqual(get_func_args(object), []) # TODO: how do we fix this to return the actual argument names? self.assertEqual(get_func_args(unicode.split), []) self.assertEqual(get_func_args(" ".join), [])
def __init__(self, name, xpath=None, quant='*', value=None, callback=None, group=None, children=None, css=None, filter=None): ''' name - the parsed item will be stored in a dictionary on this index (not to store the item, use '_' as a prefix of the name). xpath - xpath to the item. quant - used for validation. The expected number of items to be parsed. value - specify it, if you want the items to be extracted. Otherwise selector objects will be returned. callback - callback function to be called on each found item. It can take named argument "context", which is dictionary containing additionnal values. group - if not None, all the child nodes will be stored under one dictionary entry of group's name children - list of nested S objects. For each item found, each child will be called with the item as the selector. css - css selector, in a case xpath is not defined filter - one-argument function. Given the node from the xpath, return true, if to the node. Otherwise return False. `quant` is checked AFTER the filter is applied. ''' if (xpath is None) == (css is None): raise TypeError('Exactly one of `xpath` or `css` arguments must be specified.') self.name = name if xpath is not None: self.raw_xpath = xpath elif css_supported: self.raw_xpath = GenericTranslator().css_to_xpath(css) else: raise TypeError('Css selectors not supported, install cssselect library.') self.hashed_namespaces = None self.compiled_xpath = None self.quant = Quantity(quant) self.raw_value = value self.compiled_value = None self.callback = callback self.context_callback = callback and 'context' in get_func_args(callback) self.group = group self.children = children if children is not None else [] self.filter = filter
def __init__(self, name, xpath=None, quant='*', value=None, callback=None, group=None, children=None, css=None, filter=None): ''' name - the parsed item will be stored in a dictionary on this index (not to store the item, use '_' as a prefix of the name). xpath - xpath to the item. quant - used for validation. The expected number of items to be parsed. value - specify it, if you want the items to be extracted. Otherwise selector objects will be returned. callback - callback function to be called on each found item. It can take named argument "context", which is dictionary containing additionnal values. group - if not None, all the child nodes will be stored under one dictionary entry of group's name children - list of nested S objects. For each item found, each child will be called with the item as the selector. css - css selector, in a case xpath is not defined filter - one-argument function. Given the node from the xpath, return true, if to the node. Otherwise return False. `quant` is checked AFTER the filter is applied. ''' if (xpath is None) == (css is None): raise TypeError( 'Exactly one of `xpath` or `css` arguments must be specified.') self.name = name if xpath is not None: self.raw_xpath = xpath elif css_supported: self.raw_xpath = GenericTranslator().css_to_xpath(css) else: raise TypeError( 'Css selectors not supported, install cssselect library.') self.hashed_namespaces = None self.compiled_xpath = None self.quant = Quantity(quant) self.raw_value = value self.compiled_value = None self.callback = callback self.context_callback = callback and 'context' in get_func_args( callback) self.group = group self.children = children if children is not None else [] self.filter = filter
def wrap_context(function, context): if 'context' in get_func_args(function): return partial(function, context=context) else: return function