Last active
December 7, 2017 00:29
-
-
Save anthonyjb/ea7c3df8c449c49d206fbdae985bfee7 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' | |
# Define a table of lists | |
lists = { | |
'shopping': ['milk', 'eggs'], | |
'todo': ['take dog to groomers'] | |
} | |
# Define a table of actions the user can perform | |
actions = { | |
'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