Skip to content

A command line utility for transforming text using Python expressions. Or let Claude write the code for you.

License

Notifications You must be signed in to change notification settings

armandmcqueen/pystr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pystr

Test

A command line utility for transforming text using Python expressions. If you know Python, you already know pystr.

echo "Hello World" | pystr 's.lower().replace(" ", "-")'
hello-world

Or let Claude write the Python for you.

echo "5550123456" | pystr --prompt "format as US phone number: 000-000-0000"
555-012-3456

Installation

pystr is a single ~250 line Python script. Copy it or download it from github:

curl -fsSL https://raw.githubusercontent.com/armandmcqueen/pystr/main/install.sh | bash

Requires uv.

LLM

For prompt mode (-p), set your ANTHROPIC_API_KEY environment variable.

Usage

pystr reads from stdin and runs your Python expression on line, printing the expression result.

Variable Description
s The current line (string)
i Line number (0-indexed)
math The math module
re The re module for regex

All Python builtins (len, int, str, sum, sorted, etc.) are available.

It's trivial to add dependencies to the script if you need - add it manually or open an issue.

Basic: transform each line

echo "hello world" | pystr 's.upper()'
HELLO WORLD

seq 5 | pystr 'int(s) ** 2'
1
4
9
16
25

cat file.txt | pystr 'f"{i}: {s}"'  # number lines

Grep mode (-g): filter lines

Print lines where the expression is truthy.

seq 10 | pystr -g 'int(s) % 2 == 0'
2
4
6
8
10

cat log.txt | pystr -g '"ERROR" in s'  # find error lines

All mode (-a): process entire input at once

Instead of line-by-line, s becomes the entire input.

cat file.txt | pystr -a 'len(s.splitlines())'  # count lines
cat file.txt | pystr -a '",".join(s.splitlines())'  # join lines

Prompt mode (-p): let Claude write the Python

Don't want to write the expression? Describe what you want in plain English.

echo "5550123456" | pystr -p "format as US phone number"
555-012-3456

echo "john,doe,30" | pystr -p "get the second comma-separated field"
doe

Use --show to see the generated code:

echo "hello world" | pystr -p --show "reverse each word"
Generated code: ' '.join(word[::-1] for word in s.split())
olleh dlrow

Uses Claude Haiku by default (--model sonnet or --model opus for smarter models). Haiku is fast, but you may need to be descriptive. Sonnet is good if you don't want to think.

Combining flags

Flags can be combined. For example, grep + prompt:

cat uuids.txt | pystr -g -p "invalid uuids"
not-a-uuid
hello world

All Options

pystr [OPTIONS] EXPRESSION

Options:
  -a, --all       Read all input at once (s = entire input)
  -g, --grep      Filter mode: print lines where expression is truthy
  -n, --no-print  Don't auto-print (for expressions with side effects)
  -p, --prompt    Interpret expression as natural language
  --show          Show generated code (with -p)
  --confirm       Ask before executing generated code (with -p)
  --model MODEL   haiku (default), sonnet, or opus (with -p)
  --dry-run       Show the LLM prompt without executing (with -p)
  -v, --version   Show version

License

Apache 2.0

About

A command line utility for transforming text using Python expressions. Or let Claude write the code for you.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published