3.12. sciexp2.common.text

Source: sciexp2/common/text.py

Text translation and extraction functions.

The translation and extraction functions are based on text templates that follow a subset of the mustache syntax: https://mustache.github.io.

The accepted subset of mustache tags is:

  • Variables: {{name}}

  • Sections: {{#name}}...{{/name}}

  • Inverted sections: {{^name}}...{{/name}}

Functions

extract

Shorthand for Extractor(template).extract(text).

get_variables

Get the variables referenced in the given text.

translate

Shorthand for Translator(template).translate(env, **kwargs).

translate_many

Shorthand for Translator(template).translate_many(envs, **kwargs).

Classes

Extractor

Extract a dict with the variable values that match a given template.

Translator

Translate a template text with given variables.

Exceptions

ExtractError

ParseError

VariableError

3.12.3. extract

extract(template, text)

Shorthand for Extractor(template).extract(text).

3.12.4. get_variables

get_variables(text, nested=False)

Get the variables referenced in the given text.

Parameters:
textstr

Text to get variables from.

nestedoptional

Whether to allow nested variables. Can have values “all” for all the variables, or “inner” for just the inner variables.

Examples

>>> get_variables("{{a}}")
['a']
>>> get_variables("{{#a}} {{b}} {{/a}}", nested="inner")
['b']
>>> get_variables("{{#a}} {{b}} {{/a}}", nested="all")
['a', 'b']

3.12.5. translate

translate(template, env, **kwargs)

Shorthand for Translator(template).translate(env, **kwargs).

3.12.6. translate_many

translate_many(template, envs, **kwargs)

Shorthand for Translator(template).translate_many(envs, **kwargs).