Python Glossary Русский

Python 3.2+configparserterm

configparser.Interpolation

interpolation

The base interpolation class that substitutes nothing. Subclass it to define your own substitution scheme for config values.

Syntax

class My(configparser.Interpolation):
    def before_get(self, parser, section, option, value, defaults): ...

ConfigParser(interpolation=None) uses it internally, so it is the same 'values as is' mode. A custom scheme overrides before_get (called when a value is read) and optionally before_set. Pass an instance: the bare class without parentheses raises TypeError right in the parser constructor.

Examples

import configparser
class Home(configparser.Interpolation):
    def before_get(self, parser, section, option, value, defaults):
        return value.replace("~", "/home/user")
cfg = configparser.ConfigParser(interpolation=Home())
cfg.read_string("[paths]\ndata = ~/data\n")
print(cfg.get("paths", "data"))  # → /home/user/data
import configparser
try:
    configparser.ConfigParser(interpolation=configparser.Interpolation)
except TypeError:
    print("TypeError: нужен экземпляр")  # → TypeError: нужен экземпляр

See also

Open in the full glossarydocs.python.orgReport an inaccuracy