eshan6704 commited on
Commit
21b9618
·
verified ·
1 Parent(s): d748bde

Update persist.py

Browse files
Files changed (1) hide show
  1. persist.py +76 -45
persist.py CHANGED
@@ -33,32 +33,36 @@ def _latest(prefix, ext):
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)
@@ -70,39 +74,65 @@ def load(name: str, ftype: str | None = None):
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
@@ -114,13 +144,14 @@ def list_files(name=None, ftype=None):
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")
 
 
33
  # ==============================
34
  # SAVE
35
  # ==============================
36
+ def save(name: str, data: Any, ftype: str = "csv") -> str | bool:
37
  ts = _ts()
38
  filename = f"{name}_{ts}.{ftype}"
39
  path = _path(filename)
40
 
41
+ try:
42
+ if ftype == "csv":
43
+ if not isinstance(data, pd.DataFrame):
44
+ raise TypeError("CSV requires pandas DataFrame")
45
+ data.to_csv(path, index=False)
46
 
47
+ elif ftype == "json":
48
+ with open(path, "w", encoding="utf-8") as f:
49
+ json.dump(data, f, indent=2)
50
 
51
+ elif ftype == "html":
52
+ with open(path, "w", encoding="utf-8") as f:
53
+ f.write(str(data))
54
 
55
+ elif ftype == "pkl":
56
+ with open(path, "wb") as f:
57
+ pickle.dump(data, f)
58
 
59
+ else:
60
+ raise ValueError("Unsupported file type")
61
+
62
+ return filename
63
 
64
+ except Exception:
65
+ return False
66
 
67
  # ==============================
68
  # LOAD (STRICT)
 
74
  load("nifty", "csv")
75
  """
76
 
77
+ # Full filename
78
  if "." in name:
 
 
 
79
  filename = name
80
 
81
+ # Base name + type
82
  else:
83
  if not ftype:
84
+ return False
85
  filename = _latest(name, ftype)
86
  if not filename:
87
+ return False
88
+
89
+ path = _path(filename)
90
+ if not os.path.exists(path):
91
+ return False
92
+
93
+ try:
94
+ if filename.endswith(".csv"):
95
+ return pd.read_csv(path)
96
+
97
+ if filename.endswith(".json"):
98
+ with open(path, "r", encoding="utf-8") as f:
99
+ return json.load(f)
100
+
101
+ if filename.endswith(".html"):
102
+ with open(path, "r", encoding="utf-8") as f:
103
+ return f.read()
104
+
105
+ if filename.endswith(".pkl"):
106
+ with open(path, "rb") as f:
107
+ return pickle.load(f)
108
+
109
+ return False
110
 
111
+ except Exception:
112
+ return False
 
113
 
114
+ # ==============================
115
+ # EXISTS (NEW FUNCTION)
116
+ # ==============================
117
+ def exists(name: str, ftype: str | None = None) -> bool:
118
+ """
119
+ True -> file exists
120
+ False -> file not present
121
 
122
+ Allowed:
123
+ exists("nifty_YYYY_MM_DD_HH_MM_SS.csv")
124
+ exists("nifty", "csv")
125
+ """
126
 
127
+ # Full filename
128
+ if "." in name:
129
+ return os.path.exists(_path(name))
130
+
131
+ # Base name + type
132
+ if not ftype:
133
+ return False
134
 
135
+ return _latest(name, ftype) is not None
136
 
137
  # ==============================
138
  # UTILITIES
 
144
  if ftype:
145
  files = [f for f in files if f.endswith("." + ftype)]
146
  return files
147
+
 
148
  # SAVE
149
+ #save("nifty", df, "csv")
150
 
151
+ # CHECK existence (NO load)
152
+ #if exists("nifty", "csv"):
153
+ #df = load("nifty", "csv")
154
 
155
+ # CHECK specific file
156
+ #if exists("nifty_2025_12_18_10_30_00.csv"):
157
+ #df_old = load("nifty_2025_12_18_10_30_00.csv")