Skip to main content

Partially lockable strings

Project description

lstr: partially lockable Python strings

lstr is a Python package for partially lockable strings.

Installation

lstr requires Python 3.8 or later.

pip install lstr

Examples

Creating an lstr

from lstr import lstr

greeting = lstr("Hello, world!")
print(greeting)
Hello, world!

Getting help with indexes

from lstr import lstr

greeting = lstr("Hello, world!")
print(repr(greeting))
  0  1  2  3  4  5  6  7  8  9 10 11 12
  H  e  l  l  o  ,     w  o  r  l  d  !

Inserting text

from lstr import lstr

greeting = lstr("Good morning, Bobby!")
greeting.write("Captain ", index=14)
print(greeting)
Good morning, Captain Bobby!

Overwriting text

from lstr import lstr

greeting = lstr("Good morning, Captain Bobby!")
greeting.write("Fleet Admiral", index=14, length=7)
print(greeting)
Good morning, Fleet Admiral Bobby!

Substituting text

from lstr import lstr

greeting = lstr("Good morning, Fleet Admiral Bobby!")
greeting.sub("morning", "evening")
print(greeting)
Good evening, Fleet Admiral Bobby!

Substituting text with a regular expression

from lstr import lstr

greeting = lstr("Good evening, Fleet Admiral Bobby!")
greeting.sub(r"(Fleet Admiral)", r"🎉\g<1>🎉")
print(greeting)
Good evening, 🎉Fleet Admiral🎉 Bobby!

Locking a range

from lstr import lstr

greeting = lstr("Good morning, Fleet Admiral Bobby!")
greeting.lock(index=14, length=13)

greeting.write("Ensign", index=14, length=13)
print(greeting)
Good morning, Fleet Admiral Bobby!

Locking a substitution

from lstr import lstr

greeting = lstr("Good morning, Captain Bobby!")
greeting.sub("Captain", "Fleet Admiral", lock=True)
greeting.sub("Fleet Admiral", "Ensign")

print(greeting)
Good morning, Fleet Admiral Bobby!

Equality

lstr instances are considered equal only if their string value and locks are identical.

lstr("f", locks=[Lock(index=1, length=2)]) == lstr("f", locks=[Lock(index=1, length=2)])
lstr("f", locks=[Lock(index=1, length=2)]) != lstr("f", locks=[Lock(index=3, length=4)])

lstr and str instances are considered equal if the string value is identical, regardless of the locks.

lstr("f") == "f"
lstr("f") != "g"

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

lstr-1.0.0-py3-none-any.whl (6.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page