Spaces:
Sleeping
Sleeping
Update persist.py
Browse files- persist.py +69 -67
persist.py
CHANGED
|
@@ -2,28 +2,39 @@ 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 |
-
#
|
| 10 |
# ==============================
|
| 11 |
-
BASE_DIR = "
|
| 12 |
os.makedirs(BASE_DIR, exist_ok=True)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ==============================
|
| 15 |
-
#
|
| 16 |
# ==============================
|
| 17 |
def _ts():
|
| 18 |
return datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
|
| 19 |
|
| 20 |
-
def _path(
|
| 21 |
-
return os.path.join(BASE_DIR,
|
| 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)
|
|
@@ -31,111 +42,96 @@ def _latest(prefix, ext):
|
|
| 31 |
return max(files) if files else None
|
| 32 |
|
| 33 |
# ==============================
|
| 34 |
-
#
|
| 35 |
# ==============================
|
| 36 |
-
def save(name: str, data: Any, ftype: str
|
| 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 |
-
#
|
| 69 |
# ==============================
|
| 70 |
-
def load(name: str, ftype: str
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 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 |
-
#
|
| 116 |
# ==============================
|
| 117 |
-
def exists(name: str, ftype: str
|
| 118 |
"""
|
| 119 |
-
|
| 120 |
-
False
|
| 121 |
-
|
| 122 |
-
Allowed:
|
| 123 |
-
exists("nifty_YYYY_MM_DD_HH_MM_SS.csv")
|
| 124 |
-
exists("nifty", "csv")
|
| 125 |
"""
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
if
|
| 129 |
-
return os.path.exists(_path(name))
|
| 130 |
-
|
| 131 |
-
# Base name + type
|
| 132 |
-
if not ftype:
|
| 133 |
return False
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
# ==============================
|
| 138 |
-
#
|
| 139 |
# ==============================
|
| 140 |
def list_files(name=None, ftype=None):
|
| 141 |
files = sorted(_list_files())
|
|
@@ -144,14 +140,20 @@ def list_files(name=None, ftype=None):
|
|
| 144 |
if ftype:
|
| 145 |
files = [f for f in files if f.endswith("." + ftype)]
|
| 146 |
return files
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import pickle
|
| 4 |
import pandas as pd
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
from typing import Any
|
|
|
|
| 7 |
|
| 8 |
# ==============================
|
| 9 |
+
# Configuration
|
| 10 |
# ==============================
|
| 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},
|
| 18 |
+
"bhav": {"days": 1},
|
| 19 |
+
"intraday": {"minutes": 15},
|
| 20 |
+
"eq": {"hours": 1},
|
| 21 |
+
"daily": {"days": 1},
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
# ==============================
|
| 25 |
+
# Helper functions
|
| 26 |
# ==============================
|
| 27 |
def _ts():
|
| 28 |
return datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
|
| 29 |
|
| 30 |
+
def _path(filename: str):
|
| 31 |
+
return os.path.join(BASE_DIR, filename)
|
| 32 |
|
| 33 |
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)
|
|
|
|
| 42 |
return max(files) if files else None
|
| 43 |
|
| 44 |
# ==============================
|
| 45 |
+
# Save function
|
| 46 |
# ==============================
|
| 47 |
+
def save(name: str, data: Any, ftype: str) -> bool:
|
| 48 |
ts = _ts()
|
| 49 |
filename = f"{name}_{ts}.{ftype}"
|
| 50 |
path = _path(filename)
|
|
|
|
| 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:
|
| 58 |
json.dump(data, f, indent=2)
|
|
|
|
| 59 |
elif ftype == "html":
|
| 60 |
with open(path, "w", encoding="utf-8") as f:
|
| 61 |
f.write(str(data))
|
|
|
|
| 62 |
elif ftype == "pkl":
|
| 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
|
| 135 |
# ==============================
|
| 136 |
def list_files(name=None, ftype=None):
|
| 137 |
files = sorted(_list_files())
|
|
|
|
| 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 |
+
'''
|