Coffee machine FSA
This is a small Python program simulating a coffee machine, mostly done as an experiment/code sample, with the bulk of it thrown together in about an hour and a half. The file
controller.py includes the logic for the virtual controller implemented using a few of Python's OO features.
That includes the
CoffeeMachine class as well as the encapsulated
Basket classes, object methods for changing system state, and plenty of exception handling.
cm.py file invokes an interactive REPL for interacting with the coffee machine and is mostly just imperative (besides invoking an instance of the
This script relies only on the
tqdm package, available for both Python 2 and 3 via
tqdm throws up a loading bar when filling the water tank or hopper in the REPL.
Any dependencies are available via
pip install -r requirements.txt
Running the script
The script should work with both Python2 and Python3 and should be run using
This will initialzie a REPL accepting a number of commands and subcommands.
Available commands subcommands
||none||Displays a help menu with this list of commands|
||none||Shows a list of the coffee machine's facilities and their readiness|
||Toggles the coffee machine's power|
||Fills the water tank or hopper up to 100%|
||Empties the water tank or hopper down to 0%|
||Places a clean, empty filter in the basket|
||Throws away the filter, regardless of whether new or used|
||Brews the coffee.
CM also supports command chaining using the semicolon (
;), similarly to the Python REPL. For example:
$ python cm.py ☕ > power on; fill water; replace filter; status Powered on Water tank filled Filter replaced Power: [ OK ] Water: [ OK ] Hopper: [ WARN ] Basket: [ OK ] ☕ > fill hopper; brew truck Hopper filled Pouring coffee into truck... Done pouring
Cancelling a bad command and exiting
Like the Python shell, you can cancel the command currently being entered by hitting
^C. Exiting the shell requires sending an EOF, which can be done on Unix systems using