Ovid: tools for text metamorphosis
This Python package is a templating engine. It will remind you of other such engines available for Python, such as the standard library’s string.Template, Jinja, and the Django template system.
Ovid works by pairing up regular expressions with functions. Both are needed to create an Ovid processor. You apply the processor to a string, and if the regular expression matches, the function receives the content of the matching groups from that expression. The function’s output replaces the match.
Examples
Here is a trivial example:
from ovid.basic import OneWayProcessor
def f(group):
return 3 * group
OneWayProcessor('(b)', f).sub('abc') # Returns 'abbbc'
As you can see, the regex matches b and identifies it as a group, which
Ovid passes to the function we have defined. The function does not
receive the match object.
A slightly more meaningful example follows, using a different Ovid class, through a decorator.
import random
from ovid.inspecting import SignatureShorthand as SS
_BARK_STATES = ('mostly stripped', 'brown', 'gray')
@SS.register
def melee(to_hit, damage, defense=''):
repl = f'{to_hit or "±0"} to hit with {damage or "±0"} damage.'
if defense:
repl += f' {defense} to be hit in melee.'
return repl
@SS.register
def wood():
return f'The bark is {random.choice(_BARK_STATES)}.'
sample = 'A stick. {{wood}} {{melee||+1|defense=-1}}'
SS.collective_sub(sample) # Return value:
# 'A stick. The bark is gray. ±0 to hit with +1 damage. -1 to be hit in melee.'
Here, the decorator adds our two functions to a registry, and the Ovid class constructs our regular expressions for us, with delimiters and separators that can be customized through subclassing. We apply both processors collectively, through a class method. Collective application supports recursion, nesting, and the passing of additional contextual information to processors.
Finally, Ovid processors can evert, outputting suitable input.
from ovid.producing import TwoWaySignatureShorthand
def hyperlink(href, text=None):
return f'<a href="{href}">{text or href}</a>'
processor = TwoWaySignatureShorthand(hyperlink)
processor.produce('https://www.fsf.org/', text='FSF')
# Return value: '{{hyperlink|https://www.fsf.org/|text=FSF}}'
processor.sub('{{hyperlink|https://www.python.org/psf/|text=PSF}}')
# Return value: '<a href="https://www.python.org/psf/">PSF</a>'
In this example, an object built from one function can produce a template, and parse such a template as in the first example.
Use cases
Ovid grew out of CBG. There, Ovid enables shorthand expressions in the manual text input that CBG uses to make playing cards. Because Ovid processors can evert, Ovid also combines with CBG to generate elegant raw text specifications for larger games.
A more complicated real-world use case is the maintenance of the author’s website. Here, Ovid refines specifications as a pre-processor to Markdown. This makes it easy to write a blog article that references a movie review that hasn’t been written yet. When the review is eventually added to the database, an Ovid processor finds it and adds a hyperlink to the article’s published form.
In the same process, the Django model instance that owns each raw string is passed through the Ovid layer to the encapsulated functions as contextual information, which enables these functions to map internal references in addition to replacing substrings.
Legal
Copyright 2015–2021 Viktor Eikman
Ovid is licensed as detailed in the accompanying file LICENSE.
Change log
This log follows the conventions of keepachangelog.com. It picks up from Ovid version 0.5.0.
Unreleased
Nothing yet.
0.6.0 - 2021-04-17
Removed
- Ceased to import all modules on loading
__init__.py, for performance.
Changed
- Modernized use of
setuptools(via PyPAbuild) for PyPI publication.- Removed Debian system packaging shortcuts.
- Converted unit tests to
pytestand ceased to distribute them.
Added
- Flake8 configuration via
pyproject.toml, for use withpflake8.
Fixed
- Linting.
0.5.1 - 2020-10-24
Changed
- Switched from
distutils.coretosetuptoolsto enable wheel distribution. - Changed distribution name from
Ovidtoovid.
Metadata
Release files for ovid 0.6.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 | Actions |
|---|---|---|---|---|
| ovid-0.6.0.tar.gz | 26.1 kB | Without provenance | Details for ovid-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ovid-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for ovid-0.6.0-py3-none-any.whl |
Total release size: 52.0 kB
Release files / ovid-0.6.0.tar.gz
| Download URL | ovid-0.6.0.tar.gz |
|---|---|
| Size | 26.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e58768c0408f5fa3f06f7db1eec1a0705ef976759717705865f2c61988090835
|
|
BLAKE2b-256 checksum How to use checksums |
df47f7d50809fd6eb4c82cca31b0e3e24c098c613edff1a40138138c93d78c23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6
|
Release files / ovid-0.6.0-py3-none-any.whl
| Download URL | ovid-0.6.0-py3-none-any.whl |
|---|---|
| Size | 25.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56540041859991ebe095192c1e792f611fdc25bda072efed189821c1c700752d
|
|
BLAKE2b-256 checksum How to use checksums |
fe8ec14f5b57f6b7b5adf5f4f896eef7078d46053fcde05724c855a5ab1ef4c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.6
|
Security for ovid 0.6.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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.