Tools for manipulating strings while retaining source positions in the resulting object
Project description
Snaptext
Snaptext is a Python package that provides tools for manipulating strings while retaining source positions in the resulting object.
Installation
$ pip install snaptext
Usage
Indexing, concatenation and stripping
from snaptext import Source
source = Source("123 / 456")
source[0:4].area.format()
# =>
# 123 / 456
# ^^^^
source[0:4].strip().area.format()
# =>
# 123 / 456
# ^^^
(source[0:3] + source[-3:]).area.format()
# =>
# 123 / 456
# ^^^ ^^^
Regular expression matching
text = Source("Pi is about 3.1415.")
match = text.transform_match(text.search(r"(\d+)\.(\d+)"))
match.group().area.format()
# =>
# Pi is about 3.1415.
# ^^^^^^
match.group(2).area.format()
# =>
# Pi is about 3.1415.
# ^^^^
Regular expression substitution
text = Source("The sum of 3.02 and 12.8 is 15.82")
result = text.sub(r"(\d+)\.(\d+)", r"\1,\2")
# => 'The sum of 3,02 and 12,8 is 15,82'
result.area.format()
# =>
# The sum of 3.02 and 12.8 is 15.82
# ^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^ ^^
Example: JSON parser
from snaptext.json import loads
result = loads("""
{
"a": "x",
"b": [13, 14, 15],
"c": true
}
""")
result.value['b'].value[1].area.format()
# =>
# "b": [13, 14, 15],
# ^^
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.
Source Distribution
snaptext-0.1.1.tar.gz
(7.5 kB
view details)
Built Distribution
File details
Details for the file snaptext-0.1.1.tar.gz
.
File metadata
- Download URL: snaptext-0.1.1.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4747e114a5e490920043c65a0ecb2f2ddbb60efbb7fcea5a98b4ad712203c3c0 |
|
MD5 | 92ced7ea527dbd01cf88637a6fae60ad |
|
BLAKE2b-256 | da2a8b13cbddc1017ebae56ffa9a1afef358099d1aeea246ae0792e54184b198 |
File details
Details for the file snaptext-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: snaptext-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eeb0a6349fc5d68311d2b3dd294d5681821532c7f3ffd4c376eb514d076bdec9 |
|
MD5 | f40e38f6e601e9063dd768e95bfa1f34 |
|
BLAKE2b-256 | 3e67c8e96f6b7438ab1be048156c6e680e17beb8bc031a8654ce2a2ce7952140 |