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.4.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.4-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.4-cp313-cp313-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

exhash-0.2.4-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.4-cp312-cp312-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

exhash-0.2.4-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.4-cp311-cp311-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

exhash-0.2.4-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.4-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.4.tar.gz.

File metadata

  • Download URL: exhash-0.2.4.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.4.tar.gz
Algorithm Hash digest
SHA256 c96d09174068cedc946e511db644635e7e30696cb0b8344b53e958189bc6350c
MD5 b1e1f14224797e05b97ebaaa43d8bde2
BLAKE2b-256 c89a0072a83a1afd32b6e6ee9b8414ea8621767e7da3ded920d594d0f6f43a5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4.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.4-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 83a393da443f7c93bfca28bf8e44dcab516218e90be94315bf4bb67d21127727
MD5 301c88dba95c57b9b25d072f3078fb3b
BLAKE2b-256 1b073a9e1edf31301f5dad3369ee68f17e4e62bbea89a7a2782b748dcb74d3ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e0f9c044c0bfe35dd1994c35d30f68f46b4c4153664d714b567caf75a4190afd
MD5 6aa5c9f809613f659675cf6b2972a646
BLAKE2b-256 a8da318ba4af7f83bd99364dded19c06c95eb1539db5ae4e35ab92ad5e3dd8eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2b005215247c487c85477dbefa23f1fc11952facf5fa07a82100e99505e12d45
MD5 0ba5b37379fb8426bf4950200b230ab0
BLAKE2b-256 71bec0a46d955fd9d1bce0392e92b09d634b7c3d628b48eb76d413924d8ab10a

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 803c2268be33d90162ac3d8f4ee69ca08f6eec0a80ea4d905d125ec4b2a50664
MD5 9b4f08290feab6653a3e848c762a5dad
BLAKE2b-256 3c2d4311f7631b003c88c701701db48813563f234457686ef29b3b6bee98b44a

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c6b67a61b728aff9d12edfbf608bea2167685f20f30a37952511f3e23f9401c1
MD5 7d5a8c2b0d76b1c4cc2b49def8bd3688
BLAKE2b-256 f2233f36c3ac017171b9f1cc4a118e454e2e8c357c4eba87bfea031cf0e8f61a

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19a990d081e9d7717d386a45c5d3598fe57d615a671452b94e3de33d88e89929
MD5 94c84228dd1dc3d456abd72de30dd4e8
BLAKE2b-256 6ac069cf476a39b7dfc9aa2f7040971ace67dfbadc3a8702e31b23188908bacc

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 63970f2ea94f35cbbfb7b2cf81d7a7c739f9dc1504f906f8b99c9d2690cb40ff
MD5 cd35fb6f5da49d87d415cc9fdad9f625
BLAKE2b-256 f202fecc08bc47d45bd23dde5dfd707eb77c8a8bb4db47468c13d024cfb447fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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.4-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for exhash-0.2.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 61bac960f65c4fc55b0abae5f73fbf4314067823fd945e57ae65318ad95dd3ef
MD5 689a4c19cdab33c4defcbbab88f7cd3c
BLAKE2b-256 97c635df08b572818be72681fc7b8314adf4883dd26239a8d6193c30fc9db70b

See more details on using hashes here.

Provenance

The following attestation bundles were made for exhash-0.2.4-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