Since functions are first-class citizens in Python I should be able to refactor this:
def get_events():
csv_path = os.path.join(INPUT_CSV_PATH, DATA_NAME + '.csv')
print(f'Starting reading events at {datetime.now()}')
start_time = datetime.now()
events = pd.read_csv(csv_path, dtype=DTYPES)
end_time = datetime.now()
print(f'Finished reading events at {end_time} ({end_time - start_time})')
return events
To something like this:
def get_events():
csv_path = os.path.join(INPUT_CSV_PATH, DATA_NAME + '.csv')
events = _time_function_call('reading events', pd.read_csv, {'filepath_or_buffer': csv_path, 'dtype': DTYPES})
return events
def _time_function_call(message, func, *kwargs):
print(f'Starting {message} at {datetime.now()}')
start_time = datetime.now()
result = func(*kwargs)
end_time = datetime.now()
print(f'Finished {message} at {end_time} ({end_time - start_time})')
return result
I.e. pass the pandas read_csv function and its named arguments into a helper function. (N.B. I wasn't sure how to pass in the named arguments when passing a function around, this answer helped.)
But after refactoring I get the following error:
ValueError: Invalid file path or buffer object type: <class 'dict'>
What am I missing about how to pass functions and their named parameters into another Python function for evaluation?