Partially lockable strings
Project description
lstr: partially lockable Python strings
lstr
is a Python package for partially lockable strings.
Example
Create an lstr
via a regular string:
from lstr import lstr
greeting = lstr("Hello, world!")
print(greeting) # "Hello, world!"
Update an lstr
by invoking replace()
with the replacement string, the index to insert at, and the length of current string to replace.
index: 0 1 2 3 4 5 6 7 8 9 10 11 12
string: H e l l o , w o r l d !
For example, to change "Hello" to "Hey", the replacement would start at index 0
and have length 5
:
from lstr import lstr
greeting = lstr("Hello, world!")
greeting.replace("Hey", index=0, length=5)
print(greeting) # "Hey, world!"
To prevent the word "Hello" being changed, add a lock on that range:
from lstr import lstr
greeting = lstr("Hello, world!")
greeting.lock(index=0, length=5)
greeting.replace("Hey", index=0, length=5) # False
print(greeting) # "Hello, world!"
greeting.replace(" there", index=5, length=7) # True
print(greeting) # "Hello there!"
Installation
lstr
requires Python 3.8 or later.
pip install lstr
Usage
lstr(value: str, locks: List[Lock] = [])
requires a base string value, and optionally accepts a list of locks to apply.lstr.can_replace(index: int, length: int) -> bool
indicates whether or not a range can be replaced.lstr.lock(index: int, length: int) -> None
adds a lock.lstr.replace(value: str, index: int, length: int) -> bool
attempts to replace a given range with a new value. ReturnsTrue
if the replacement was permitted, otherwiseFalse
.
Thank you! 🎉
My name is Cariad, and I'm an independent freelance DevOps engineer.
I'd love to spend more time working on projects like this, but--as a freelancer--my income is sporadic and I need to chase gigs that pay the rent.
If this project has value to you, please consider ☕️ sponsoring me. Sponsorships grant me time to work on your wants rather than someone else's.
Thank you! ❤️
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.