File: string.templatelib.rst

package info (click to toggle)
python3.14 3.14.0~rc1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 126,824 kB
  • sloc: python: 745,274; ansic: 713,752; xml: 31,250; sh: 5,822; cpp: 4,063; makefile: 1,988; objc: 787; lisp: 502; javascript: 136; asm: 75; csh: 12
file content (313 lines) | stat: -rw-r--r-- 10,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
:mod:`!string.templatelib` --- Support for template string literals
===================================================================

.. module:: string.templatelib
   :synopsis: Support for template string literals.

**Source code:** :source:`Lib/string/templatelib.py`

--------------

.. seealso::

   * :ref:`Format strings <f-strings>`
   * :ref:`T-string literal syntax <t-strings>`


.. _template-strings:

Template strings
----------------

.. versionadded:: 3.14

Template strings are a formatting mechanism that allows for deep control over
how strings are processed. You can create templates using
:ref:`t-string literal syntax <t-strings>`, which is identical to
:ref:`f-string syntax <f-strings>` but uses a ``t`` instead of an ``f``.
While f-strings evaluate to ``str``, t-strings create a :class:`Template`
instance that gives you access to the static and interpolated (in curly braces)
parts of a string *before* they are combined.


.. _templatelib-template:

Template
--------

The :class:`!Template` class describes the contents of a template string.

:class:`!Template` instances are immutable: their attributes cannot be
reassigned.

.. class:: Template(*args)

   Create a new :class:`!Template` object.

   :param args: A mix of strings and :class:`Interpolation` instances in any order.
   :type args: str | Interpolation

   The most common way to create a :class:`!Template` instance is to use the
   :ref:`t-string literal syntax <t-strings>`. This syntax is identical to that of
   :ref:`f-strings <f-strings>` except that it uses a ``t`` instead of an ``f``:

   >>> name = "World"
   >>> template = t"Hello {name}!"
   >>> type(template)
   <class 'string.templatelib.Template'>

   Templates ars stored as sequences of literal :attr:`~Template.strings`
   and dynamic :attr:`~Template.interpolations`.
   A :attr:`~Template.values` attribute holds the interpolation values:

   >>> template.strings
   ('Hello ', '!')
   >>> template.interpolations
   (Interpolation('World', ...),)
   >>> template.values
   ('World',)

   The :attr:`!strings` tuple has one more element than :attr:`!interpolations`
   and :attr:`!values`; the interpolations “belong” between the strings.
   This may be easier to understand when tuples are aligned::

      template.strings:  ('Hello ',          '!')
      template.values:   (          'World',    )

   While literal syntax is the most common way to create :class:`!Template`
   instances, it is also possible to create them directly using the constructor:

   >>> from string.templatelib import Interpolation, Template
   >>> name = "World"
   >>> template = Template("Hello, ", Interpolation(name, "name"), "!")
   >>> list(template)
   ['Hello, ', Interpolation('World', 'name', None, ''), '!']

   If two or more consecutive strings are passed, they will be concatenated
   into a single value in the :attr:`~Template.strings` attribute. For example,
   the following code creates a :class:`Template` with a single final string:

   >>> from string.templatelib import Template
   >>> template = Template("Hello ", "World", "!")
   >>> template.strings
   ('Hello World!',)

   If two or more consecutive interpolations are passed, they will be treated
   as separate interpolations and an empty string will be inserted between them.
   For example, the following code creates a template with empty placeholders
   in the :attr:`~Template.strings` attribute:

   >>> from string.templatelib import Interpolation, Template
   >>> template = Template(Interpolation("World", "name"), Interpolation("!", "punctuation"))
   >>> template.strings
   ('', '', '')

   .. attribute:: strings
       :type: tuple[str, ...]

       A :ref:`tuple <tut-tuples>` of the static strings in the template.

       >>> name = "World"
       >>> t"Hello {name}!".strings
       ('Hello ', '!')

       Empty strings *are* included in the tuple:

       >>> name = "World"
       >>> t"Hello {name}{name}!".strings
       ('Hello ', '', '!')

       The ``strings`` tuple is never empty, and always contains one more
       string than the ``interpolations`` and ``values`` tuples:

       >>> t"".strings
       ('',)
       >>> t"".values
       ()
       >>> t"{'cheese'}".strings
       ('', '')
       >>> t"{'cheese'}".values
       ('cheese',)

   .. attribute:: interpolations
       :type: tuple[Interpolation, ...]

       A tuple of the interpolations in the template.

       >>> name = "World"
       >>> t"Hello {name}!".interpolations
       (Interpolation('World', 'name', None, ''),)

       The ``interpolations`` tuple may be empty and always contains one fewer
       values than the ``strings`` tuple:

       >>> t"Hello!".interpolations
       ()

   .. attribute:: values
       :type: tuple[Any, ...]

       A tuple of all interpolated values in the template.

       >>> name = "World"
       >>> t"Hello {name}!".values
       ('World',)

       The ``values`` tuple always has the same length as the
       ``interpolations`` tuple. It is equivalent to
       ``tuple(i.value for i in template.interpolations)``.

   .. describe:: iter(template)

       Iterate over the template, yielding each string and
       :class:`Interpolation` in order.

       >>> name = "World"
       >>> list(t"Hello {name}!")
       ['Hello ', Interpolation('World', 'name', None, ''), '!']

       Empty strings are *not* included in the iteration:

       >>> name = "World"
       >>> list(t"Hello {name}{name}")
       ['Hello ', Interpolation('World', 'name', None, ''), Interpolation('World', 'name', None, '')]

   .. describe:: template + other
                 template += other

       Concatenate this template with another, returning a new
       :class:`!Template` instance:

       >>> name = "World"
       >>> list(t"Hello " + t"there {name}!")
       ['Hello there ', Interpolation('World', 'name', None, ''), '!']

       Concatenation between a :class:`!Template` and a ``str`` is *not* supported.
       This is because it is ambiguous whether the string should be treated as
       a static string or an interpolation. If you want to concatenate a
       :class:`!Template` with a string, you should either wrap the string
       directly in a :class:`!Template` (to treat it as a static string) or use
       an :class:`!Interpolation` (to treat it as dynamic):

       >>> from string.templatelib import Template, Interpolation
       >>> template = t"Hello "
       >>> # Treat "there " as a static string
       >>> template += Template("there ")
       >>> # Treat name as an interpolation
       >>> name = "World"
       >>> template += Template(Interpolation(name, "name"))
       >>> list(template)
       ['Hello there ', Interpolation('World', 'name', None, '')]


