def ds_item(definition, data): if isinstance(definition, XObject): return (~definition)(data) if isinstance(definition, basestring): return StringFormatter(definition)(data) if callable(definition): return definition(data) try: return DSBuilder(definition)(data) except NoBuilder: # static item return definition
def test_unicode2(self): f = StringFormatter(u'Asdf {0}') assert f(u'Žluťoučký kůň') == u'Asdf Žluťoučký kůň'
def test_format_one_arg(self): f = StringFormatter('This is {0}!!1') assert f('Spartah') == 'This is Spartah!!1'
def test_format_dict(self): f = StringFormatter('{a} and {b}') assert f(dict(a='A', b='B')) == 'A and B'
def test_format_generator(self): f = StringFormatter('{0} + {0} = {1}') assert f(xrange(1, 3)) == '1 + 1 = 2'
def test_format_list(self): f = StringFormatter('{0} + {0} = {1}') assert f([1, 2]) == '1 + 1 = 2'
def test_format_tuple(self): f = StringFormatter('{0} + {0} = {1}') assert f((1, 2)) == '1 + 1 = 2'
def auto_string_formatter_wrapper(function, *args, **kwargs): if isinstance(function, string_types): function = StringFormatter(function) return func(function, *args, **kwargs)