Created
November 30, 2017 21:05
-
-
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.
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
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