eshan6704 commited on
Commit
47cd39f
·
verified ·
1 Parent(s): 56c91fb

Update persist.py

Browse files
Files changed (1) hide show
  1. persist.py +40 -49
persist.py CHANGED
@@ -11,7 +11,7 @@ from typing import Any
11
  BASE_DIR = "./data/store"
12
  os.makedirs(BASE_DIR, exist_ok=True)
13
 
14
- # TTL validity map (centralized)
15
  VALIDITY_MAP = {
16
  "result": {"days": 7},
17
  "qresult": {"days": 7},
@@ -22,7 +22,7 @@ VALIDITY_MAP = {
22
  }
23
 
24
  # ==============================
25
- # Helper functions
26
  # ==============================
27
  def _ts():
28
  return datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
@@ -34,7 +34,6 @@ def _list_files():
34
  return os.listdir(BASE_DIR)
35
 
36
  def _latest(prefix: str, ext: str):
37
- """Return latest file with prefix + ext"""
38
  files = [
39
  f for f in _list_files()
40
  if f.startswith(prefix + "_") and f.endswith("." + ext)
@@ -51,7 +50,8 @@ def save(name: str, data: Any, ftype: str) -> bool:
51
  try:
52
  if ftype == "csv":
53
  if not isinstance(data, pd.DataFrame):
54
- raise TypeError("CSV requires pandas DataFrame")
 
55
  data.to_csv(path, index=False)
56
  elif ftype == "json":
57
  with open(path, "w", encoding="utf-8") as f:
@@ -63,72 +63,80 @@ def save(name: str, data: Any, ftype: str) -> bool:
63
  with open(path, "wb") as f:
64
  pickle.dump(data, f)
65
  else:
66
- raise ValueError(f"Unsupported file type: {ftype}")
 
 
67
  return True
68
- except Exception:
 
69
  return False
70
 
71
  # ==============================
72
  # Load function
73
  # ==============================
74
  def load(name: str, ftype: str):
75
- """
76
- Load file by:
77
- 1) full filename (name_YYYY_MM_DD_HH_MM_SS.ext)
78
- 2) base name + ftype (latest)
79
- Returns False if file not present
80
- """
81
  filename = _latest(name, ftype) if "." not in name else name
82
  path = _path(filename)
83
  if not os.path.exists(path):
 
84
  return False
85
  try:
86
  if filename.endswith(".csv"):
87
- return pd.read_csv(path)
88
- if filename.endswith(".json"):
89
  with open(path, "r", encoding="utf-8") as f:
90
- return json.load(f)
91
- if filename.endswith(".html"):
92
  with open(path, "r", encoding="utf-8") as f:
93
- return f.read()
94
- if filename.endswith(".pkl"):
95
  with open(path, "rb") as f:
96
- return pickle.load(f)
97
- return False
98
- except Exception:
 
 
 
 
 
99
  return False
100
 
101
  # ==============================
102
  # Exists with TTL
103
  # ==============================
104
  def exists(name: str, ftype: str) -> bool:
105
- """
106
- Check if file exists and is within TTL defined in VALIDITY_MAP
107
- Returns True/False
108
- """
109
  filename = _latest(name, ftype)
110
  if not filename:
 
111
  return False
112
 
113
  path = _path(filename)
114
  if not os.path.exists(path):
 
115
  return False
116
 
117
  mtime = datetime.fromtimestamp(os.path.getmtime(path))
118
  func_name = name.split("_")[0]
119
  validity = VALIDITY_MAP.get(func_name)
120
  if not validity:
121
- return True # no TTL → always valid
 
122
 
123
  now = datetime.now()
 
124
  if "minutes" in validity:
125
- return (now - mtime) <= timedelta(minutes=validity["minutes"])
126
- if "hours" in validity:
127
- return (now - mtime) <= timedelta(hours=validity["hours"])
128
- if "days" in validity:
129
- return (now - mtime) <= timedelta(days=validity["days"])
 
 
 
 
 
130
 
131
- return True
132
 
133
  # ==============================
134
  # Utilities
@@ -140,20 +148,3 @@ def list_files(name=None, ftype=None):
140
  if ftype:
141
  files = [f for f in files if f.endswith("." + ftype)]
142
  return files
143
- '''
144
- import pandas as pd
145
- from hf_persistence import save, load, exists
146
-
147
- # Save DataFrame
148
- df = pd.DataFrame({"A":[1,2], "B":[3,4]})
149
- save("intraday_RELIANCE", df, "csv")
150
-
151
- # Check existence with TTL
152
- if exists("intraday_RELIANCE", "csv"):
153
- df_cached = load("intraday_RELIANCE", "csv")
154
-
155
- # Save HTML
156
- save("intraday_RELIANCE", "<h1>Hello</h1>", "html")
157
- if exists("intraday_RELIANCE", "html"):
158
- html_cached = load("intraday_RELIANCE", "html")
159
- '''
 
11
  BASE_DIR = "./data/store"
12
  os.makedirs(BASE_DIR, exist_ok=True)
13
 
14
+ # TTL validity map
15
  VALIDITY_MAP = {
16
  "result": {"days": 7},
17
  "qresult": {"days": 7},
 
22
  }
23
 
24
  # ==============================
25
+ # Helpers
26
  # ==============================
27
  def _ts():
28
  return datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
 
34
  return os.listdir(BASE_DIR)
35
 
36
  def _latest(prefix: str, ext: str):
 
37
  files = [
38
  f for f in _list_files()
39
  if f.startswith(prefix + "_") and f.endswith("." + ext)
 
50
  try:
51
  if ftype == "csv":
52
  if not isinstance(data, pd.DataFrame):
53
+ print(f"[SAVE FAILED] CSV requires pandas DataFrame for {filename}")
54
+ return False
55
  data.to_csv(path, index=False)
56
  elif ftype == "json":
57
  with open(path, "w", encoding="utf-8") as f:
 
63
  with open(path, "wb") as f:
64
  pickle.dump(data, f)
65
  else:
66
+ print(f"[SAVE FAILED] Unsupported file type: {ftype} for {filename}")
67
+ return False
68
+ print(f"[SAVE OK] {filename}")
69
  return True
70
+ except Exception as e:
71
+ print(f"[SAVE FAILED] {filename} - Exception: {e}")
72
  return False
73
 
74
  # ==============================
75
  # Load function
76
  # ==============================
77
  def load(name: str, ftype: str):
 
 
 
 
 
 
78
  filename = _latest(name, ftype) if "." not in name else name
79
  path = _path(filename)
80
  if not os.path.exists(path):
81
+ print(f"[LOAD FAILED] File does not exist: {filename}")
82
  return False
83
  try:
84
  if filename.endswith(".csv"):
85
+ df = pd.read_csv(path)
86
+ elif filename.endswith(".json"):
87
  with open(path, "r", encoding="utf-8") as f:
88
+ df = json.load(f)
89
+ elif filename.endswith(".html"):
90
  with open(path, "r", encoding="utf-8") as f:
91
+ df = f.read()
92
+ elif filename.endswith(".pkl"):
93
  with open(path, "rb") as f:
94
+ df = pickle.load(f)
95
+ else:
96
+ print(f"[LOAD FAILED] Unsupported file type: {filename}")
97
+ return False
98
+ print(f"[LOAD OK] {filename}")
99
+ return df
100
+ except Exception as e:
101
+ print(f"[LOAD FAILED] {filename} - Exception: {e}")
102
  return False
103
 
104
  # ==============================
105
  # Exists with TTL
106
  # ==============================
107
  def exists(name: str, ftype: str) -> bool:
 
 
 
 
108
  filename = _latest(name, ftype)
109
  if not filename:
110
+ print(f"[EXISTS] No file found for {name}.{ftype}")
111
  return False
112
 
113
  path = _path(filename)
114
  if not os.path.exists(path):
115
+ print(f"[EXISTS] File not present: {filename}")
116
  return False
117
 
118
  mtime = datetime.fromtimestamp(os.path.getmtime(path))
119
  func_name = name.split("_")[0]
120
  validity = VALIDITY_MAP.get(func_name)
121
  if not validity:
122
+ print(f"[EXISTS] File exists with no TTL: {filename}")
123
+ return True
124
 
125
  now = datetime.now()
126
+ is_valid = True
127
  if "minutes" in validity:
128
+ is_valid = (now - mtime) <= timedelta(minutes=validity["minutes"])
129
+ elif "hours" in validity:
130
+ is_valid = (now - mtime) <= timedelta(hours=validity["hours"])
131
+ elif "days" in validity:
132
+ is_valid = (now - mtime) <= timedelta(days=validity["days"])
133
+
134
+ if is_valid:
135
+ print(f"[EXISTS] File exists and valid: {filename}")
136
+ else:
137
+ print(f"[EXISTS] File expired: {filename}")
138
 
139
+ return is_valid
140
 
141
  # ==============================
142
  # Utilities
 
148
  if ftype:
149
  files = [f for f in files if f.endswith("." + ftype)]
150
  return files