bw_simapro_csv.parameters ========================= .. py:module:: bw_simapro_csv.parameters Attributes ---------- .. autoapisummary:: bw_simapro_csv.parameters.LEADING_ZERO_RE Classes ------- .. autoapisummary:: bw_simapro_csv.parameters.FormulaSubstitutor bw_simapro_csv.parameters.OnlySelectedUppercase Functions --------- .. autoapisummary:: bw_simapro_csv.parameters.add_prefix_to_uppercase_input_parameters bw_simapro_csv.parameters.build_substitutes bw_simapro_csv.parameters.compile_iff_re bw_simapro_csv.parameters.fix_iff_formula bw_simapro_csv.parameters.fix_leading_zero_formula bw_simapro_csv.parameters.prepare_formulas bw_simapro_csv.parameters.substitute_in_formulas Module Contents --------------- .. py:class:: FormulaSubstitutor(substitutions) Callable class that will substitute symbol names using ``substitutions`` substitution dictionary. .. py:attribute:: visitor .. py:class:: OnlySelectedUppercase(substitutes=None) Bases: :py:obj:`asteval.astutils.NameFinder` Change name of all symbols already redefined in ``substitutes``. .. py:method:: generic_visit(node) .. py:attribute:: substitutes :value: None .. py:function:: add_prefix_to_uppercase_input_parameters(block: list, prefix: str = 'SP_') -> list 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'}] .. py:function:: build_substitutes(project_parameters: Iterable, database_parameters: Iterable) -> dict[str, str] 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'} .. py:function:: compile_iff_re(header: dict) -> Pattern 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)` .. py:function:: fix_iff_formula(formula: str, pattern: Pattern) -> str 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. :param formula: A string containing SimaPro 'iff' formulae. :type formula: str :param pattern: :type pattern: compiled regular expression from `compile_iff_re` :returns: **string** -- A string with SimaPro 'iff' formulae replaced by Python 'if-else' expressions. :rtype: str .. rubric:: Examples >>> string = "iff(A > 0, A, 0)" >>> fix_iff_formula(string, compile_iff_re({})) "((A) if (A > 0) else (0))" .. py:function:: fix_leading_zero_formula(formula: str, pattern: Pattern = LEADING_ZERO_RE) -> str Replace leading zeros in numbers, as these cause Python syntax errors. https://github.com/brightway-lca/bw_simapro_csv/issues/19 :param formula: A string, possibly containing leading zeros. :type formula: str :param pattern: :type pattern: compiled regular expression :returns: **string** -- A string with leading zeros removed. :rtype: str .. rubric:: 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" .. py:function:: prepare_formulas(block: list[dict], header: dict, formula_field: str = 'formula') -> list[dict] Make necessary conversions so formulas can be parsed by Python. Does the following: * Substitute `^` with `**`. * Replace `Iff()` clauses using `fix_iff_formula()` .. py:function:: substitute_in_formulas(obj: dict, visitor: Type, formula_field: str = 'formula') -> dict 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'} .. py:data:: LEADING_ZERO_RE