Parsers#
Concrete class to handler parser models.
This module provides the abstract class to create parser objects.
- class r2x.parser.handler.BaseParser(config, data=<factory>)
Class that defines the shared methods of parsers.
Note
This class is meant to be use for developing new parsers. Do not use it directly.
- config
Scenario configuration
- Type:
- data
We save each file read in a data dictionary
- Type:
- read_file(fpath='load.csv')
Read load data.
- parse_data()
Read all files from a configuration file map.
- abstract build_system()
Create the infra_sys model.
- Return type:
- parse_data(*, base_folder, fmap, filter_func=None, **kwargs)
Parse all the data for the given translation.
- read_file(fpath, filter_funcs=None, **kwargs)
Read input model data from the file system.
- Currently supported formats:
.csv
.h5
.xml
More to come!
- class r2x.parser.handler.PCMParser(config, data=<factory>)
Class defining shared methods for PCM (currently plexos and sienna) parsers.
- r2x.parser.handler.csv_handler(fpath, csv_file_encoding='utf8', **kwargs)
Parse CSV files and return a Polars DataFrame with all column names in lowercase.
- Parameters:
- Returns:
The parsed CSV file as a Polars DataFrame with lowercase column names if successful, or None if the file was not found.
- Return type:
pl.DataFrame or None
- Raises:
pl.exceptions.ComputeError – Raised if there are issues with the data types in the CSV file.
FileNotFoundError – Raised if the file is not found.
See also
pl_lowercase
Function to convert all column names of a Polars DataFrame to lowercase.
Example
>>> df = csv_handler("data/example.csv") >>> print(df) shape: (2, 3) ┌─────┬────────┬──────┐ │ id │ name │ age │ │ --- │ --- │ --- │ │ i64 │ str │ i64 │ ╞═════╪════════╪══════╡ │ 1 │ Alice │ 30 │ │ 2 │ Bob │ 24 │ └─────┴────────┴──────┘
- r2x.parser.handler.file_handler(fpath, optional=False, **kwargs)
Return FileHandler based on file extension.
- Raises:
FileNotFoundError – If the file is not found.
NotImplementedError – If the file format is not yet supported.
- Parameters:
- Return type:
LazyFrame | DataFrame | Sequence | XMLHandler | None
- r2x.parser.handler.get_parser_data(config, parser_class, filter_funcs=None, **kwargs)
Return parsed system.
This function will create the ReEDS DataPortal and populate with the most common set of data needed
Paremters#
config Scenario configuration class parser
Parser to process
- filter_func
Functions that will applied to read_data process
- param kwargs:
- year
For filtering by solve year
- year_column
To change the column to apply the filter
- column_mapping
For renaming columns
See also
BaseParser
,pl_filter_year
,pl_lower_case
,pl_rename
ReEDS#
Functions related to parsers.
- class r2x.parser.reeds.ReEDSParser(*args, **kwargs)#
ReEDS parser class.
- parse_data(*, base_folder, fmap, filter_func=None, **kwargs)#
Parse all the data for the given translation.
- read_file(fpath, filter_funcs=None, **kwargs)#
Read input model data from the file system.
- Currently supported formats:
.csv
.h5
.xml
More to come!
- r2x.parser.reeds.cli_arguments(parser)#
CLI arguments for the plugin.
- Parameters:
parser (ArgumentParser)
Sienna#
Plexos#
Plexos parser class.
- class r2x.parser.plexos.PlexosParser(*args, xml_file=None, **kwargs)#
Plexos parser class.
- Parameters:
xml_file (str | None)
- parse_data(*, base_folder, fmap, filter_func=None, **kwargs)#
Parse all the data for the given translation.
- read_file(fpath, filter_funcs=None, **kwargs)#
Read input model data from the file system.
- Currently supported formats:
.csv
.h5
.xml
More to come!
- r2x.parser.plexos.cli_arguments(parser)#
CLI arguments for the plugin.
- Parameters:
parser (ArgumentParser)