Skip to main content

Surgical, occurrence-based word manipulation for strings and files

Project description

wordscalpel 🔪

Surgical, occurrence-based word manipulation for strings and files.

Log sanitization, document redaction, and content pipelines often need precise word-level control that Python's stdlib doesn't provide. wordscalpel solves that, now with intelligent space normalization!

PyPI version License: MIT

Installation

pip install wordscalpel

⚡️ Python API (v2.0)

For v2.0, the API was drastically simplified while backwards-compatibility was retained. Intelligent space normalization prevents dangling spaces when deleting words inside code/log files.

import wordscalpel as ws

text = "the cat sat on the mat near the hat"

# Count occurrences
ws.count(text, "the")           # → 3

# Find with rich context
ws.find(text, "the", context=10) # Provides exact character metadata

# Remove occurrences (Default: intelligently cleans extra spaces!)
ws.remove(text, "the")                  # Remove all: "cat sat on mat near hat"
ws.remove(text, "the", n=2)             # Remove 2nd occurrence
ws.remove(text, "the", n=(1, 2))        # Remove range (1st and 2nd)

# Replace occurrences
ws.replace(text, "the", "a")            # Replace all
ws.replace(text, "the", "a", n=2)       # Replace 2nd occurrence
ws.replace(text, "the", "a", n=(1, 2))  # Replace range (1st and 2nd)

# Swap words simultaneously (avoids double-replace traps)
ws.swap("cat chased the dog", "cat", "dog") # → "dog chased the cat"

# Un-normalized raw mode (protects exact space-counts)
ws.remove("a b a c", "a", normalize=False)  # → " b  c "

📄 File Operations

Every core function has a file_* counterpart that safely mutates files on disk while retaining space layout structure!

from wordscalpel.file_ops import file_count, file_remove, file_replace, file_swap

# Count in file
file_count("input.txt", "error")

# Remove 2nd occurrence and save
file_remove("input.txt", "error", n=2, out="output.txt")

# Replace all
file_replace("input.txt", "error", "warning")

# Swap variables
file_swap("input.py", "user_id", "client_id")

💻 CLI Tools

# Remove 2nd occurrence of "error" from a file
wordscalpel remove --word "error" --n 2 --file input.txt --output out.txt

# Remove a specific range
wordscalpel remove --word "error" --range 2 5 --file input.txt

# Replace all occurrences
wordscalpel replace --word "foo" --with "bar" --all --file input.txt 

# Swap two words universally
wordscalpel swap --word "cat" --swap-with "dog" --file input.txt

# Count occurrences
wordscalpel count --word "error" --file input.txt

# Extract occurrences with 20px context string limits
wordscalpel find --word "Exception" --context 20 --file input.txt

# Pipe from stdin instantly
echo "hello world hello" | wordscalpel remove --word "hello" --n 1

🛡️ Error Handling

from wordscalpel.exceptions import WordscalpelError, OccurrenceNotFoundError

try:
    ws.remove("hello world", "hello", n=5)
except OccurrenceNotFoundError as e:
    print(e)  # Occurrence 5 of 'hello' not found. Total occurrences found: 1

Running Tests

pip install pytest
python3 -m pytest tests/ -v

License

MIT

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

wordscalpel-2.0.1.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

wordscalpel-2.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file wordscalpel-2.0.1.tar.gz.

File metadata

  • Download URL: wordscalpel-2.0.1.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for wordscalpel-2.0.1.tar.gz
Algorithm Hash digest
SHA256 cdacb39fe4e909f09263e96c6cb6fab051852bd6b71c255854a89ea6658ce95d
MD5 4f6930d8a3634f4ad8fbaa848231b912
BLAKE2b-256 b5a4a79a8c93e8b00a40c80dbb6dd1c79b54f7890934e978ab2c36b79b1142c6

See more details on using hashes here.

File details

Details for the file wordscalpel-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: wordscalpel-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for wordscalpel-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 944a042ef0cb9be4048398054050f49c7e51be29de471cfb95c9042e24097c62
MD5 7f23539503f15938195cf9aa9e6eca40
BLAKE2b-256 032cf4780ce8e92c6e7e4574462f15a653757f471b530d0f0d513a21ce35aa8a

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