eshan6704 commited on
Commit
d748bde
·
verified ·
1 Parent(s): 177d114

Create persist.py

Browse files
Files changed (1) hide show
  1. persist.py +126 -0
persist.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import pickle
4
+ import pandas as pd
5
+ from typing import Any
6
+ from datetime import datetime
7
+
8
+ # ==============================
9
+ # HF Persistent Base Directory
10
+ # ==============================
11
+ BASE_DIR = "/data/store"
12
+ os.makedirs(BASE_DIR, exist_ok=True)
13
+
14
+ # ==============================
15
+ # Helpers
16
+ # ==============================
17
+ def _ts():
18
+ return datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
19
+
20
+ def _path(name):
21
+ return os.path.join(BASE_DIR, name)
22
+
23
+ def _list_files():
24
+ return os.listdir(BASE_DIR)
25
+
26
+ def _latest(prefix, ext):
27
+ files = [
28
+ f for f in _list_files()
29
+ if f.startswith(prefix + "_") and f.endswith("." + ext)
30
+ ]
31
+ return max(files) if files else None
32
+
33
+ # ==============================
34
+ # SAVE
35
+ # ==============================
36
+ def save(name: str, data: Any, ftype: str = "csv") -> str:
37
+ ts = _ts()
38
+ filename = f"{name}_{ts}.{ftype}"
39
+ path = _path(filename)
40
+
41
+ if ftype == "csv":
42
+ if not isinstance(data, pd.DataFrame):
43
+ raise TypeError("CSV requires pandas DataFrame")
44
+ data.to_csv(path, index=False)
45
+
46
+ elif ftype == "json":
47
+ with open(path, "w", encoding="utf-8") as f:
48
+ json.dump(data, f, indent=2)
49
+
50
+ elif ftype == "html":
51
+ with open(path, "w", encoding="utf-8") as f:
52
+ f.write(str(data))
53
+
54
+ elif ftype == "pkl":
55
+ with open(path, "wb") as f:
56
+ pickle.dump(data, f)
57
+
58
+ else:
59
+ raise ValueError(f"Unsupported file type: {ftype}")
60
+
61
+ return filename
62
+
63
+ # ==============================
64
+ # LOAD (STRICT)
65
+ # ==============================
66
+ def load(name: str, ftype: str | None = None):
67
+ """
68
+ Allowed:
69
+ load("nifty_YYYY_MM_DD_HH_MM_SS.csv")
70
+ load("nifty", "csv")
71
+ """
72
+
73
+ # Case 1: full filename
74
+ if "." in name:
75
+ path = _path(name)
76
+ if not os.path.exists(path):
77
+ return None
78
+ filename = name
79
+
80
+ # Case 2: base name + type
81
+ else:
82
+ if not ftype:
83
+ raise ValueError("File type must be provided when filename is not full")
84
+ filename = _latest(name, ftype)
85
+ if not filename:
86
+ return None
87
+ path = _path(filename)
88
+
89
+ # Load by extension
90
+ if filename.endswith(".csv"):
91
+ return pd.read_csv(path)
92
+
93
+ if filename.endswith(".json"):
94
+ with open(path, "r", encoding="utf-8") as f:
95
+ return json.load(f)
96
+
97
+ if filename.endswith(".html"):
98
+ with open(path, "r", encoding="utf-8") as f:
99
+ return f.read()
100
+
101
+ if filename.endswith(".pkl"):
102
+ with open(path, "rb") as f:
103
+ return pickle.load(f)
104
+
105
+ raise ValueError(f"Unsupported file type: {filename}")
106
+
107
+ # ==============================
108
+ # UTILITIES
109
+ # ==============================
110
+ def list_files(name=None, ftype=None):
111
+ files = sorted(_list_files())
112
+ if name:
113
+ files = [f for f in files if f.startswith(name + "_")]
114
+ if ftype:
115
+ files = [f for f in files if f.endswith("." + ftype)]
116
+ return files
117
+
118
+
119
+ # SAVE
120
+ #save("nifty", df, ftype="csv")
121
+
122
+ # LOAD latest
123
+ #df_latest = load("nifty", "csv")
124
+
125
+ # LOAD specific version
126
+ #df_old = load("nifty_2025_12_18_10_30_00.csv")