[formatter] add 'I' format specifier - identity
This commit is contained in:
@@ -375,6 +375,12 @@ Format specifiers can be used for advanced formatting by using the options provi
|
|||||||
<td><code>{date:Olocal/}</code></td>
|
<td><code>{date:Olocal/}</code></td>
|
||||||
<td><code>2010-01-01 01:00:00</code></td>
|
<td><code>2010-01-01 01:00:00</code></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><code>I</code></td>
|
||||||
|
<td>Return the current value as is.<br>Do not convert it to <code>str</code></td>
|
||||||
|
<td><code>{num:I}</code></td>
|
||||||
|
<td><code>1</code></td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|||||||
@@ -430,6 +430,10 @@ def _parse_maxlen(format_spec, default):
|
|||||||
return mlen
|
return mlen
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_identity(format_spec, default):
|
||||||
|
return util.identity
|
||||||
|
|
||||||
|
|
||||||
def _parse_join(format_spec, default):
|
def _parse_join(format_spec, default):
|
||||||
separator, _, format_spec = format_spec.partition(_SEPARATOR)
|
separator, _, format_spec = format_spec.partition(_SEPARATOR)
|
||||||
join = separator[1:].join
|
join = separator[1:].join
|
||||||
@@ -609,6 +613,7 @@ _FORMAT_SPECIFIERS = {
|
|||||||
"A": _parse_arithmetic,
|
"A": _parse_arithmetic,
|
||||||
"C": _parse_conversion,
|
"C": _parse_conversion,
|
||||||
"D": _parse_datetime,
|
"D": _parse_datetime,
|
||||||
|
"I": _parse_identity,
|
||||||
"J": _parse_join,
|
"J": _parse_join,
|
||||||
"L": _parse_maxlen,
|
"L": _parse_maxlen,
|
||||||
"M": _parse_map,
|
"M": _parse_map,
|
||||||
|
|||||||
@@ -369,6 +369,15 @@ class TestFormatter(unittest.TestCase):
|
|||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
self._run_test("{t:Mname", "")
|
self._run_test("{t:Mname", "")
|
||||||
|
|
||||||
|
def test_specifier_identity(self):
|
||||||
|
self._run_test("{a:I}", self.kwdict["a"])
|
||||||
|
self._run_test("{i:I}", self.kwdict["i"])
|
||||||
|
self._run_test("{dt:I}", self.kwdict["dt"])
|
||||||
|
|
||||||
|
self._run_test("{t!D:I}", self.kwdict["dt"])
|
||||||
|
self._run_test("{t!D:I/O+01:30}", self.kwdict["dt"])
|
||||||
|
self._run_test("{i:A+1/I}", self.kwdict["i"]+1)
|
||||||
|
|
||||||
def test_chain_special(self):
|
def test_chain_special(self):
|
||||||
# multiple replacements
|
# multiple replacements
|
||||||
self._run_test("{a:Rh/C/RE/e/RL/l/}", "Cello wOrld")
|
self._run_test("{a:Rh/C/RE/e/RL/l/}", "Cello wOrld")
|
||||||
|
|||||||
Reference in New Issue
Block a user