Spaces:
Running
Running
Update persist.py
Browse files- persist.py +20 -8
persist.py
CHANGED
|
@@ -105,23 +105,34 @@ def load(name: str, ftype: str):
|
|
| 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 |
-
|
| 119 |
-
|
| 120 |
-
validity = VALIDITY_MAP.get(
|
| 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:
|
|
@@ -132,12 +143,13 @@ def exists(name: str, ftype: str) -> bool:
|
|
| 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
|
| 143 |
# ==============================
|
|
|
|
| 105 |
# Exists with TTL
|
| 106 |
# ==============================
|
| 107 |
def exists(name: str, ftype: str) -> bool:
|
| 108 |
+
"""
|
| 109 |
+
Checks if a file exists AND is valid within TTL based on parent type + symbol.
|
| 110 |
+
Example:
|
| 111 |
+
name = INTRADAY_RELIANCE
|
| 112 |
+
matches any file starting with INTRADAY_RELIANCE_YYYY_MM_DD_HH_MM_SS.csv
|
| 113 |
+
TTL applied according to parent type (INTRADAY -> 15 minutes)
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
# Pick the latest matching file for this type+symbol
|
| 117 |
filename = _latest(name, ftype)
|
| 118 |
if not filename:
|
| 119 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [EXISTS] No file found for {name}.{ftype}")
|
| 120 |
return False
|
| 121 |
|
| 122 |
path = _path(filename)
|
| 123 |
if not os.path.exists(path):
|
| 124 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [EXISTS] File not present: {filename}")
|
| 125 |
return False
|
| 126 |
|
| 127 |
+
# Extract parent type for TTL
|
| 128 |
+
parent_func = name.split("_")[0].lower() # INTRADAY, RESULT, etc.
|
| 129 |
+
validity = VALIDITY_MAP.get(parent_func)
|
| 130 |
if not validity:
|
| 131 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [EXISTS] File exists with no TTL: {filename}")
|
| 132 |
return True
|
| 133 |
|
| 134 |
+
# Check TTL
|
| 135 |
+
mtime = datetime.fromtimestamp(os.path.getmtime(path))
|
| 136 |
now = datetime.now()
|
| 137 |
is_valid = True
|
| 138 |
if "minutes" in validity:
|
|
|
|
| 143 |
is_valid = (now - mtime) <= timedelta(days=validity["days"])
|
| 144 |
|
| 145 |
if is_valid:
|
| 146 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [EXISTS] File exists and valid: {filename}")
|
| 147 |
else:
|
| 148 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [EXISTS] File expired: {filename}")
|
| 149 |
|
| 150 |
return is_valid
|
| 151 |
|
| 152 |
+
|
| 153 |
# ==============================
|
| 154 |
# Utilities
|
| 155 |
# ==============================
|