[Python-checkins] cpython (3.3): Added example to recently added cookbook entry.

vinay.sajip python-checkins at python.org
Fri Jan 17 19:36:52 CET 2014


http://hg.python.org/cpython/rev/4d867dc0d5fb
changeset:   88525:4d867dc0d5fb
branch:      3.3
parent:      88520:195793d5208d
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Fri Jan 17 18:36:02 2014 +0000
summary:
  Added example to recently added cookbook entry.

files:
  Doc/howto/logging-cookbook.rst |  28 ++++++++++++++++++++++
  1 files changed, 28 insertions(+), 0 deletions(-)


diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -1920,3 +1920,31 @@
 ``_`` for the message (or perhaps ``__``, if you are using ``_`` for
 localization).
 
+Examples of this approach are given below. Firstly, formatting with
+:meth:`str.format`::
+
+    >>> __ = BraceMessage
+    >>> print(__('Message with {0} {1}', 2, 'placeholders'))
+    Message with 2 placeholders
+    >>> class Point: pass
+    ...
+    >>> p = Point()
+    >>> p.x = 0.5
+    >>> p.y = 0.5
+    >>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})', point=p))
+    Message with coordinates: (0.50, 0.50)
+
+Secondly, formatting with :class:`string.Template`::
+
+    >>> __ = DollarMessage
+    >>> print(__('Message with $num $what', num=2, what='placeholders'))
+    Message with 2 placeholders
+    >>>
+
+One thing to note is that you pay no significant performance penalty with this
+approach: the actual formatting happens not when you make the logging call, but
+when (and if) the logged message is actually about to be output to a log by a
+handler. So the only slightly unusual thing which might trip you up is that the
+parentheses go around the format string and the arguments, not just the format
+string. That’s because the __ notation is just syntax sugar for a constructor
+call to one of the ``XXXMessage`` classes shown above.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list