Skip to content

Instantly share code, notes, and snippets.

@mitchellbusby
Created April 9, 2017 10:41
Show Gist options
  • Save mitchellbusby/df7a250b1dd5387bb33dd7033d59edf2 to your computer and use it in GitHub Desktop.
Save mitchellbusby/df7a250b1dd5387bb33dd7033d59edf2 to your computer and use it in GitHub Desktop.
machine = {
'field_1': 5,
'field_2': 10
}
def func_1(machine):
"""
Does something to the machine
"""
machine['field_1'] = 10
return machine
def func_2(machine):
"""
Does something else to the machine
"""
machine['field_2'] = 5
return machine
# This is your lookup table of inputs to functions
dict_of_funcs = {
'input_1': func_1,
'input_2': func_2
}
# Lets pretend we have a file with the following line separated inputs
# input_1
# input_2
# (this is super rough btw...don't forget to close the file etc)
for line in file('input.txt').readlines():
# Get the relevant function
func = dict_of_funcs[line]
# Apply the function
machine = func(machine)
print(machine)
# Ideally each machine should be a new instance of the object
# with no relation to the previous one - but Python doesn't really offer immutability like that
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment