| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -212,19 +212,20 @@ def _vformat(self, format_string, args, kwargs, used_args, recursion_depth, | |||
| 212 | 212 | # this is some markup, find the object and do | |
| 213 | 213 | # the formatting | |
| 214 | 214 | ||
| 215 | - # handle arg indexing when empty field_names are given. | ||
| 216 | - if field_name == '': | ||
| 215 | + # handle arg indexing when empty field first parts are given. | ||
| 216 | + field_first, _ = _string.formatter_field_name_split(field_name) | ||
| 217 | + if field_first == '': | ||
| 217 | 218 | if auto_arg_index is False: | |
| 218 | 219 | raise ValueError('cannot switch from manual field ' | |
| 219 | 220 | 'specification to automatic field ' | |
| 220 | 221 | 'numbering') | |
| 221 | - field_name = str(auto_arg_index) | ||
| 222 | + field_name = str(auto_arg_index) + field_name | ||
| 222 | 223 | auto_arg_index += 1 | |
| 223 | - elif field_name.isdigit(): | ||
| 224 | + elif isinstance(field_first, int): | ||
| 224 | 225 | if auto_arg_index: | |
| 225 | - raise ValueError('cannot switch from manual field ' | ||
| 226 | - 'specification to automatic field ' | ||
| 227 | - 'numbering') | ||
| 226 | + raise ValueError('cannot switch from automatic field ' | ||
| 227 | + 'numbering to manual field ' | ||
| 228 | + 'specification') | ||
| 228 | 229 | # disable auto arg incrementing, if it gets | |
| 229 | 230 | # used later on, then an exception will be raised | |
| 230 | 231 | auto_arg_index = False | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | import unittest | |
| 2 | 2 | import string | |
| 3 | 3 | from string import Template | |
| 4 | + import types | ||
| 4 | 5 | ||
| 5 | 6 | ||
| 6 | 7 | class ModuleTest(unittest.TestCase): | |
@@ -101,6 +102,23 @@ def test_index_lookup(self): | |||
| 101 | 102 | with self.assertRaises(KeyError): | |
| 102 | 103 | fmt.format("{0[2]}{0[0]}", {}) | |
| 103 | 104 | ||
| 105 | + def test_auto_numbering_lookup(self): | ||
| 106 | + fmt = string.Formatter() | ||
| 107 | + namespace = types.SimpleNamespace(foo=types.SimpleNamespace(bar='baz')) | ||
| 108 | + widths = [None, types.SimpleNamespace(qux=4)] | ||
| 109 | + self.assertEqual( | ||
| 110 | + fmt.format("{.foo.bar:{[1].qux}}", namespace, widths), 'baz ') | ||
| 111 | + | ||
| 112 | + def test_auto_numbering_reenterability(self): | ||
| 113 | + class ReenteringFormatter(string.Formatter): | ||
| 114 | + def format_field(self, value, format_spec): | ||
| 115 | + if format_spec.isdigit() and int(format_spec) > 0: | ||
| 116 | + return self.format('{:{}}!', value, int(format_spec) - 1) | ||
| 117 | + else: | ||
| 118 | + return super().format_field(value, format_spec) | ||
| 119 | + fmt = ReenteringFormatter() | ||
| 120 | + self.assertEqual(fmt.format('{:{}}', 'X', 3), 'X!!!') | ||
| 121 | + | ||
| 104 | 122 | def test_override_get_value(self): | |
| 105 | 123 | class NamespaceFormatter(string.Formatter): | |
| 106 | 124 | def __init__(self, namespace={}): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Add attribute and item access support to :class:`string.Formatter` in auto-numbering mode, which allows format strings like '{.name}' and '{[1]}'. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments