Skip to content

Instantly share code, notes, and snippets.

@tchiavegatti
Created January 31, 2020 19:25
Show Gist options
  • Save tchiavegatti/bed4c40ee641364b99489f1f77de4d2f to your computer and use it in GitHub Desktop.
Save tchiavegatti/bed4c40ee641364b99489f1f77de4d2f to your computer and use it in GitHub Desktop.
[Storage filepath] Function to set a filepath to a dataframe storage in feather, HDF5, CSV or Excel #pandas
def filepath(filetype, destination, version=None, tag=tag):
"""
Returns a filepath to for file export.
Attributes:
filetype: `str`, 'feather', 'hdf', 'csv', 'excel'
Type of the output file. Implemented to date are feather, hdf5 (h5), csv and Excel (xlsx).
destination: `str`, 'data' or 'output'.
Whether the file should be exported to the data folder or to the output folder. \
HDF5 and feather files storing temporary data can go to the data folder. \
Usually, CSV or Excel files intended to be shared go to the output folder.
version: `str`, default = None
An additional version identifier.
tag: `str`, default = tag.
The base of the file name; usually the client name. It also sets the output folder.
"""
ext_dict = {
'feather': '.feather',
'hdf': '.h5',
'csv': '.csv',
'excel': '.xlsx'
}
valid_types = ['feather', 'hdf', 'csv', 'excel']
if filetype not in valid_types:
raise NotImplementedError(f'Not implemented {filetype}. Currently valid options are {valid_types}')
valid_destinations = ['data', 'output']
if destination not in valid_destinations:
raise ValueError(f'Unknown file destination folder. Valid values for destination are {valild_destinations}.')
if destination=='data':
export_to_folder = Path.cwd().joinpath('data').joinpath(tag)
elif destination=='output':
export_to_folder = Path.cwd().joinpath('output').joinpath(tag)
filename = tag + version + ext_dict[filetype]
filepath = export_to_folder.joinpath(filename)
return filepath
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment