A Python library for implementing string-like classes and lazy strings.
stringlike
is available from
PyPI.
To install:
$ pip install stringlike
Or from the source:
$ python setup.py install
To implement your own StringLike
class, inherit from StringLike
and implement the __str__
magic function, like this:
from stringlike import StringLike class StringyThingy(StringLike): def __str__(self): return self.text_type("A string representation of my class.")
Instances ofStringLike
class have atext_type
property. By default it holds a propper string type for current version of Python:str
for Python 3 andunicode
for Python 2.
You can redefine text_type
property for your needs.
It's strongly recommended to use it anyway just to make your code compatible with different versions of Python.
Use the provided lazy string implementations like this:
from stringlike.lazy import LazyString, CachedLazyString print "This was lazily {str}".format(str=LazyString(lambda: "generated")) print "This is {str}".format(str=CachedLazyString(lambda: "cached"))
A more detailed example can be found here.
To run the unit tests, do this:
$ python tests/run_tests.py
To see the latest test results, check out stringlike
's Travis CI
page.
Special thanks to Eric Shull for much Python help!
This package is released under the MIT License. (See LICENSE.txt.)