Skip to main content

Text metamorphosis toolbox'

Project description

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 PyPA build) for PyPI publication.
    • Removed Debian system packaging shortcuts.
  • Converted unit tests to pytest and ceased to distribute them.

Added

  • Flake8 configuration via pyproject.toml, for use with pflake8.

Fixed

  • Linting.

0.5.1 - 2020-10-24

Changed

  • Switched from distutils.core to setuptools to enable wheel distribution.
  • Changed distribution name from Ovid to ovid.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ovid-0.6.0.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

ovid-0.6.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file ovid-0.6.0.tar.gz.

File metadata

  • Download URL: ovid-0.6.0.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ovid-0.6.0.tar.gz
Algorithm Hash digest
SHA256 e58768c0408f5fa3f06f7db1eec1a0705ef976759717705865f2c61988090835
MD5 f56d13c6f68327a5a458ddc94749a52a
BLAKE2b-256 df47f7d50809fd6eb4c82cca31b0e3e24c098c613edff1a40138138c93d78c23

See more details on using hashes here.

File details

Details for the file ovid-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: ovid-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for ovid-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56540041859991ebe095192c1e792f611fdc25bda072efed189821c1c700752d
MD5 6c22bd9026e5601bdc36a00d0e046102
BLAKE2b-256 fe8ec14f5b57f6b7b5adf5f4f896eef7078d46053fcde05724c855a5ab1ef4c3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page