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()
source[0:4].strip().area.format()
(source[0:3] + source[-3:]).area.format()
Regular expression matching
text = Source("Pi is about 3.1415.")
match = text.transform_match(text.search(r"(\d+)\.(\d+)"))
match.group().area.format()
match.group(2).area.format()
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")
result.area.format()
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()