Skip to main content

Verified line-addressed file editor using lnhash addresses

Project description

exhash — Verified Line-Addressed File Editor

exhash combines Can Bölük's very clever line number + hash editing system with the powerful and expressive syntax of the classic ex editor.

Install via pip to get both a convenient Python API, and native CLI binaries:

pip install exhash

Or install just the CLI binaries via cargo:

cargo install exhash

lnhash format

We refer to an lnhash as a tag of the form lineno|hash|, where hash is the lower 16 bits of Rust's DefaultHasher over the line content.

Address forms:

  • lineno|hash| — hash-verified address
  • $ — last line (no hash)
  • % — whole file (1,$, no hashes)

CLI

The native Rust binaries are installed into your PATH via pip.

View

# Shows every line prefixed with its lnhash
lnhashview path/to/file.txt
# Optional line number range to show
lnhashview path/to/file.txt 10 20

Edit

# Substitute on one line
exhash file.txt '12|abcd|s/foo/bar/g'

# Transliterate characters on one line
exhash file.txt '12|abcd|y/abc/ABC/'

# Append multiline text (terminated by a single dot)
exhash file.txt '12|abcd|a' <<'EOF'
new line 1
new line 2
.
EOF

# Dry-run
exhash --dry-run file.txt '12|abcd|d'

# Set shift width for < and >
exhash --sw 2 file.txt '12|abcd|>1'

# Last line and whole file shorthands (no hash)
exhash file.txt '$d'
exhash file.txt '%j'

# Move a line to EOF using $ as the destination
exhash file.txt '12|abcd|m$'

Substitute uses Rust regex syntax:

  • Pattern syntax is from regex
  • Replacement syntax is from regex::Replacer, e.g. $1, $0, ${name}
  • \/ escapes the command delimiter in pattern/replacement
  • Transliteration uses y/src/dst/ and requires source/destination to have equal character counts

When passing multiple commands, each command's lnhashes are verified immediately before that command runs.

For a/i/c commands, provide the text block on stdin:

printf "new line 1\nnew line 2\n.\n" | exhash file.txt "2|beef|a"

Stdin filter mode

cat file.txt | exhash --stdin - '1|abcd|s/foo/bar/'

In --stdin mode, multiline a/i/c text blocks are not available.

Python API

from exhash import exhash, exhash_result, lnhash, lnhashview, line_hash

Viewing

text = "foo\nbar\n"
view = lnhashview(text)  # ["1|a1b2|  foo", "2|c3d4|  bar"]

Editing

exhash(text, cmds, sw=4) takes the text and a required iterable of command strings (use [] for no-op). sw controls how far < and > shift. For a/i/c commands, lines after the command are the text block (no . terminator needed):

addr = lnhash(1, "foo")  # "1|a1b2|"
res = exhash(text, [f"{addr}s/foo/baz/"])
print(res["lines"])    # ["baz", "bar"]
print(res["modified"]) # [1]

# Multiple commands
a1, a2 = lnhash(1, "foo"), lnhash(2, "bar")
res = exhash(text, [f"{a1}s/foo/FOO/", f"{a2}s/bar/BAR/"])

# Hashes are checked just-in-time per command.
# If earlier commands change/shift a later target line, recompute lnhash first.

# Append multiline text (no dot terminator)
res = exhash(text, [f"{addr}a\nnew line 1\nnew line 2"])

# Change shift width for < and >
res = exhash(text, [f"{addr}>1"], sw=2)

Result dict

  • lines — list of output lines
  • hashes — lnhash for each output line
  • modified — 1-based line numbers of modified/added lines
  • deleted — 1-based line numbers of removed lines (in original)

exhash_result([res1, res2, ...]) renders modified lines in lnhash format, matching the old repr(EditResult) style.

Tests

cargo test && pytest -q

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

exhash-0.2.5.tar.gz (27.1 kB view details)

Uploaded Source

Built Distributions

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

exhash-0.2.5-cp313-cp313-manylinux_2_34_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

exhash-0.2.5-cp313-cp313-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

exhash-0.2.5-cp312-cp312-manylinux_2_34_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

exhash-0.2.5-cp312-cp312-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

exhash-0.2.5-cp311-cp311-manylinux_2_34_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

exhash-0.2.5-cp311-cp311-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

exhash-0.2.5-cp310-cp310-manylinux_2_34_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

exhash-0.2.5-cp310-cp310-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file exhash-0.2.5.tar.gz.

File metadata

  • Download URL: exhash-0.2.5.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for exhash-0.2.5.tar.gz
Algorithm Hash digest
SHA256 4b1da87e193d84671e4c5709d263fefb98cafc570163d895d3d6773f29d099dc
MD5 24ccb452a73b437f727594a570a6c47c
BLAKE2b-256 7497ff358f9e671c0c0e725fe2fcf0d26ca8ce48919bd6cb1255e37d5d7c712a

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5.tar.gz:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1dc0926c90323b64428a546cbeef1b4c9aee27d5347dc55e372fc2943c8635ee
MD5 774732a134a360acb600309934dcb4e3
BLAKE2b-256 79b02c25be100604971debde7a338a91f644b258a4534737c86af29b71a58bc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc0329bef2c441cee4aec112fefd71797de503b14c764e6044ac379f37131426
MD5 f52c751ac671373d76f8577f19f682a7
BLAKE2b-256 b7673bfa19b92d652e1e2461df8805e85e1d87b2f2487d7244f256d0466038e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1f37064cde9d452e61da10f59c143f7bd7d223ad784c64d36e16eba3d9131253
MD5 cae630b1d830ad15818be7807223a052
BLAKE2b-256 d233b3be2ef65688e034beae3d4dd23585027bb13c4ee3602c13db1c317022c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 17269f7a49619ee41e28e6f13746014df12f9b363902830b5db962b6dd7bfd4b
MD5 82b2126e71cd41a8778e867988e049a6
BLAKE2b-256 f919a1403651467a74970b5c3345e94025c32b492a178fc6c580001929ca3133

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 35ed914621cfe63e20eedddbb52527b6b1faa39d6b5f0e46340ceadd2b3c4745
MD5 d1c5365511a96103f4e068a020467ab4
BLAKE2b-256 5d37ef315be9783979d12a4468e0868e134ca71faedd674bb38f2017454817e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp311-cp311-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a8f05a6f53c48914ef545a7bf6e7bf6464f2981503dc63a4a737a1d58f309842
MD5 613d3f214b73b9a266cc58db636ee215
BLAKE2b-256 86a71ea5690cbde22f168802af98838112e70d644d023c2d030606e74af64373

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 abd1806ab0186b815ab1381b5dbf10fdc5cd13964caec349cb39027c700a8e47
MD5 599fac70e4bbbb5eaca3c223734ac4d5
BLAKE2b-256 d16d23ff4c90387bbae87b7c958dbb24fb0010015e55fbf33131543d8878b3fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp310-cp310-manylinux_2_34_x86_64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file exhash-0.2.5-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d712ac883676e7627ea7a501ce54cd1c679af90f7352d93eac2c7f5578c2c11f
MD5 8bea55b1bb78cda02021d16054df572f
BLAKE2b-256 f56b8622de175294cd0b1ef10deabd12ae0fa8e93bed79c2d75a27b7fc0ddf1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.5-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: ci.yml on AnswerDotAI/exhash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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