Package: nfa
Pure-Python library for building and working with nondeterministic finite automata (NFAs).PurposeThis 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).Installation and UsageThis library is available as apackage on PyPI:python -m pip install nfaThe library can be imported in the usual way:import nfa
from nfa import nfaExamplesThis library makes it possible to concisely construct an NFA by using one or more instances of thenfaclass. In the example below, an NFA is defined in which transition labels are strings:>>> from nfa import nfa
>>> n = nfa({'a': nfa({'b': nfa({'c': nfa()})})})Thenfaobject can be applied to a sequence of symbols (represented as an iterable of transition labels). This returns the length (as an integer) of the longest path that (1) traverses an ordered sequence of the NFA’s transitions whose labels match the sequence of symbols supplied as the argument and (2) terminates at an accepting state:>>> n(['a', 'b', 'c'])
3By default, an empty NFA objectnfa()is an accepting state and a non-empty object isnotan accepting state. When an NFA is applied to an iterable of labels that does not traverse a path that leads to an accepting state,Noneis returned:>>> n(['a', 'b']) is None
TrueTo ensure that a state is not accepting (even if it is empty), the built-in prefix operator-can be used:>>> n = nfa({'a': nfa({'b': nfa({'c': -nfa()})})})
>>> n(['a', 'b', 'c']) is None
TrueThe prefix operator+returns 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'])
2Applying the built-inboolfunction to annfaobject returns a boolean value indicating whetherthat specific object(andnotthe overall NFA within which it may be an individual state) is an accepting state:>>> bool(n)
False
>>> bool(nfa())
True
>>> bool(-nfa())
FalseEpsilon transitions can be introduced using theepsilonobject:>>> from nfa import epsilon
>>> n = nfa({'a': nfa({epsilon: nfa({'b': nfa({'c': nfa()})})})})
>>> n(['a', 'b', 'c'])
3If an NFA instance is applied to an iterable that yields enough symbols to reach an accepting state but has additional symbols remaining,Noneis returned:>>> n(['a', 'b', 'c', 'd', 'e']) is None
TrueIf the length of the longest path leading to an accepting state is desired (even if additional symbols remain in the iterable), thefullparameter can be set toFalse:>>> n(['a', 'b', 'c', 'd', 'e'], full=False)
3It is possible to retrieve the set of all transition labels that are found in the overall NFA (note that this does not include instances ofepsilon):>>> n.symbols()
{'c', 'a', 'b'}Because thenfaclass is derived fromdict, it supports all operators and methods that are supported bydict. 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'])
2To retrieve the collection ofallstates 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 includesallintermediate 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 toretrieve all the states found in an NFA, tocompile an NFA(enabling more efficient processing of iterables), and totransform an NFA into a deterministic finite automaton (DFA). Descriptions and examples of these methods can be found in thedocumentation for the main library module.DevelopmentAll installation and development dependencies are fully specified inpyproject.toml. Theproject.optional-dependenciesobject is used tospecify optional requirementsfor various development tasks. This makes it possible to specify additional options (such asdocs,lint, and so on) when performing installation usingpip:python -m pip install .[docs,lint]DocumentationThe documentation can be generated automatically from the source files usingSphinx:python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make htmlTesting and ConventionsAll unit tests are executed and their coverage is measured when usingpytest(see thepyproject.tomlfile for configuration details):python -m pip install .[test]
python -m pytestThe subset of the unit tests included in the module itself can be executed usingdoctest:python src/nfa/nfa.py -vStyle conventions are enforced usingPylint:python -m pip install .[lint]
python -m pylint src/nfa test/test_nfa.pyContributionsIn order to contribute to the source code, open an issue or submit a pull request on theGitHub pagefor this library.VersioningThe version number format for this library and the changes to the library associated with version number increments conform withSemantic Versioning 2.0.0.PublishingThis library can be published as apackage on PyPIby a package maintainer. First, install the dependencies required for packaging and publishing:python -m pip install .[publish]Ensure that the correct version number appears inpyproject.toml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has anautomation rulethat activates and sets as the default all tagged versions. Create and push a tag for this version (replacing?.?.?with the version number):git tag ?.?.?
git push origin ?.?.?Remove any old build/distribution files. Then, package the source into a distribution archive:rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .Finally, upload the package distribution archive toPyPI:python -m twine upload dist/*