Skip to main content

Library for defining and working with abstract regular expressions that work with any symbol type.

Project description

Library for defining and working with abstract regular expressions that support strings/sequences with elements of any symbol type, with an emphasis on supporting scenarios in which it is necessary to work with regular expressions as abstract mathematical objects.

PyPI version and link. Read the Docs documentation status. GitHub Actions status. Coveralls test coverage summary

Purpose

This library provides classes that enable concise construction of abstract regular expressions. In the case of this library, the term abstract refers to the fact that the symbols that constitute the “strings” (i.e., iterable sequences) that satisfy an abstract regular expression can be values or objects of any immutable type. Thus, this library also makes it possible to determine whether an iterable containing zero or more objects satisfies a given abstract regular expression. Any abstract regular expression can also be converted into a nondeterministic finite automaton (as implemented within the PyPI package) that accepts exactly those iterables which satisfy that abstract regular expression.

Package Installation and Usage

The package is available on PyPI:

python -m pip install are

The library can be imported in the usual way:

import are
from are import *

Examples

This library makes it possible to construct abstract regular expressions that work with a chosen symbol type. In the example below, a regular expression is defined (using only the literal and concatenation operators) in which symbols are integers. It is then applied to an iterable of integers. This returns the iterable’s length (as an integer) if that iterable satisfies the abstract regular expression:

>>> from are import *
>>> a = con(lit(1), con(lit(2), lit(3)))
>>> a([1, 2, 3])
3

If the longest prefix of an iterable that satisfies an abstract regular expression is desired, the full parameter can be set to False:

>>> a([1, 2, 3, 4, 5], full=False)
3

Operators for alternation and repetition of abstract regular expressions are also available:

>>> a = rep(con(lit(1), lit(2)))
>>> a([1, 2, 1, 2, 1, 2])
6
>>> a = alt(rep(lit(2)), rep(lit(3)))
>>> a([2, 2, 2, 2, 2])
5
>>> a([3, 3, 3, 3])
4

The emp constructor can be used to create an abstract regular expression that is satisfied by the empty iterable:

>>> a = emp()
>>> a([])
0

The nul constructor can be used to create an abstract regular expression that cannot be satisfied:

>>> a = nul()
>>> a([]) is None
True
>>> a([1, 2, 3]) is None
True

An abstract regular expression that has only string symbols can be converted into a regular expression string that is compatible with the built-in re library:

>>> a = alt(lit('x'), rep(lit('y')))
>>> r = a.to_re()
>>> r
'(((x)*)|((y)*))'
>>> import re
>>> r = re.compile(a.to_re())
>>> r.fullmatch('yyy')
<re.Match object; span=(0, 3), match='yyy'>

An abstract regular expression can also be converted into an NFA representation (as implemented within the PyPI package):

>>> a = con(lit(1), con(lit(2), lit(3)))
>>> a.to_nfa()
nfa({1: nfa({2: nfa({3: nfa()})})})

Documentation

The documentation can be generated automatically from the source files using Sphinx:

cd docs
python -m pip install -r requirements.txt
sphinx-apidoc -f -E --templatedir=_templates -o _source .. ../setup.py && make html

Testing and Conventions

All unit tests are executed and their coverage is measured when using pytest (see setup.cfg for configuration details):

python -m pip install pytest pytest-cov
python -m pytest

The subset of the unit tests included in the module itself can be executed using doctest:

python are/are.py -v

Style conventions are enforced using Pylint:

python -m pip install pylint
python -m pylint are ./test/test_are.py

Contributions

In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.

Versioning

Beginning with version 0.1.0, the version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.

Publishing

This library can be published as a package on PyPI by a package maintainer. Install the wheel package, remove any old build/distribution files, and package the source into a distribution archive:

python -m pip install wheel
rm -rf dist *.egg-info
python setup.py sdist bdist_wheel

Next, install the twine package and upload the package distribution archive to PyPI:

python -m pip install twine
python -m twine upload dist/*

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

are-2.0.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

are-2.0.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file are-2.0.0.tar.gz.

File metadata

  • Download URL: are-2.0.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.0

File hashes

Hashes for are-2.0.0.tar.gz
Algorithm Hash digest
SHA256 d3c0e9fe9ac5ec1774bd2a2a1c89b8b386534edc895549abbdaabfe223bcbe66
MD5 192a1bac5bd8b6737a54809c3cd48545
BLAKE2b-256 8fc11eabf636c82bc04b01cd89c7ee043bf982174d58bc86a5683f1c96baa914

See more details on using hashes here.

File details

Details for the file are-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: are-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.0

File hashes

Hashes for are-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6036a31adce81aed13a7b2265ee613f14d4cf92835b01d0c02fcf02b222c0ce4
MD5 e932d0dad424cc5b2929cd76284df5b5
BLAKE2b-256 43e4aa59e913b30536b3e991aa399717098f59d0f36b619321c47856e7fb2071

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