Skip to main content

Text manipulation library for Python

Project description

textdancer

textdancerはテキスト操作を扱いやすくするためのモジュールです。

インストール方法

pipモジュールを利用してインストールします。

pip install textdancer

クラス

2種類のカテゴリに属するクラスを用意しています。

Cursor

カーソル機能を提供します。

元々はテキスト行を扱いしやすくするために開発されましたが、 抽象化してあるので、一般的なデータ構造として扱えます。

RangeCursor

min~maxの値を指すカーソルを定義します。

SubscriptCursor

配列の添字を扱うためのカーソルです。RangeCursorをラップしたものです。

TextChunk

テキストファイルをチャンクとして扱うtextdancerのメインクラスです。

カーソルを保持しているため、fetchしながら操作を行なうことができます。 また、リストの派生クラスであるため、イテレーターを利用する書き方もできます。 イテレーターを利用した場合はカーソルポジションは影響を受けません。

次のコードはfetchとイテレーターをそれぞれ利用した記述方法です。 いずれも同じ結果を返します。

chunk = TextChunk(["10", "20", "30", "40", "50", "60"])

while chunk.cursor.hasNext():
    print(chunk.fetchNext())

for line in chunk:
    print(line)

また、簡易的なパーサーを書きやすくなるような機能を実装しています。

正規表現リストを用いた行検索機能、行検索機能を用いたpick機能です。 行検索機能はカーソルの影響を受けずに正規表現にマッチする行のポジションを取得できます。 pick機能は内部的に行検索を実行し、見つかった行から最終行、 あるいはカーソルのカレント行から見つかった行までをサブセットとし、 新しいTextChunkとして切り出します。

行検索機能はカーソルの影響を受けませんでしたが、pick機能はカーソル位置が変更されます。 これはテキストのサブセットを取得しながら全行を走査できるようにするためです。

たとえば、次のように書くとテキストを走査し、 ヘッダに紐づくディテールをサブセットとして得られます。

chunk = TextChunk([
    "brief 1",
    "  detail 1-1",
    "  detail 1-2",
    "brief 2",
    "  detail 2-1",
    "  detail 2-2",
])

while chunk.cursor.hasNext():
    picked = chunk.pickTo(["^brief.*$"], skipHeaderSearch=True)
    if picked:
        print(f"picked: {picked}")

実行結果

picked: ['brief 1', '  detail 1-1', '  detail 1-2']
picked: ['brief 2', '  detail 2-1', '  detail 2-2']

テキストの構成として、いくつかの明細の後に合計を示すものがあります。 そのような場合は次のように書くと、全行を走査しながら必要なサブセットを得られます。 skipHeaderSearchパラメーターの代わりにpickToSearchLineパラメーターが利用されている点に注意してください。

chunk = TextChunk([
    "detail 1-1",
    "detail 1-2",
    "total 1",
    "detail 2-1",
    "detail 2-2",
    "total 2",
])

while chunk.cursor.hasNext():
    picked = chunk.pickTo(["^total.*$"], pickToSearchLine=True)
    if picked:
        print(f"picked: {picked}")

実行結果

picked: ['detail 1-1', 'detail 1-2', 'total 1']
picked: ['detail 2-1', 'detail 2-2', 'total 2']

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

textdancer-0.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file textdancer-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: textdancer-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.7

File hashes

Hashes for textdancer-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e717dbb2aa5b12fb80a65a385912d4dc2e7987239daac8abe43bed95300b0b24
MD5 359d6b55fcdec366c9b64794a18345aa
BLAKE2b-256 041720d4ae74bd62ab4dd50e588f76c2fa75c142d37e6fe5bc50bfe5075a99f8

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