A reader for text file output from the program.
Project description
outputdatareader
This outputdatareader reads the necessary data from a text file output as a result of program execution.
There are many programs that output the results of an execution to a text file, but the output is not always in a consistent format. If the output consists of multiple sections, each of which is formatted differently, it can be difficult for a single analyzer to provide the entire output. Also, there are cases where only some sections are wanted.
With outputodatareader, you do not necessarily need to read the entire output, only the parts you want.
Install
outputdatareader can be installed with pip:
$ pip install outputdatareader
Or add it to your project using the package manager:
$ poetry add outputdatareader
Usage
How it works
outputdatareader consists of two stages
- the scanner breaks the text file into appropriate segments (e.g., one line)
- the reader repeatedly retrieves segments from the scanner one by one and reads them.
The reader processes the segments retrieved from the scanner one at a time, but can select and read only the ones it really needs. Unnecessary segments can be discarded.
The reader can also delegate part of the processing to another reader. When the processing comes back, it continues from there.
Reader
Define a subclass that extends the outputdatareader.readers.Base class and create an instance for use.
This class provides the framework for the read loop, so the subclass can concentrate on parsing and reading segments retrieved from the scanner.
The subclass implements the following methods:
setuploopteardown
Only loop is required. This method is the essential part of reading.
setup and teardown pre-process and post-process the read, respectively.
Several private methods are available during the read loop.
_next_loopjumps to “next loop”._exit_loopexits the reading loop.teardownis executed before returning to the caller._exit_readskipsteardownand finishes reading._unshiftreturns one segment to the scanner.
_unshift is needed when the segment received from the scanner is the beginning of the “next section” to be read by another reader.
If you return one segment to the scanner and then stop reading, another reader that reads the “next section” can start reading from there.
Scanner
The scanner breaks the text file into appropriate segments and provides them to the reader. The scanner is an iterable object.
To create your own scanner, extend outputdatareader.scanners.Base class and implement the method _scan. _scan is responsible for breaking it down into appropriate segments.
outputdatareader.scanners module provides LineScanner to scan one line at a time, and CsvScanner to scan a CSV file one record at a time.
Example
from outputdatareader.scanners import LineScanner
from outputdatareader.readers import Base
class MainReader(Base):
def loop(self, line):
if line.statswith('Section 2'):
self._unshift()
reader = Section2Reader(self.scanner, {})
self.holder['section_2'] = reader.read()
elif line.statswith('Section 5'):
self._unshift()
reader = Section5Reader(self.scanner, {})
self.holder['section_5'] = reader.read()
else:
# ignore other sections
pass
class Section2Reader(Base):
def loop(self, line):
if line.startswith('Section 3'):
self._exit_read()
else:
# implement here
class Section5Reader(Base):
def loop(self, line):
# implement here
def main():
scanner = LineScanner('some_output.txt', encoding='utf-8')
result_holder = {}
reader = MainReader(scanner, result_holder)
result = reader.read()
print(result)
main()
License
MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file outputdatareader-0.9.1.tar.gz.
File metadata
- Download URL: outputdatareader-0.9.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/6.8.0-51-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6709d86dfaf0d7823c4b6e90d6d8d330f1feee5e204c8846c243fa15b08b75c5
|
|
| MD5 |
c579cfec1859927fafa967b3bc8a1296
|
|
| BLAKE2b-256 |
cce825cfff1f90a5a2dac9359011d3ef0a94ea7cbe9199b6ce55c15354d2891f
|
File details
Details for the file outputdatareader-0.9.1-py3-none-any.whl.
File metadata
- Download URL: outputdatareader-0.9.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/6.8.0-51-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75ba9c7de4c7f3d4a1add85790edef332a3422292c39a6902bb5042747b1e3d7
|
|
| MD5 |
430249066f47b36b257bde1304b99bd8
|
|
| BLAKE2b-256 |
e0a55e13acf8cf83e9901da1cc411ec49a59e54d8981143c4b7fe78b9af8d727
|