Python _string.formatter_field_name_split() Examples

The following are 30 code examples of _string.formatter_field_name_split(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module _string , or try the search function .
Example #1
Source File: test_strformat.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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]) 
Example #2
Source File: test_strformat.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: util.py    From gallery-dl with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #4
Source File: sandbox.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #5
Source File: sandbox.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #6
Source File: sandbox.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #7
Source File: sandbox.py    From planespotter with MIT License 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #8
Source File: sandbox.py    From planespotter with MIT License 5 votes vote down vote up
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 
Example #9
Source File: sandbox.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #10
Source File: sandbox.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
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 
Example #11
Source File: string.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #12
Source File: test_unicode.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #13
Source File: string.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #14
Source File: sandbox.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #15
Source File: test_unicode.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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) 
Example #16
Source File: sandbox.py    From EDCOP with Apache License 2.0 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #17
Source File: sandbox.py    From EDCOP with Apache License 2.0 5 votes vote down vote up
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 
Example #18
Source File: string.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #19
Source File: sandbox.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #20
Source File: sandbox.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #21
Source File: sandbox.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #22
Source File: sandbox.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
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 
Example #23
Source File: string.py    From android_universal with MIT License 5 votes vote down vote up
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 
Example #24
Source File: sandbox.py    From android_universal with MIT License 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split() 
Example #25
Source File: sandbox.py    From android_universal with MIT License 5 votes vote down vote up
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 
Example #26
Source File: test_unicode.py    From android_universal with MIT License 5 votes vote down vote up
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) 
Example #27
Source File: sandbox.py    From odoo12-x64 with GNU General Public License v3.0 5 votes vote down vote up
def formatter_field_name_split(field_name):
            return field_name._formatter_field_name_split() 
Example #28
Source File: sandbox.py    From odoo12-x64 with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #29
Source File: sandbox.py    From OpenXR-SDK-Source with Apache License 2.0 5 votes vote down vote up
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 
Example #30
Source File: sandbox.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def formatter_field_name_split(field_name):
        return field_name._formatter_field_name_split()