本文整理汇总了Python中_string.formatter_field_name_split方法的典型用法代码示例。如果您正苦于以下问题:Python _string.formatter_field_name_split方法的具体用法?Python _string.formatter_field_name_split怎么用?Python _string.formatter_field_name_split使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_string
的用法示例。
在下文中一共展示了_string.formatter_field_name_split方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_format_field_name_split_errors
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def test_format_field_name_split_errors(self):
if is_cpython: #http://ironpython.codeplex.com/workitem/28224
temp = _string.formatter_field_name_split('') #Just ensure it doesn't throw
else:
self.assertRaisesMessage(ValueError, "empty field name", _string.formatter_field_name_split, '')
self.assertRaisesMessage(ValueError, "empty field name", _string.formatter_field_name_split, '[')
self.assertRaisesMessage(ValueError, "empty field name", _string.formatter_field_name_split, '.')
self.assertRaisesMessage(ValueError, "empty field name", _string.formatter_field_name_split, '[.abc')
self.assertRaisesMessage(ValueError, "empty field name", _string.formatter_field_name_split, '.abc')
errors = [ ("0[", "Missing ']' in format string"),
("abc.", "Empty attribute in format string"),
("abc[]", "Empty attribute in format string"),
]
for format, errorMsg in errors:
self.assertRaisesMessage(ValueError, errorMsg, list, _string.formatter_field_name_split(format)[1])
示例2: test_format_field_name_split
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def test_format_field_name_split(self):
tests = [ ('0', [long(0), []]),
('abc.foo', ['abc', [(True, 'foo')]]),
('abc[2]', ['abc', [(False, long(2))]]),
('1[2]', [long(1), [(False, long(2))]]),
('1.abc', [long(1), [(True, 'abc')]]),
('abc 2.abc', ['abc 2', [(True, 'abc')]]),
('abc!2.abc', ['abc!2', [(True, 'abc')]]),
('].abc', [']', [(True, 'abc')]]),
("abc[[]", ['abc', [(False, '[')]] ),
("abc[[[]", ['abc', [(False, '[[')]] ),
]
if not is_cpython: #http://ironpython.codeplex.com/workitem/28331
tests.append(("abc[2]#x", ['abc', [(False, long(2))]] ))
tests.append([allChars, [allChars, []]])
tests.append([allChars + '.foo', [allChars, [(True, 'foo')]]])
tests.append([allChars + '[2]', [allChars, [(False, long(2))]]])
for format, expected in tests:
res = list(_string.formatter_field_name_split(format))
res[1] = list(res[1])
self.assertEqual(res, expected)
示例3: _parse_field_name
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def _parse_field_name(field_name):
first, rest = _string.formatter_field_name_split(field_name)
funcs = []
for is_attr, key in rest:
if is_attr:
func = operator.attrgetter
else:
func = operator.itemgetter
try:
if ":" in key:
start, _, stop = key.partition(":")
stop, _, step = stop.partition(":")
start = int(start) if start else None
stop = int(stop) if stop else None
step = int(step) if step else None
key = slice(start, stop, step)
except TypeError:
pass # key is an integer
funcs.append(func(key))
return first, funcs
示例4: get_field
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def get_field(self, field_name, args, kwargs):
first, rest = _string.formatter_field_name_split(field_name)
obj = self.get_value(first, args, kwargs)
# loop through the rest of the field_name, doing
# getattr or getitem as needed
for is_attr, i in rest:
if is_attr:
obj = getattr(obj, i)
else:
obj = obj[i]
return obj, first
示例5: formatter_field_name_split
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def formatter_field_name_split(field_name):
return field_name._formatter_field_name_split()
示例6: get_field
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def get_field(self, field_name, args, kwargs):
first, rest = formatter_field_name_split(field_name)
obj = self.get_value(first, args, kwargs)
for is_attr, i in rest:
if is_attr:
obj = self._env.getattr(obj, i)
else:
obj = self._env.getitem(obj, i)
return obj, first
示例7: get_field
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def get_field(self, field_name, args, kwargs):
first, rest = _string.formatter_field_name_split(field_name)
try:
obj = self.get_value(first, args, kwargs)
except (IndexError, KeyError):
return "<Invalid>", first
# loop through the rest of the field_name, doing
# getattr or getitem as needed
# stops when reaches the depth of 2 or starts with _.
try:
for n, (is_attr, i) in enumerate(rest):
if n >= 2:
break
if is_attr:
if str(i).startswith("_"):
break
obj = getattr(obj, i)
else:
obj = obj[i]
else:
return obj, first
except (IndexError, KeyError):
pass
return "<Invalid>", first
示例8: test_formatter_field_name_split
# 需要导入模块: import _string [as 别名]
# 或者: from _string import formatter_field_name_split [as 别名]
def test_formatter_field_name_split(self):
def split(name):
items = list(_string.formatter_field_name_split(name))
items[1] = list(items[1])
return items
self.assertEqual(split("obj"), ["obj", []])
self.assertEqual(split("obj.arg"), ["obj", [(True, 'arg')]])
self.assertEqual(split("obj[key]"), ["obj", [(False, 'key')]])
self.assertEqual(split("obj.arg[key1][key2]"), [
"obj",
[(True, 'arg'),
(False, 'key1'),
(False, 'key2'),
]])
self.assertRaises(TypeError, _string.formatter_field_name_split, 1)