Skip to main content

Save your files with current date time appended; load the newest back without specifying file name everytime

Project description

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, like sldt.s(variable, 'filename.csv') will save variable to a file "filename_1912181212.csv". Everytime you want to load it back, use sldt.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!


pip install sldt

Supported File Types

It supports only 4 common file types now showed below. Basically it just calls like pd.DataFrame.to_csv() as you already familiar.

import sldt
['.pkl', '.csv', '.png', '.txt']


# launch logger to see info when saving and loading files
import logging

anything -> pkl

a_list = ['a', 0.1, False]

# save two files for demo
sldt.s(a_list, 'output/a_list.pkl')
import time
sldt.s(a_list, 'output/a_list.pkl')

# it will load the newest (which is the later one) back
a_list = sldt.l('output/a_list.pkl')
INFO:root:output/a_list_1912240321.pkl saved
INFO:root:output/a_list_1912240322.pkl saved
INFO:root:output/a_list_1912240322.pkl loaded

['a', 0.1, False]

pandas dataframe -> csv

import pandas as pd
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
sldt.s(df, 'output/df.csv')

df = sldt.l('output/df.csv')
INFO:root:output/df_1912240322.csv saved
INFO:root:output/df_1912240322.csv loaded
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;

.dataframe thead th {
    text-align: right;
col1 col2
0 1 3
1 2 4

you 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 → png

import seaborn as sns
exercise = 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_1912240322.png saved

Display it using ![](output/g_1912240037.png) in markdown.

the default arguments will be dpi=600, bbox_inches='tight'. And it will try to close the fig after saving the file.

string → txt

text = '''some random sentences
and there'''
sldt.s(text, 'output/text.txt')

text = sldt.l('output/text.txt')
INFO:root:output/text_1912240322.txt saved
INFO:root:output/text_1912240322.txt loaded

'some random sentences\nhere\nand there'

helper functions

you can save your own file type and load it back using append_dt and find_newest

import pickle

output_filename = sldt.append_dt('output/sth.pkl', datetime_format="%y%m%d%H%M")[0]
with open(output_filename, 'wb') as f:
    pickle.dump('some strings', f)
newest_file = sldt.find_newest('output/sth.pkl')[0]
with open(newest_file, 'rb') as f:
    sth = pickle.load(f)
'some strings'

find_newest returns a tuplet (filename, extension)

('output/text_1912240322.txt', '.txt')

It can be served as checking whether file is exist

    print('file exist')
    print('no file exist')
file exist

Similarly, load only if you haven't save the needed file

    # do some calculation
    result = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
    # save calculation result
    sldt.s(result, 'output/result.csv')
INFO:root:output/result_1912240322.csv saved

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

sldt-1.0.1-py3-none-any.whl (5.2 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page