class objectDumper: ## ## @brief returns a string representation of given object attributes ## @return string multi-line string, listing object attributes and their values ## def toString(obj, prefix=None, format=None): if format == 'dictionary': output_format = "%s %s : %s," else: output_format = "%s\nobj.%s = %s" returnedString = '' for attr in dir(obj): if prefix == None or attr.find(prefix) == 0: returnedString = output_format % (returnedString, attr, getattr(obj, attr)) return (returnedString) toString = Callable(toString) # makes pseudo-static ## ## @brief dump given object as a string (attributes) ## def dump(obj): returnedString = objectDumper.toString(obj) print returnedString dump = Callable(dump) # makes pseudo-static
def signature(f): c = Callable(f) parts = [] name = c.code.co_name is_lambda = name == '<lambda>' if is_lambda: parts.append('lambda') if c.arguments: parts.append(' ') else: parts.append('def ') parts.append(name) parts.append('(') for arg in c.arguments: parts.append(argument(arg)) parts.append(', ') if c.arguments: parts.pop() if not is_lambda: parts.append(')') if 'return' in c.annotations: parts.append(' -> ') annot = c.annotations['return'] parts.append(getattr(annot, '__name__', repr(annot))) return ''.join(parts)
def test_default(): f = lambda a, b='x': None # noqa s = Callable(f).arguments a = s.get('a') assert not a.has_default b = s.get('b') assert b.has_default assert b.default == 'x'
def test_kwargify_exc(f, args, kwargs, exc): with pytest.raises(exc): Callable(f).kwargify(args, kwargs)
def test_kwargify(f, args, kwargs, merged): kwargified = Callable(f).kwargify(args, kwargs) assert kwargified == merged
def test_get(): c = Callable(test_signature_py2) assert not c.arguments['f'].has_default with pytest.raises(KeyError): c.arguments['xyz']