Skip to main content

A Python library for simulating and visualizing finite automata

Project description

automathon

automathon

A Python library for simulating and visualizing finite automata.

Test Quality Gate Package version


Documentation: https://rohaquinlop.github.io/automathon/

Source Code: https://github.com/rohaquinlop/automathon

PyPI: https://pypi.org/project/automathon/


Requirements

Installation

pip install automathon

Upgrade

pip install automathon --upgrade

Example

Here is are some examples about what you can do with automathon, you can check the documentation about Deterministic Finite Automata and Non-Deterministic Finite Automata to know more about the functions and methods that are available.

DFA - Deterministic Finite Automata

DFA Visualization

This image was created using automathon.

Let's create the previous automata using the library:

from automathon import DFA
q = {'q0', 'q1', 'q2'}
sigma = {'0', '1'}
delta = { 'q0' : {'0' : 'q0', '1' : 'q1'},
          'q1' : {'0' : 'q2', '1' : 'q0'},
          'q2' : {'0' : 'q1', '1' : 'q2'}
        }
initial_state = 'q0'
f = {'q0'}

automata = DFA(q, sigma, delta, initial_state, f)

Verify if the automata is valid

automata.is_valid()    # True

In this case, the automata is valid but if it wasn't, the library would raise an exception with the error message.

Errors

Errors that the library can raise are:

  • SigmaError:

    • The automata contain an initial state, or a final state that's not defined in Q.
    • The automata contain a delta transition that's not defined in Q nor Sigma.
  • InputError:

    • The automata is trying to consume a letter that's not defined in sigma.

Verify if the automata accept a given string

automata.accept("001001")  # True
automata.accept("00100")   # False

Get the automata's complement

not_automata = automata.complement()
not_automata.accept("00100")    #True

Note that this function returns a new automata, it doesn't modify the original one.

Visualize the automata

For both, DFA and NFA, the view method enables to visualize the automaton, receives as parameter a String as the file name for the png and svg files.

More information about the graphviz attributes here.

DFA Visualization

# Default styling
automata.view("DFA Visualization")

# If you want to add custom styling, you can use the following

automata.view(
    file_name="DFA Custom Styling",
    node_attr={'fontsize': '20'},
    edge_attr={'fontsize': '20pt'}
)

If you want to explore more about the functions and methods of the DFA class, you can check the DFA documentation. And if you want to know more about the NFA class, you can check the NFA documentation.

NFA - Non-Deterministic Finite Automata

Image taken from: r9paul.org

Representing the previous automata

from automathon import NFA

## Epsilon Transition is denoted by '' -> Empty string
q = {'q1', 'q2', 'q3', 'q4'}
sigma = {'0', '1'}
delta = {
    'q1' : {
            '0' : {'q1'},
            '1' : {'q1', 'q2'}
            },
    'q2' : {
            '0' : {'q3'},
            '' : {'q3'}
            },
    'q3' : {
            '1' : {'q4'},
            },
    'q4' : {
            '0' : {'q4'},
            '1' : {'q4'},
            },
}
initial_state = 'q1'
f = {'q4'}

automata = NFA(q, sigma, delta, initial_state, f)

Verify if the automata is valid

automata.is_valid()  # True

Verify if the automata accept a string

automata.accept("0000011")   #True
automata.accept("000001")    #False

Get the automata's complement

not_automata = automata.complement()
not_automata.accept("000001")    #True

Visualize the automata

NFA Visualization

# Default styling
automata.view("NFA Visualization")

# If you want to add custom styling, you can use the following

automata.view(
    file_name="NFA Custom Styling",
    node_attr={'fontsize': '20'},
    edge_attr={'fontsize': '20pt'}
)

License

This project is licensed under the terms of the MIT license.

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

automathon-0.0.15.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

automathon-0.0.15-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file automathon-0.0.15.tar.gz.

File metadata

  • Download URL: automathon-0.0.15.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for automathon-0.0.15.tar.gz
Algorithm Hash digest
SHA256 81106726b5e44b8bb530b0c76468e027987ef9774010a469e0f8a1f2c2ae2171
MD5 c580542fe0e6cc12b159eca9cdf5e098
BLAKE2b-256 0397fb50d1a2d093d8766ee42088a73caea70c0c9b90494a107380ccd504804a

See more details on using hashes here.

File details

Details for the file automathon-0.0.15-py3-none-any.whl.

File metadata

  • Download URL: automathon-0.0.15-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for automathon-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 93ff6f25a0022cf52633d12cc91938a8c3326bd3580290a4bc81f2378b7036db
MD5 8f742ae2a57e6531b979bb9d1ef39cd5
BLAKE2b-256 cbba3c8bfeb61437c78cf3616cd67670326798fba5709544edd9046c2e099eef

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