Last active
December 7, 2017 00:28
-
-
Save anthonyjb/068ac386b3986bf1d996c220e44c3c20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prerequisites: | |
# | |
# - Using Python 3.5+ | |
# - Install manhattan-chains (`pip install manhattan-chains`) | |
# | |
from manhattan.chains import Chain | |
# Define the chains | |
# Show the list | |
show = Chain([ | |
'get_list', | |
'render_list' | |
]) | |
@show.link | |
def get_list(state): | |
if state.args[0] not in state.lists: | |
return 'List not found: ' + state.args[0] | |
state.list_name = state.args[0] | |
state.list = state.lists[state.list_name] | |
@show.link | |
def render_list(state): | |
lines = [state.list_name, '-' * len(state.list_name)] | |
lines += ['- ' + item for item in state.list] | |
return '\n' + '\n'.join(lines) + '\n' | |
# Add an item to the list | |
add = Chain([ | |
'get_list', | |
'add_item', | |
'render_list' | |
]) | |
add.set_link(get_list) | |
add.set_link(render_list) | |
@add.link | |
def add_item(state): | |
state.list.append(state.args[1]) | |
# Define a table of lists | |
lists = { | |
'shopping': ['milk', 'eggs'], | |
'todo': ['take dog to groomers'] | |
} | |
# Define a table of actions the user can perform | |
actions = { | |
'add': add, | |
'show': show | |
} | |
# Parse the users commands | |
while True: | |
user_input = input('? ') | |
[action, *args] = user_input.split(' ') | |
if action == 'quit': | |
break | |
elif action in actions: | |
output = actions[action](lists=lists, args=args) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment