Package: sldt
This is a simple script that has only two functions for one purpose. It allows you to save files with date time appended behind the filename but in front of the extension, likesldt.s(variable, 'filename.csv')will savevariableto a file "filename_1912181212.csv". Everytime you want to load it back, usesldt.l('filename.csv'), it will automatically find the newest file.This is useful when you want to save some intermediate result during running your code. But be aware the saved files will keep growing everytime you run.Feel free to download/edit this script, and maybe share your version with me!Installationpip install sldtSupported File TypesIt supports only 4 common file types now showed below. Basically it just calls likepd.DataFrame.to_csv()as you already familiar.importsldtsldt.__version__'1.0.2'sldt.SUPPORTED_EXT['.pkl', '.csv', '.png', '.txt']Demo# launch logger to see info when saving and loading filesimportlogginglogging.getLogger().setLevel(logging.INFO)anything -> pkla_list=['a',0.1,False]# savesldt.s(a_list,'output/a_list.pkl')# save the second file for demoimporttimetime.sleep(60)a_list=['b',0.2,True]sldt.s(a_list,'output/a_list.pkl')# load the newest (which is the later one) backa_list=sldt.l('output/a_list.pkl')a_listINFO:root:output/a_list_1912240334.pkl saved
INFO:root:output/a_list_1912240335.pkl saved
INFO:root:output/a_list_1912240335.pkl loaded





['b', 0.2, True]pandas dataframe -> csvimportpandasaspddf=pd.DataFrame({'col1':[1,2],'col2':[3,4]})sldt.s(df,'output/df.csv')df=sldt.l('output/df.csv')dfINFO:root:output/df_1912240335.csv saved
INFO:root:output/df_1912240335.csv loadedcol1col2013124you can pass any argument as to `pd.DataFrame.to_csv()`, if there's no arguments in `save_dt`, the default will be `index=False`.For example,save_dt(df, 'output/df.csv', sep=';')figure -> pngimportseabornassnsexercise=sns.load_dataset("exercise")g=sns.catplot(x="time",y="pulse",hue="kind",data=exercise)sldt.s(g,'output/g.png')INFO:root:output/g_1912240335.png savedDisplay it using![](output/g_1912240037.png)in markdown.the default arguments will bedpi=600, bbox_inches='tight'. And it will try to close the fig after saving the file.string -> txttext='''some random sentenceshereand there'''sldt.s(text,'output/text.txt')text=sldt.l('output/text.txt')textINFO:root:output/text_1912240335.txt saved
INFO:root:output/text_1912240335.txt loaded





'some random sentences\nhere\nand there'helper functionsyou can save your own file type and load it back usingappend_dtandfind_newestimportpickleoutput_filename=sldt.append_dt('output/sth.pkl',datetime_format="%y%m%d%H%M")[0]withopen(output_filename,'wb')asf:pickle.dump('some strings',f)newest_file=sldt.find_newest('output/sth.pkl')[0]withopen(newest_file,'rb')asf:sth=pickle.load(f)sth'some strings'find_newestreturns a tuplet (filename, extension)sldt.find_newest('output/text.txt')('output/text_1912240335.txt', '.txt')It can be served as checking whether file is existtry:sldt.find_newest('output/text.txt')print('file exist')except:print('no file exist')file existSimilarly, load only if you haven't save the needed filetry:sldt.l('output/result.csv')except:# do some calculationresult=pd.DataFrame({'col1':[1,2],'col2':[3,4]})# save calculation resultsldt.s(result,'output/result.csv')INFO:root:output/result_1912240335.csv saved