bw2parameters.mangling#

Attributes#

Classes#

FormulaSubstitutor

Callable class that will substitute symbol names using substitutions substitution dictionary.

OnlySelected

Change name of all symbols already redefined in substitutes.

PrefixNameAdder

Change name of all symbols by adding a prefix, unless name already in context.

Functions#

mangle_formula(string, prefix[, context])

Add prefix to all variable names in formula string, except those in context or builtin to Python, math, or numpy.

prefix_parameter_dict(dct, prefix)

Add prefix to each key in dct. Also updates the formulas, if present.

substitute_in_formulas(dct, substitutions)

Substitute symbol names in dct formulas following substitutions.

Module Contents#

class bw2parameters.mangling.FormulaSubstitutor(substitutions)[source]#

Bases: object

Callable class that will substitute symbol names using substitutions substitution dictionary.

visitor[source]#
class bw2parameters.mangling.OnlySelected(substitutes=None)[source]#

Bases: asteval.astutils.NameFinder

Change name of all symbols already redefined in substitutes.

generic_visit(node)[source]#
substitutes = None[source]#
class bw2parameters.mangling.PrefixNameAdder(prefix, context=None)[source]#

Bases: asteval.astutils.NameFinder

Change name of all symbols by adding a prefix, unless name already in context.

generic_visit(node)[source]#
builtins[source]#
prefix[source]#
bw2parameters.mangling.mangle_formula(string, prefix, context=None)[source]#

Add prefix to all variable names in formula string, except those in context or builtin to Python, math, or numpy.

Uses asteval and astunparse.

Returns the formula as a string.

Example usage:

… code-block:: python

>>> mangle_formula("log(foo * bar) + 7 / baz", "pre", ['bar'])
'(log((pre__foo * bar)) + (7 / pre__baz))'
bw2parameters.mangling.prefix_parameter_dict(dct, prefix)[source]#

Add prefix to each key in dct. Also updates the formulas, if present.

Adds original to each value in dct with the original key name.

Returns the new dictionary, and a dictionary of name substitutions like {old: new}

bw2parameters.mangling.substitute_in_formulas(dct, substitutions)[source]#

Substitute symbol names in dct formulas following substitutions.

Modifies in place. Returns the modified dct.

bw2parameters.mangling.BUILTINS[source]#