A coffee machine FSA with a REPL
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


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 ResourceStorage and Basket classes, object methods for changing system state, and plenty of exception handling.

The cm.py file invokes an interactive REPL for interacting with the coffee machine and is mostly just imperative (besides invoking an instance of the CoffeeMachine class.


This script relies only on the tqdm package, available for both Python 2 and 3 via pip/pip3. 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

python cm.py

This will initialzie a REPL accepting a number of commands and subcommands.

Available commands subcommands
Command Subcommand Description
help none Displays a help menu with this list of commands
status none Shows a list of the coffee machine's facilities and their readiness
power on|off Toggles the coffee machine's power
fill water|hopper Fills the water tank or hopper up to 100%
empty water|hopper Empties the water tank or hopper down to 0%
replace filter Places a clean, empty filter in the basket
throw filter Throws away the filter, regardless of whether new or used
brew [container] Brews the coffee. container is optional and arbitrary, and may be multiple words. Defaults to cup.
Command chaining

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 ^D.