bw_simapro_csv.parameters

Attributes

LEADING_ZERO_RE

Classes

FormulaSubstitutor

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

OnlySelectedUppercase

Change name of all symbols already redefined in substitutes.

Functions

add_prefix_to_uppercase_input_parameters(→ list)

For each input parameter, uppercase and add prefix to its name and store the original.

build_substitutes(→ dict[str, str])

Build a dictionary of parameter name substitutions.

compile_iff_re(→ Pattern)

Compile a regular expression for Iff patterns taking the decimal separator into account.

fix_iff_formula(→ str)

Replace SimaPro 'iff' formula with a Python equivalent 'if-else' expression.

fix_leading_zero_formula(→ str)

Replace leading zeros in numbers, as these cause Python syntax errors.

prepare_formulas(→ list[dict])

Make necessary conversions so formulas can be parsed by Python.

substitute_in_formulas(→ dict)

Substitute variable names in obj[formula_field] based on substitutions.

Module Contents

class bw_simapro_csv.parameters.FormulaSubstitutor(substitutions)[source]

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

visitor[source]
class bw_simapro_csv.parameters.OnlySelectedUppercase(substitutes=None)[source]

Bases: asteval.astutils.NameFinder

Change name of all symbols already redefined in substitutes.

generic_visit(node)[source]
substitutes = None[source]
bw_simapro_csv.parameters.add_prefix_to_uppercase_input_parameters(block: list, prefix: str = 'SP_') list[source]

For each input parameter, uppercase and add prefix to its name and store the original.

Example usage:

… code-block:: python

>>> add_prefix_to_input_parameters([{'name': 'foo'}])
[{'name': 'SP_FOO', 'original_name': 'foo'}]
bw_simapro_csv.parameters.build_substitutes(project_parameters: Iterable, database_parameters: Iterable) dict[str, str][source]

Build a dictionary of parameter name substitutions.

Project parameters take precedence over database parameters.

Example usage:

… code-block:: python

>>> p = [
>>>         {'name': 'sp_foo', 'original_name': 'foo'},
>>>         {'name': 'sp_bar', 'original_name': 'bar'}
>>>     ]
>>> d = [{'name': 'hey_foo', 'original_name': 'foo'}]
>>> build_substitutes(p, d)
{'foo': 'hey_foo', 'bar': 'sp_bar'}
bw_simapro_csv.parameters.compile_iff_re(header: dict) Pattern[source]

Compile a regular expression for Iff patterns taking the decimal separator into account.

Normally a SimaPro Iff expression has the form Iff(test, if_true, if_false); however, if the decimal separator is “,”, then it has the form Iff(test; if_true; if_false)

bw_simapro_csv.parameters.fix_iff_formula(formula: str, pattern: Pattern) str[source]

Replace SimaPro ‘iff’ formula with a Python equivalent ‘if-else’ expression.

Processes a given string containing SimaPro ‘iff’ formulae and replaces them with Python equivalent ‘if-else’ expressions. The conversion is done using regular expressions.

Parameters:
  • formula (str) – A string containing SimaPro ‘iff’ formulae.

  • pattern (compiled regular expression from compile_iff_re)

Returns:

string – A string with SimaPro ‘iff’ formulae replaced by Python ‘if-else’ expressions.

Return type:

str

Examples

>>> string = "iff(A > 0, A, 0)"
>>> fix_iff_formula(string, compile_iff_re({}))
"((A) if (A > 0) else (0))"
bw_simapro_csv.parameters.fix_leading_zero_formula(formula: str, pattern: Pattern = LEADING_ZERO_RE) str[source]

Replace leading zeros in numbers, as these cause Python syntax errors.

https://github.com/brightway-lca/bw_simapro_csv/issues/19

Parameters:
  • formula (str) – A string, possibly containing leading zeros.

  • pattern (compiled regular expression)

Returns:

string – A string with leading zeros removed.

Return type:

str

Examples

>>> string = "01.23 foo +0123 1.02 john012 123e023 123**023 123*0123 123/0123 clive012"
>>> fix_leading_zero_formula(string)
"1.23 foo +123 1.02 john012 123e23 123**23 123*123 123/123 clive012"
bw_simapro_csv.parameters.prepare_formulas(block: list[dict], header: dict, formula_field: str = 'formula') list[dict][source]

Make necessary conversions so formulas can be parsed by Python.

Does the following:

  • Substitute ^ with **.

  • Replace Iff() clauses using fix_iff_formula()

bw_simapro_csv.parameters.substitute_in_formulas(obj: dict, visitor: Type, formula_field: str = 'formula') dict[source]

Substitute variable names in obj[formula_field] based on substitutions.

Keeps original_formula.

Example usage:

… code-block:: python

>>> given = {'formula': 'a * 2'}
>>> substitutions = {'a': 'hi_mom'}
>>> substitute_in_formulas(given, substitutions)
{'formula': 'hi_mom * 2', 'original_formula': 'a * 2'}
bw_simapro_csv.parameters.LEADING_ZERO_RE[source]