[formatter] add 'i' and 'f' conversions (#6582)
https://github.com/mikf/gallery-dl/issues/6582#issuecomment-2792166608
This commit is contained in:
@@ -176,6 +176,18 @@ Conversion specifiers allow to *convert* the value to a different form or type.
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><code>i</code></td>
|
||||
<td>Convert value to <a href="https://docs.python.org/3/library/functions.html#int"><code>int</code></a></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><code>f</code></td>
|
||||
<td>Convert value to <a href="https://docs.python.org/3/library/functions.html#float"><code>float</code></a></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -233,7 +245,7 @@ Format specifiers can be used for advanced formatting by using the options provi
|
||||
<td><code>Foo Bar</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>{foo:L6/ .../}</code></td>
|
||||
<td><code>{foo:X6/ .../}</code></td>
|
||||
<td><code>Fo ...</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
||||
@@ -495,6 +495,8 @@ _CONVERSIONS = {
|
||||
"s": str,
|
||||
"r": repr,
|
||||
"a": ascii,
|
||||
"i": int,
|
||||
"f": float,
|
||||
}
|
||||
_FORMAT_SPECIFIERS = {
|
||||
"?": _parse_optional,
|
||||
|
||||
@@ -35,6 +35,8 @@ class TestFormatter(unittest.TestCase):
|
||||
"ds": "2010-01-01T01:00:00+0100",
|
||||
"dt": datetime.datetime(2010, 1, 1),
|
||||
"dt_dst": datetime.datetime(2010, 6, 1),
|
||||
"i_str": "12345",
|
||||
"f_str": "12.45",
|
||||
"name": "Name",
|
||||
"title1": "Title",
|
||||
"title2": "",
|
||||
@@ -70,6 +72,9 @@ class TestFormatter(unittest.TestCase):
|
||||
self._run_test("{a!L}", 11)
|
||||
self._run_test("{l!L}", 3)
|
||||
self._run_test("{d!L}", 3)
|
||||
self._run_test("{i_str!i}", 12345)
|
||||
self._run_test("{i_str!f}", 12345.0)
|
||||
self._run_test("{f_str!f}", 12.45)
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
self._run_test("{a!q}", "hello world")
|
||||
@@ -483,10 +488,10 @@ def noarg():
|
||||
fmt4 = formatter.parse("\fM " + path + ":lengths")
|
||||
|
||||
self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name")
|
||||
self.assertEqual(fmt2.format_map(self.kwdict), "126")
|
||||
self.assertEqual(fmt2.format_map(self.kwdict), "136")
|
||||
|
||||
self.assertEqual(fmt3.format_map(self.kwdict), "'Title' by Name")
|
||||
self.assertEqual(fmt4.format_map(self.kwdict), "126")
|
||||
self.assertEqual(fmt4.format_map(self.kwdict), "136")
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
self.assertEqual(fmt0.format_map(self.kwdict), "")
|
||||
|
||||
Reference in New Issue
Block a user