midgard.parsers._parser_line
Basic functionality for parsing datafiles line by line using Numpy
Description:
This module contains functions and classes for parsing datafiles.
Example:
from midgard import parsers
my_new_parser = parsers.parse_file('my_new_parser', 'file_name.txt', ...)
my_data = my_new_parser.as_dict()
LineParser
LineParser(file_path:Union[str, pathlib.Path], encoding:Union[str, NoneType]=None, logger:Union[Callable[[str], NoneType], NoneType]=<built-in function print>) -> None
An abstract base class that has basic methods for parsing a datafile
This class provides functionality for using numpy to parse a file line by line. You should inherit from this one,
and at least specify the necessary parameters in setup_parser
.
LineParser.read_data()
read_data(self) -> None
Read data from the data file
Uses the np.genfromtxt-function to parse the file. Any necessary
parameters should be set by setup_parser
. Override
self.structure_data
if the self.data-dictionary needs to be
structured in a particular way.
LineParser.setup_parser()
setup_parser(self) -> Any
Set up information needed for the parser
This method should return a dictionary which contains all parameters needed by np.genfromtxt to do the actual parsing.
LineParser.structure_data()
structure_data(self) -> None
Structure raw array data into the self.data dictionary
This simple implementation creates a dictionary with one item per column in the array. Override this method for more complex use cases.