iode.execute_report

iode.execute_report(filepath: str | Path, parameters: str | List[str] = None, encoding: str = 'cp850')[source]

Execute an IODE report.

Parameters:
filepath: str, Path

Filepath of the IODE report to execute. The extension of the file must be .rep.

parameters: str or list(str), optional

Parameter(s) passed to the report. If multiple parameters are passed in one string, they must be separated by a whitespace. Default to None.

encoding: str, optional

Encoding of the report file. Default to ‘cp850’. The (old) IODE report .rep are written using encoding ‘oem-850’ (cp850).

Warning

The encoding of the report file must be ‘oem-850’ (cp850) for the report to be executed correctly. If the encoding is different, it must be specified through the encoding parameter. In that case, a temporary copy of the report file will be created in ‘oem-850’ encoding and used for execution. The temporary file will be deleted after execution.

By default, Python uses ‘utf-8’ encoding to read text files.

Notes

Equivalent to the IODE command $ReportExec

Examples

>>> from iode import execute_report, variables
>>> from iode import SAMPLE_DATA_DIR
>>> from pathlib import Path
>>> output_dir = getfixture('tmp_path')

Existing (old) IODE report file (oem-850 encoding)

>>> parameters = [SAMPLE_DATA_DIR, str(output_dir)]
>>> report_file = str(f"{SAMPLE_DATA_DIR}/reports/oem850.rep")
>>> execute_report(report_file, parameters)
iode> $ report containing special characters
iode> $indent
iode> $noparsing
iode> $goto continue 1
iode> $define input_dir ...
iode> $define output_dir ...
iode> $ print output of the present report to oem850.a2m file
iode> $ A2M files are encoded in OEM-850
iode> $PrintDest ...oem850.a2m A2M
iode> $define string àâäéèêëîïöôùç
iode> $ evaluate macro
iode> àâäéèêëîïöôùç
iode> $ Load comments and tables
iode> $WsLoadCmt ...fun
Loading ...fun
317 objects loaded
iode> $WsLoadTbl ...fun
Loading ...fun
46 objects loaded
iode> $ get a comment
iode> Deflator privaat verbruik (vóór statistische aanpassing).
iode> $ get title of an IODE table
iode> Déterminants de l'output potentiel
iode> $return
>>> # check content of output file created by the report
>>> # Warning: A2M are written using 'oem-850' encoding (cp850) 
>>> #          -> need to specify the encoding when reading the file
>>> result_file = output_dir / "oem850.a2m"
>>> # without specifying the encoding, the special characters 
>>> # are not read correctly
>>> with open(result_file, "r") as f:
...     print(f.read())
…ƒ„‚Šˆ‰Œ‹”“—‡


Deflator privaat verbruik (v¢¢r statistische aanpassing).

D‚terminants de l'output potentiel


>>> # with the correct encoding, the special characters 
>>> # are read correctly
>>> with open(result_file, "r", encoding="cp850") as f:
...     print(f.read())
àâäéèêëîïöôùç


Deflator privaat verbruik (vóór statistische aanpassing).

Déterminants de l'output potentiel


New IODE report file (utf-8 encoding)

>>> # create a dump report.
>>> # Note: By default, Python uses 'utf-8' encoding 
>>> #       to write text files.
>>> create_var_rep = str(output_dir / "create_var.rep")
>>> result_var_file = str(output_dir / "test_var.av")
>>> with open(create_var_rep, "w") as f:
...     f.write("$WsClearVar\n")
...     f.write("$WsSample 2000Y1 2005Y1\n")
...     f.write("$DataCalcVar %1% t+1 \n")
...     f.write("$DataCalcVar %2% t-1 \n")
...     f.write("$DataCalcVar %3% %1%/%2%\n")
...     f.write("$DataCalcVar %4% grt %1% \n")
...     f.write(f"$WsSaveVar {result_var_file}\n")
12
24
22
22
25
26
...
>>> # execute the report.
>>> # Warning: IODE report files are interpreted as files written 
>>> #          using the old 'oem-850' encoding (cp850).
>>> execute_report(create_var_rep, ["A", "B", "C", "D"], encoding='utf-8')
iode> $WsClearVar
iode> $WsSample 2000Y1 2005Y1
iode> $DataCalcVar A t+1
iode> $DataCalcVar B t-1
iode> $DataCalcVar C A/B
iode> $DataCalcVar D grt A
iode> $WsSaveVar ...test_var.av
Saving ...test_var.av
>>> # check content of file test_var.av
>>> with open(result_var_file, "r") as f:
...     print(f.read())
sample 2000Y1 2005Y1
A 1 2 3 4 5 6
B -1 0 1 2 3 4
C -1 na 3 2 1.66666666666667 1.5
D na 100 50 33.3333333333333 25 20