Skip to main content

A text shaping package.

Project description

textform

A data transformation pipeline module based on the seminal Potter's Wheel data wrangling formalism. The name is a portmanteau of "text" and "transform".

Overview

textform (abbreviated txf) is a text-oriented data transformation module. With it, you can create sequential record processing pipelines that convert data from (say) lines of text into records and then route the final record stream for another use (e.g, write the records to a csv file.)

Pipelines are cosntructed from a sequence of transforms that take in a record and modify it in some way. For example, the Split transform will replace an input field with several new fields that are derived from the input by splitting on a pattern.

While inspired by the Potter's Wheel transform list, textform is designed for practical everyday use. This means it includes transforms for limiting the number of rows, writing intermediate results to files and capturing via regular expressions.

Audience

How do I know if textform is right for me? The simplest use case is where you want to use Python's DictReader but the file isn't a csv. With textform you can write a pipeline that will end up producing the records you would get from DictReader.

More complex use cases can be built on top of this kind of record stream. Reshaping, computing values, splitting, dividing, merging, filling in blanks and other kinds of data cleaning and preparation tasks can all be implemented in a reusable fashion with textform. A pipeline effectively describes the format of a text file in an executable fashion that can be reused.

Example

I created textform because I had worked on a similar research system in the past and had two text files produced by the DuckDB performance test suite that I needed to convert into csvs:

------------------
|| Q01_PARALLEL ||
------------------
Cold Run...Done!
Run 1/5...0.12345
Run 1/5...0.12345
Run 1/5...0.12345
Run 1/5...0.12345
Run 1/5...0.12345
------------------
|| Q02_PARALLEL ||
------------------
...

This file is esssentially a sequence of records grouped by higher attributes. Instead of writing a one-off Python script, I decided to write some simple transforms and build a pipeline, which looked like this:

p = Text(sys.stdin, 'Line')                         # Read a line
p = Add(p, 'Branch', sys.argv[1])                   # Tag the file with the branch name
p = Match(p, 'Line', r'------', invert=True).       # Remove horizontal lines
p = Divide(p, 'Line', 'Query', 'Run', r'Q')         # Separate the query names from the run data
p = Fill(p, 'Query', '00')                          # Fill down the blank query names
p = Capture(p, 'Query', ('Query',), r'\|\|\s+Q(\w+)\s+\|\|')  # Capture the query number
# Split the execution mode from the query name
p = Split(p, 'Query', ('Query', 'Mode',), r'_', ('00', 'SERIAL',))
p = Cast(p, 'Query', int)                           # Cast the query number to an integer
p = Match(p, 'Run', r'\d')                          # Filter to the runs with data
# Capture the run components
p = Capture(p, 'Run', ('Run #', 'Run Count', 'Time',), r'(\d+)/(\d+)...(\d+\.\d+)')
p = Cast(p, 'Run #', int)                           # Cast the run components
p = Cast(p, 'Run Count', int)
p = Cast(p, 'Time', float)
p = Write(p, sys.stdout)                            # Write the records to stdout as a csv
p.pump()

We can now invoke the pipeline script as:

$ python3 pipeline.py master < performance.txt > performance.csv

Contributing

You know the drill: Fork, branch, test submit a PR. This is a completely open source, free as in beer project.

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

textform-0.10.8.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

textform-0.10.8-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file textform-0.10.8.tar.gz.

File metadata

  • Download URL: textform-0.10.8.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for textform-0.10.8.tar.gz
Algorithm Hash digest
SHA256 04a78ee116a101112e9e97378f413401da39bba9578ef60b4b2b075eb5c206b4
MD5 cb9c6bb082b948d921a2aee9e2001cdb
BLAKE2b-256 7bb5c4975b61ed3243485efeeba85ce90623c0a8a0199e1aef5c267d36205e9e

See more details on using hashes here.

File details

Details for the file textform-0.10.8-py3-none-any.whl.

File metadata

  • Download URL: textform-0.10.8-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for textform-0.10.8-py3-none-any.whl
Algorithm Hash digest
SHA256 4e8a98c1c085923c3cf615cb09f4f1b81ca1376bd95c9d01287019ad0b6c0c3f
MD5 34bf7c10ef847f151df8fdf32041d0c6
BLAKE2b-256 89770f171848167a3adeb7ef50707b6e9c982b1ccdf9f5b9f049b66a38fce202

See more details on using hashes here.

Supported by

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