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.

PyPI version Python Versions License: MIT

Log sanitization, document redaction, and content pipelines often need precise word-level control that Python's standard library doesn't provide.
wordscalpel solves that permanently.


⚡️ Why wordscalpel?

Standard Python .replace() and Regex are blind—they either replace everything, the first occurrence, or require complex error-prone patterns. wordscalpel allows you to target exact integer occurrences or ranges, swap terms cleanly, and stream 10GB files without breaking a sweat.

🆚 How it compares

Feature wordscalpel Standard .replace() Regex (re.sub) sed (CLI)
Replace $N$th occurrence ✅ Native ❌ No ⚠️ Complex logic ⚠️ Awkward syntax
Replace occurrence ranges ✅ Native ❌ No ❌ No ⚠️ Very hard
Simultaneous A↔B Swap ✅ Native ❌ Breaks on overlap ⚠️ Custom functions ⚠️ Difficult
Smart Space Cleanup ✅ Native ❌ No ⚠️ Manual patterns ⚠️ Manual patterns
O(1) Memory Streaming ✅ Native ❌ Loads entire string ❌ Loads entire string ✅ Yes
Indentation Safe ✅ Yes ❌ Blind wipe ⚠️ Manual Lookbehinds ⚠️ Blind wipe

🎯 Core Capabilities

  • Intelligent Space Normalization: Safely swallows extra bounding spaces upon deletion to preserve code alignments, without breaking text structure or indentation.
  • O(1) Streaming Engine: Never loads massive .log or .sql files into memory; files are operated on dynamically chunk-by-chunk for unmatched speed.
  • Perfect Safety: Synchronous word-swapping absolutely prevents "double-replace" collision traps.
  • Zero Dependencies: Built entirely using the Python standard library.

📦 Installation

pip install wordscalpel

💻 CLI Tools

Use the wordscalpel binary right from your terminal without opening Python!

# 1. Remove the EXACT 2nd occurrence of 'error' from a log
wordscalpel remove --word "error" --n 2 --file server.log --output out.log

# 2. Re-write the first 5 occurrences 
wordscalpel replace --word "DEBUG" --with "INFO" --range 1 5 --file server.log

# 3. Swap variables simultaneously everywhere
wordscalpel swap --word "cat" --swap-with "dog" --file input.txt

# 4. Extract instances with beautiful surrounding context arrays (-c chars)
wordscalpel find --word "Exception" --context 20 --file server.log

🐍 Python API (v2.0)

Using the newly refined minimal API, text operations are universally effortless.

import wordscalpel as ws

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

# 🔍 Inspection
ws.count(text, "the")               # → 3
ws.find(text, "the", context=10)    # → Metadata and surrounding substrings

# ✂️ Smart Removal (Intelligently drops adjacent spaces natively)
ws.remove(text, "the", n=2)         # → "the cat sat on mat near the hat"
ws.remove(text, "the", n=(1, 2))    # Remove 1st and 2nd

# 🔁 Swaps & Replacements
ws.replace(text, "the", "a", n=2)   # → "the cat sat on a mat near the hat"
ws.swap("cat chased dog", "cat", "dog") # → "dog chased cat"

Advanced Control (Space Targeting)

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

📄 File Stream Operations

Every core function has a file_* counterpart that safely mutates massive files on disk using efficient O(1) memory pipelines.

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

# Process a multi-gigabyte log in milliseconds
file_remove("giant_input.log", "PASSWORD_HASH", n=None, out="sanitized.log")

# Count targets without crashing RAM
total = file_count("input.txt", "error")

# Change specific variable definitions safely inline
file_replace("input.py", "deprecated_var", "new_var", n=2)

🛡️ Error Safety (Exceptions)

Strict adherence to safe typing and predictable error catching:

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

Developed via TDD. 100% Core coverage.

pip install pytest
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.3.tar.gz (18.0 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.3-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wordscalpel-2.0.3.tar.gz
  • Upload date:
  • Size: 18.0 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.3.tar.gz
Algorithm Hash digest
SHA256 d1be7345b866c5136dc34e00eb0b2b6d51e114ac55a522175ae09883a29ab049
MD5 acb3af2c7ef219edd732b7e3b3fdccc4
BLAKE2b-256 47c8d09eb42ccd0d151ebf0eb9871a10ad3baa61d3cd8311fb015bce95d2ec3c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wordscalpel-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 14.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 852928e25d3f97b57460c8a7cf348ea90d80f09ce20fda616915b67ccdf130a1
MD5 87e60110279a16394600def77f4ceb0d
BLAKE2b-256 d05fe57cc9b65ea89a347376a637666e10276f71947ed04308315d9f7f56ee27

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