An implementation of lazy strings. Can be used in combination with gettext for the translation of web apps.
It’s less beautiful than speaklater (an other implementation of lazy strings), but I wanted lazy strings that work even if a single thread interleaves the processing of many http requests (this happens with async frameworks).
postpone doesn’t rely on a global state. And it doesn’t use the thread.local() trick that speaklater uses either.
Example of use:
>>> from postpone import evalr, LazyString as _
>>> translations = {
"Order {item}.": "Commander {item}.",
"Take a nap": "Faire une sieste",
"Stare at the wall for %s minutes.": "Fixer le mur pendant %s minutes." ,
"a new pillow": "un nouvel oreiller"
}
>>> tasklist = [
_("Order {item}.").format(item = _("a new pillow")),
_("Take a nap") + '!',
_("Stare at the wall for %s minutes.") % 30
]
>>> evalr(tasklist, translations.get)
['Commander un nouvel oreiller.', 'Faire une sieste!', 'Fixer le mur pendant 30 minutes.']
evalr walks python dictionaries, lists, tuples or sets and apply a function to all the strings wrapped inside a LazyString object.
To apply a function to the strings inside a single expression, you can use the eval method:
>>> s = _("Take a nap") + '!'
>>> s.eval(str.upper)
'TAKE A NAP!'
>>> s.eval(translations.get)
'Faire une sieste!'
Or, again, the evalr function
>>> evalr(_("a new pillow").capitalize(), translations.get)
'Un nouvel oreiller'
For most projects containing more than a few strings or languages, you’ll probably want to use the gettext module to supply you with a translation function.
import gettext
translation = gettext.translation("myproject", "./locale", ["fr"])
translated_tasklist = evalr(tasklist, translation.ugettext)
GitHub repo: https://github.com/leforestier/postpone
Metadata
Release files for postpone 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| postpone-0.3.0.tar.gz | 3.0 kB | Without provenance | Details |
Release files / postpone-0.3.0.tar.gz
| Download URL | postpone-0.3.0.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f5e56320422ae8bd665c3b82eca017db194775b231ca6548db90cf8f0c0072df
|
|
BLAKE2b-256 checksum How to use checksums |
6bb5737b272c54d3e84c86d5bfc3694b27deb1b983a75e6ee856592b06066616
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for postpone 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.