当前位置: 首页>>代码示例>>Python>>正文


Python _string.formatter_field_name_split方法代码示例

本文整理汇总了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]) 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:19,代码来源:test_strformat.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:25,代码来源:test_strformat.py

示例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 
开发者ID:mikf,项目名称:gallery-dl,代码行数:25,代码来源:util.py

示例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 
开发者ID:war-and-code,项目名称:jawfish,代码行数:16,代码来源:string.py

示例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() 
开发者ID:remg427,项目名称:misp42splunk,代码行数:4,代码来源:sandbox.py

示例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 
开发者ID:remg427,项目名称:misp42splunk,代码行数:11,代码来源:sandbox.py

示例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 
开发者ID:kyb3r,项目名称:modmail,代码行数:28,代码来源:models.py

示例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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:17,代码来源:test_unicode.py


注:本文中的_string.formatter_field_name_split方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。