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

Designed for large-scale, structure-safe text transformations with streaming support.

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.

🛡️ Structured Capability Matrix

wordscalpel handles formats with precise awareness of structural memory scale constraints:

Format Streaming Engine Memory Safe Notes
Text Files (.log, .txt) ✅ Yes ✅ Yes O(1) Memory row-by-row stream. Perfect for standard massive files.
CSV ✅ Yes ✅ Yes O(1) streaming using standard Python csv module row-by-row parsing.
JSON ❌ No ⚠️ Varies Uses json.loads. Safe for metadata chunks, but loads structure into memory.
Python Objects N/A ⚠️ Varies Safely recurses through loaded active memory objects.

📦 Installation

pip install wordscalpel

💻 CLI Tools

Use the wordscalpel binary right from your terminal without opening Python! Note that every command safely supports --dry-run to print colorized terminal diffs of changes without mutating disk files.

# 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 (Preview changes safely via dry-run)
wordscalpel replace --word "DEBUG" --with "INFO" --range 1 5 --file server.log --dry-run

# 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 "

🔌 Plugin Architecture (Extensibility)

wordscalpel is an ecosystem. You can register custom data format adapters internally without modifying the library!

import wordscalpel as ws
import yaml

@ws.register_adapter("yaml")
def process_yaml(data: str, operation: str, word: str, **kwargs):
    parsed = yaml.safe_load(data)
    # Securely invoke our internal recursive object traverser
    processed = ws.remove_obj(parsed, word, **kwargs)
    return yaml.dump(processed)

# Now it flows securely through the main engine routing
ws.process(open("config.yml").read(), "remove", "password", adapter="yaml")

📄 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

# ONE KILLER DEMO: Safely scrub 15,000 deep targets off a 5GB 
# streaming file without loading it to RAM or affecting JSON brackets
file_remove("massive_production_export.log", "PASSWORD_HASH", n=(10000, 25000))

# 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.5.tar.gz (22.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.5-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wordscalpel-2.0.5.tar.gz
  • Upload date:
  • Size: 22.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.5.tar.gz
Algorithm Hash digest
SHA256 7bc8545f0341b7e17fdac2ae6811c3f085de03a1f383092cfa13684ee26e1028
MD5 d7a2682f2c7312add1fdb667e95e5cac
BLAKE2b-256 1e736032062d1f1fc6c991e8091719d008dc42b2eeedad0d71edc0c2660c58f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wordscalpel-2.0.5-py3-none-any.whl
  • Upload date:
  • Size: 18.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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7f314c1d8d96589f9fffa46cf1cfc1c43475afe5310adba1fb66beb981792135
MD5 2a9266c14648e881b2f18d5bba670781
BLAKE2b-256 13153f6dd9847d4008040fd315171bcc57d97daf1cb39bd82300221168e90d25

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