midgard.parsers
Framework for parsers
Description:
To add a new parser, simply create a new .py-file which defines a class
inheriting from parsers.Parser. The class needs to be decorated with the
midgard.dev.plugins.register
decorator as follows:
from midgard.parsers import parser
from midgard.lib import plugins
@plugins.register
class MyNewParser(parser.Parser):
...
To use a parser, you will typically use the parse_file
-function defined below
from midgard import parsers
my_new_parser = parsers.parse_file('my_new_parser', 'file_name.txt', ...)
my_data = my_new_parser.as_dict()
The name used in parse_file
to call the parser is the name of the module
(file) containing the parser.
names()
names() -> List[str]
List the names of the available parsers
Returns:
Names of the available parsers
parse_file()
parse_file(parser_name:str, file_path:Union[str, pathlib.Path], encoding:Union[str, NoneType]=None, parser_logger:Union[Callable[[str], NoneType], NoneType]=<built-in function print>, timer_logger:Union[Callable[[str], NoneType], NoneType]=None, use_cache:bool=False, **parser_args:Any) -> midgard.parsers._parser.Parser
Use the given parser on a file and return parsed data
Specify parser_name
and file_path
to the file that should be parsed. The following parsers are available:
{doc_parser_names}
Data can be retrieved either as Dictionaries, Pandas DataFrames or Midgard Datasets by using one of the methods
as_dict
, as_dataframe
or as_dataset
.
Example:
>>> df = parse_file('rinex2_obs', 'ande3160.16o').as_dataframe() # doctest: +SKIP
Args:
parser_name
: Name of parserfile_path
: Path to file that should be parsed.encoding
: Encoding in file that is parsed.parser_logger
: Logging function that will be used by parser.timer_logger
: Logging function that will be used to log timing information.use_cache
: Whether to use a cache to avoid parsing the same file several times.parser_args
: Input arguments to the parser
Returns:
Parser
: Parser with the parsed data