Skip to content

Instantly share code, notes, and snippets.

@arlyon
Created November 30, 2017 21:05
Show Gist options
  • Save arlyon/4418720e57dfe009f6d3aa5091ad2928 to your computer and use it in GitHub Desktop.
Save arlyon/4418720e57dfe009f6d3aa5091ad2928 to your computer and use it in GitHub Desktop.
Reads data from a file and passes it as an argument to the function.
def read_data_from_file(filename, data_arg):
"""
Reads the data from a file and passes it into the
function as the kwarg defined in the data_arg parameter.
:param filename: The filename to read the data from.
:param data_arg: The argument to pass in the data as.
:return: A new with the data already passed in.
"""
def readfile_decorator(input_func):
def new_func(*args, **kwargs):
with open(filename) as f:
data = (json.loads(x) for x in f)
kwargs[data_arg] = data
return input_func(*args, **kwargs)
return new_func
return readfile_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment