Skip to content

Instantly share code, notes, and snippets.

@anthonyjb
Last active December 7, 2017 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyjb/ea7c3df8c449c49d206fbdae985bfee7 to your computer and use it in GitHub Desktop.
Save anthonyjb/ea7c3df8c449c49d206fbdae985bfee7 to your computer and use it in GitHub Desktop.
# 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