A way to simplify development of CLI applications
Project description
FiniteConsole
A way to simplify development of CLI applications.
Getting started
This module is based on the finite state machine pattern. It may be logic to represent
a list of options in the terminal (a page) as a state. So the other lists of
options (pages) are the other states. The program can be represented as an
oriented graph with mappings from one state to another one.
Further the terms "menu" and "option" are interchangeable with the terms
"state" and "mapping".
Create the program
Use the Program class to create the program object which is singleton. You can
initialize the program with a Menu or append it later.
from FiniteConsole import Program, Menu
p = Program() # Empty
p.init_menu = Menu('main')
p.drop()
p = Program(Menu('initial'))
The program can be dropped with the drop method.
Define a state (a menu)
The states are instances of Menu class.
The constructor takes a required parameter id_ which is its id. It must be unique
for a whole program!
from FiniteConsole import Menu
main_menu = Menu('main')
After you have created the Program instance, all new Menu instances
will be appended to the program automatically.
from FiniteConsole import Program, Menu
p = Program(Menu('main'))
Menu(1)
Menu('another_one')
print(p.menus)
# {'main': <Menu object...>, '1': <Menu object...>, 'another_one': <Menu object...>}
The second parameter action is a function, basically it is None. It means the
state is not finite.
Finite states do something and then returns to the precedent state.
import sys
from FiniteConsole import Menu
main_menu = Menu('main')
exit_menu = Menu('exit', lambda: sys.exit())
If the program state changes on "exit", the function will be executed.
Parameters are passed to the function via args and kwargs attributes of
the program, they are cleared at every loop iteration. Returned value is in the
result attribute of the program, it is conserved until the function returns a
new value.
from FiniteConsole import Program, Menu
finite = Menu('calc', lambda x: x*x)
p = Program(finite)
p.start_loop() # E.g. as a thread
p.args = [5]
...
print(p.result)
# 25
To remove states use remove_menus method of the Program instance. You can
pass either str containing ids or Menu instances.
Append a mapping (an option)
Use append_options of Menu instance to append options to the menu.
It takes Option object, which parameters are inp - input name,
out - output name, description - text for CLI.
import sys
from FiniteConsole import Menu, Option
main_menu = Menu('main').append_options(
Option('1', 'exit', 'Quit'),
Option('2', 'do_stuff_1', 'Amazing stuff'),
Option('yes', 'Another menu', 'Go to another menu, etc.')
)
To remove options use remove_options method of a Menu instance.
Run
Just use start_loop and stop_loop methods to run and stop the program.
If your graph has faults, e.g. options leading nowhere, you will see report about
these faults in the CLI.
Extensions
You can change representation of options and input method.
Render
Redefine the method render of the Menu class. It is responsible for CLI
presentation of menus.
Input for mapper
Redefine the method read_input of the Menu class. It is responsible for getting
of a new input for mapping. The returned value must be a str with id of a new
state.
Example
import sys
from FiniteConsole import Menu, Option, Program
def func():
x = float(input('Enter a number: '))
print(x*x)
main_menu = Menu('main').append_options(
Option('0', 'exit', 'Quit'), # Finite
Option('1', 'stuff_1', 'Square'), # Finite
Option('3', 'submenu', 'Go to another menu')
)
p = Program(main_menu)
Menu('submenu').append_options(Option('back', 'main', 'Go back'))
# Finite states
Menu('exit', lambda: sys.exit())
Menu('stuff_1', func)
p.start_loop()
And yes. You can change program's comportment while runtime: append other menus, options, etc.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file FiniteConsole-1.0.2.tar.gz.
File metadata
- Download URL: FiniteConsole-1.0.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
953ec23330d45d9484c22b098f3f972ac47464546ae9e52c091c6d5484bbf51d
|
|
| MD5 |
e02433b54bcdade3fe2a9c85c915f603
|
|
| BLAKE2b-256 |
f9f896875c308123934a04f2556d927ca8a4c73f5b4fe38e2368e776cb4badda
|
File details
Details for the file FiniteConsole-1.0.2-py3-none-any.whl.
File metadata
- Download URL: FiniteConsole-1.0.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bccf324bdc685b685c324bedc24fd69dc19c6ed9d0d7f64b5dee4c1cd0996848
|
|
| MD5 |
2751d87b15ae27017aa87e67b5dfabf0
|
|
| BLAKE2b-256 |
946d6687dfbcde9d157ed236798088d4202f38135d9b18f7b4568b42fb738ba9
|