.. class:: Interpolation(value, expression="", conversion=None, format_spec="")

   Create a new :class:`!Interpolation` object.

   :param value: The evaluated, in-scope result of the interpolation.
   :type value: object

   :param expression: The text of a valid Python expression, or an empty string.
   :type expression: str

   :param conversion: The optional :ref:`conversion <formatstrings>` to be used, one of r, s, and a.
   :type conversion: ``Literal["a", "r", "s"] | None``

   :param format_spec: An optional, arbitrary string used as the :ref:`format specification <formatspec>` to present the value.
   :type format_spec: str

   The :class:`!Interpolation` type represents an expression inside a template string.

   :class:`!Interpolation` instances are immutable: their attributes cannot be
   reassigned.

   .. attribute:: value

       :returns: The evaluated value of the interpolation.
       :type: object

       >>> t"{1 + 2}".interpolations[0].value
       3

   .. attribute:: expression

       :returns: The text of a valid Python expression, or an empty string.
       :type: str

       The :attr:`~Interpolation.expression` is the original text of the
       interpolation's Python expression, if the interpolation was created
       from a t-string literal. Developers creating interpolations manually
       should either set this to an empty string or choose a suitable valid
       Python expression.

       >>> t"{1 + 2}".interpolations[0].expression
       '1 + 2'

   .. attribute:: conversion

       :returns: The conversion to apply to the value, or ``None``.
       :type: ``Literal["a", "r", "s"] | None``

       The :attr:`!Interpolation.conversion` is the optional conversion to apply
       to the value:

       >>> t"{1 + 2!a}".interpolations[0].conversion
       'a'

       .. note::

         Unlike f-strings, where conversions are applied automatically,
         the expected behavior with t-strings is that code that *processes* the
         :class:`!Template` will decide how to interpret and whether to apply
         the :attr:`!Interpolation.conversion`.

   .. attribute:: format_spec

       :returns: The format specification to apply to the value.
       :type: str

       The :attr:`!Interpolation.format_spec` is an optional, arbitrary string
       used as the format specification to present the value:

       >>> t"{1 + 2:.2f}".interpolations[0].format_spec
       '.2f'

       .. note::

         Unlike f-strings, where format specifications are applied automatically
         via the :func:`format` protocol, the expected behavior with
         t-strings is that code that *processes* the :class:`!Template` will
         decide how to interpret and whether to apply the format specification.
         As a result, :attr:`!Interpolation.format_spec` values in
         :class:`!Template` instances can be arbitrary strings, even those that
         do not necessarily conform to the rules of Python's :func:`format`
         protocol.

   Interpolations support pattern matching, allowing you to match against
   their attributes with the :ref:`match statement <match>`:

   >>> from string.templatelib import Interpolation
   >>> interpolation = Interpolation(3.0, "1 + 2", None, ".2f")
   >>> match interpolation:
   ...     case Interpolation(value, expression, conversion, format_spec):
   ...         print(value, expression, conversion, format_spec)
   ...
   3.0 1 + 2 None .2f


Helper functions
----------------

.. function:: convert(obj, /, conversion)

   Applies formatted string literal :ref:`conversion <formatstrings-conversion>`
   semantics to the given object *obj*.
   This is frequently useful for custom template string processing logic.

   Three conversion flags are currently supported:

   * ``'s'`` which calls :func:`str` on the value,
   * ``'r'`` which calls :func:`repr`, and
   * ``'a'`` which calls :func:`ascii`.

   If the conversion flag is ``None``, *obj* is returned unchanged.