ZConfig.substitution — String substitution¶
Shell-style string substitution helper.
This module provides a basic substitution facility similar to that
found in the Bourne shell (sh
on most UNIX platforms).
The replacements supported by this module include:
Source | Replacement | Notes |
---|---|---|
$$ |
$ |
|
$name |
The result of looking up name | |
${name} |
The result of looking up name | |
$(name) |
The result of looking up name in the environment |
Notes:
- This is different from the Bourne shell, which uses
\$
to generate a$
in the result text. This difference avoids having as many special characters in the syntax. - Any character which immediately follows name may not be a valid character in a name.
- This is not Bourne shell style.
In each case, name is a non-empty sequence of alphanumeric and
underscore characters not starting with a digit. If there is not a
replacement for name, the exception
SubstitutionReplacementError
is raised.
Note that the lookup is expected to be case-insensitive; this module
will always use a lower-case version of the name to perform the query.
This module provides these functions:
-
ZConfig.substitution.
substitute
(s, mapping)¶ Substitute values from mapping into s.
mapping can be a
dict
or any type that supports theget()
method of the mapping protocol. Replacement values are copied into the result without further interpretation. RaisesSubstitutionSyntaxError
if there are malformed constructs in s.
-
ZConfig.substitution.
isname
(s)¶ Returns
True
if s is a valid name for a substitution text, otherwise returnsFalse
.
Examples¶
>>> from ZConfig.substitution import substitute
>>> d = {'name': 'value',
... 'top': '$middle',
... 'middle' : 'bottom'}
>>>
>>> substitute('$name', d)
'value'
>>> substitute('$top', d)
'$middle'
>>> import os
>>> os.environ['from_environment'] = 'From environment.'
>>> substitute('$(from_einvironment)', d)
'From environment.'