Inheritance diagram for IPython.external.Itpl:
String interpolation for Python (by Ka-Ping Yee, 14 Feb 2000).
This module lets you quickly and conveniently interpolate values into strings (in the flavour of Perl or Tcl, but with less extraneous punctuation). You get a bit more power than in the other languages, because this module allows subscripting, slicing, function calls, attribute lookup, or arbitrary expressions. Variables and expressions are evaluated in the namespace of the caller.
The itpl() function returns the result of interpolating a string, and printpl() prints out an interpolated string. Here are some examples:
from Itpl import printpl printpl(“Here is a $string.”) printpl(“Here is a $module.member.”) printpl(“Here is an $object.member.”) printpl(“Here is a $functioncall(with, arguments).”) printpl(“Here is an ${arbitrary + expression}.”) printpl(“Here is an $array[3] member.”) printpl(“Here is a $dictionary[‘member’].”)
The filter() function filters a file object so that output through it is interpolated. This lets you produce the illusion that Python knows how to do interpolation:
import Itpl sys.stdout = Itpl.filter() f = “fancy” print “Is this not $f?” print “Standard output has been replaced with a $sys.stdout object.” sys.stdout = Itpl.unfilter() print “Okay, back $to $normal.”
Under the hood, the Itpl class represents a string that knows how to interpolate values. An instance of the class parses the string once upon initialization; the evaluation and substitution can then be done each time the instance is evaluated with str(instance). For example:
from Itpl import Itpl s = Itpl(“Here is $foo.”) foo = 5 print str(s) foo = “bar” print str(s)
Class representing a string with interpolation abilities.
Upon creation, an instance works out what parts of the format string are literal and what parts need to be evaluated. The evaluation and substitution happens in the namespace of the caller when str(instance) is called.
The single mandatory argument to this constructor is a format string.
The format string is parsed according to the following rules:
any number of times, is evaluated as a Python expression.
A dollar sign immediately followed by an open-brace, and anything up to the matching close-brace, is evaluated as a Python expression.
Outside of the expressions described in the above two rules, two dollar signs in a row give you one literal dollar sign.
Optional arguments:
codec.
policy. See the codecs module documentation for details.
These are used to encode the format string if a call to str() fails on the expanded result.
Bases: IPython.external.Itpl.Itpl
Class representing a string with interpolation abilities.
This inherits from Itpl, but at creation time a namespace is provided where the evaluation will occur. The interpolation becomes a bit more efficient, as no traceback needs to be extracte. It also allows the caller to supply a different namespace for the interpolation to occur than its own.
ItplNS(format,globals[,locals]) -> interpolating string instance.
This constructor, besides a format string, takes a globals dictionary and optionally a locals (which defaults to globals if not provided).
For further details, see the Itpl constructor.
Return an ItplFile that filters writes to the given file object.
‘file = filter(file)’ replaces ‘file’ with a filtered object that has a write() method. When called with no argument, this creates a filter to sys.stdout.
Return the original file that corresponds to the given ItplFile.
‘file = unfilter(file)’ undoes the effect of ‘file = filter(file)’. ‘sys.stdout = unfilter()’ undoes the effect of ‘sys.stdout = filter()’.