Skip to main content

A reader for text file output from the program.

Project description

outputdatareader

プログラムの実行結果として出力されたテキストファイルから、必要なデータを読み取るリーダです。

実行結果をテキストファイルに出力するプログラムは多くありますが、その出力は一貫したフォーマットになっているとは限りません。 複数のセクションからなり、セクションごとにフォーマットが異なっている場合、全体を1つの解析器で賄うことは難しくなります。 また、欲しいのは一部のセクションだけ、というケースもあります。

outputodatareader では、必ずしも出力全てを読み取る必要はなく、欲しい部分だけを読み取ることができます。

Install

pip でインストールできます:

$ pip install outputdatareader

または、パッケージマネージャーを使ってプロジェクトに追加します:

$ poetry add outputdatareader

Usage

How it works

outputdatareader は2つのステージから成ります。

  1. スキャナはテキストファイルを適切なセグメント(例えば1つの行)に分解します。
  2. リーダはスキャナからセグメントを1つずつ取り出し、読み取ることを繰り返します。

リーダはスキャナから取り出したセグメントを1つずつ処理しますが、本当に必要なものだけを選んで読み取ることができます。不要なセグメントは捨ててしまえばいいのです。

リーダはまた、処理の一部を別のリーダに任せることができます。処理が戻ってきたら、そこから処理を続けます。

Reader

outputdatareader.readers.Base クラスを継承したサブクラスを定義し、インスタンスを生成して利用します。 このクラスは読み取りループの枠組みを提供するので、サブクラスではスキャナから取り出したセグメントを解析して読み取ることに集中できます。 サブクラスには、次のメソッドを実装します。

  • setup
  • loop
  • teardown

loop だけが必須です。このメソッドが読み取りの本質部分です。 setupteardown はそれぞれ読み取りの前処理、後処理をします。

読み取りループ中にはいくつかのプライベートメソッドが利用できます。

  • _next_loop は「つぎの回」へジャンプします。
  • _exit_loop は読み取りループを終了します。処理が親クラスへ戻る前に teardown が実行されます。
  • _exit_readteardown もとばして読み取りを終了します。
  • _unshift はセグメントを1つ、スキャナに戻します。

_unshift が必要になるのは、スキャナから受け取ったセグメントが、別のリーダが読み取るべき「次のセクション」の始まりであるような場合です。 セグメントを1つスキャナに戻してから読み取りを終了すれば、「次のセクション」を読み取る別のリーダがそこから読み取りを開始できます。

Scanner

スキャナは、テキストファイルを適切なセグメントに分解し、リーダに提供します。 スキャナは iterable なオブジェクトです。

独自のスキャナを作るには outputdatareader.scanners.Base クラスを継承し、 _scan メソッドを実装します。この _scan が適切なセグメントに分解する役割を担っています。

outputodatareader.scanners モジュールには、1行ずつに分解する LineScanner と、CSVファイルを1レコードずつに分解する CsvScanner が用意されています。

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

outputdatareader-0.9.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

outputdatareader-0.9.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file outputdatareader-0.9.0.tar.gz.

File metadata

  • Download URL: outputdatareader-0.9.0.tar.gz
  • Upload date:
  • Size: 4.2 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

Hashes for outputdatareader-0.9.0.tar.gz
Algorithm Hash digest
SHA256 8d98e77c16136009b8446b91bb7ec69955e26bfa67dac74564d7698d9337089c
MD5 b350adde665fa022bfb237cb46c4bc37
BLAKE2b-256 01c79dc1f6b9ecd9bc2986299bfe73ea63cbc19778515bb15f1d39bb831c969d

See more details on using hashes here.

File details

Details for the file outputdatareader-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: outputdatareader-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 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

Hashes for outputdatareader-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e7ee7f839bab2c9bb83a349d5a72663d31e4b412f980a03db3f5779c34caece5
MD5 c708ef477af8166c3517ee4583181b80
BLAKE2b-256 42fe59c75e4478a9cc3573af9cad6113e46b12d6c0f8f8e67d4981f2c0959971

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page