Skip to main content

Library for defining and working with native Python implementations of NFAs.

Project description

Library for defining and working with native Python implementations of nondeterministic finite automata (NFAs).

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

Purpose

This library makes it possible to concisely construct nondeterministic finite automata (NFAs) using common Python data structures and operators, as well as to perform common operations involving NFAs. NFAs are represented using a class derived from the Python dictionary type, wherein dictionary objects serve as individual states and dictionary entries serve as transitions (with dictionary keys representing transition labels).

Package Installation and Usage

The package is available on PyPI:

python -m pip install nfa

The library can be imported in the usual way:

import nfa
from nfa import nfa

Examples

This library makes it possible to concisely construct an NFA. In the example below, an NFA is defined in which transition labels are strings. It is then applied to an iterable of strings. This returns the length (as an integer) of the longest path that (1) traverses an ordered sequence of transitions whose labels match the sequence of symbols supplied as the argument and (2) terminates at an accepting state:

>>> from nfa import nfa
>>> n = nfa({'a': nfa({'b': nfa({'c': nfa()})})})
>>> n(['a', 'b', 'c'])
3

By default, an empty NFA object nfa() is an accepting state and a non-empty object is not an accepting state. When an NFA is applied to an iterable of labels that does not traverse a path that leads to an accepting state, None is returned:

>>> n(['a', 'b']) is None
True

To ensure that a state is not accepting, the built-in prefix operator - can be used:

>>> n = nfa({'a': nfa({'b': nfa({'c': -nfa()})})})
>>> n(['a', 'b', 'c']) is None
True

The prefix operator + yields an accepting state and the prefix operator ~ reverses whether a state is accepting:

>>> n = nfa({'a': ~nfa({'b': +nfa({'c': nfa()})})})
>>> n(['a'])
1
>>> n(['a', 'b'])
2

Applying the built-in bool function to an nfa object returns a boolean value indicating whether that that specific object (and not the overall NFA within which it may be an individual state) is an accepting state:

>>> bool(n)
False
>>> bool(nfa())
True
>>> bool(-nfa())
False

Epsilon transitions can be introduced using the epsilon object:

>>> from nfa import epsilon
>>> n = nfa({'a': nfa({epsilon: nfa({'b': nfa({'c': nfa()})})})})
>>> n(['a', 'b', 'c'])
3

If an NFA instance is applied to an iterable that yields enough symbols to reach an accepting state but has additional symbols remaining, None is returned:

>>> n(['a', 'b', 'c', 'd', 'e']) is None
True

If the length of the longest path leading to an accepting state is desired (even if additional symbols remain in the iterable), the full parameter can be set to False:

>>> n(['a', 'b', 'c', 'd', 'e'], full=False)
3

It is possible to retrieve the set of all transition labels that are found in the overall NFA (note that this does not include instances of epsilon):

>>> n.symbols()
{'c', 'a', 'b'}

Because the nfa class is derived from dict, it supports all operators and methods that are supported by dict. In particular, the state reachable from a given state via a transition that has a specific label can be retrieved by using index notation:

>>> n.keys()
dict_keys(['a'])
>>> m = n['a']
>>> m(['b', 'c'])
2

To retrieve the collection of all states that can be reached via paths that involve zero or more epsilon transitions (and no labeled transitions), the built-in infix operator % can be used (note that this also includes all intermediate states along the paths to the first labeled transitions):

>>> b = nfa({epsilon: nfa({'b': nfa()})})
>>> c = nfa({'c': nfa()})
>>> n = nfa({epsilon: [b, c]})
>>> for s in (n % epsilon): print(s)
...
nfa({epsilon: [nfa({epsilon: nfa({'b': nfa()})}), nfa({'c': nfa()})]})
nfa({epsilon: nfa({'b': nfa()})})
nfa({'c': nfa()})
nfa({'b': nfa()})

Other methods make it possible to retrieve all the states found in an NFA, to compile an NFA (enabling more efficient processing of iterables), and to compile an NFA into a deterministic finite automaton (DFA). Descriptions and examples of these methods can be found in the documentation for the main library module.

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 nfa/nfa.py -v

Style conventions are enforced using Pylint:

python -m pip install pylint
python -m pylint nfa ./test/test_nfa.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

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

nfa-3.0.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nfa-3.0.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file nfa-3.0.1.tar.gz.

File metadata

  • Download URL: nfa-3.0.1.tar.gz
  • Upload date:
  • Size: 16.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 nfa-3.0.1.tar.gz
Algorithm Hash digest
SHA256 ec9590e2cc08e5a970fa14e55055649ad935e4e21b3e5162e5323cf0649e96dc
MD5 645556165dd9a95a167de0a8497f9f56
BLAKE2b-256 d9dbfab1db21fb5777033aca104bdac4f9738986290c4030a99203b07a83712e

See more details on using hashes here.

File details

Details for the file nfa-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: nfa-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 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 nfa-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 de854adc1cf220db85326763a87a7301fa2c43cb28fbb746db0a9506e359b31e
MD5 2152d70787e07780d44de26fb3e4a25c
BLAKE2b-256 6a984a7afbd6e97281752157c1289b45b2e3ec10458d32a68cd4860e36607345

See more details on using hashes here.

Supported by

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