Spaces:
Running
Running
lukestanley
commited on
Commit
·
c4d55d5
1
Parent(s):
ac30aa7
remove unused helpers
Browse files
learn.py
CHANGED
@@ -64,38 +64,6 @@ def handle_cache(prefix, func, *args, _result=None, **kwargs):
|
|
64 |
return _result
|
65 |
|
66 |
|
67 |
-
def acache(prefix):
|
68 |
-
def decorator(func):
|
69 |
-
@wraps(func)
|
70 |
-
async def wrapper(*args, **kwargs):
|
71 |
-
# Generate a key based on function name and arguments
|
72 |
-
key = f"{func.__name__}_{args}_{kwargs}"
|
73 |
-
hashed_key = hashlib.sha1(key.encode()).hexdigest()
|
74 |
-
cache_filename = f"{prefix}_{hashed_key}.json"
|
75 |
-
|
76 |
-
# Check the in-memory cache first
|
77 |
-
if key in _in_memory_cache:
|
78 |
-
return _in_memory_cache[key]
|
79 |
-
|
80 |
-
# Check if cache file exists and read data
|
81 |
-
if os.path.exists(cache_filename):
|
82 |
-
with open(cache_filename, 'r') as file:
|
83 |
-
_in_memory_cache[key] = json.load(file)
|
84 |
-
return _in_memory_cache[key]
|
85 |
-
|
86 |
-
# Await the function call and get the result
|
87 |
-
print("Computing result for async function")
|
88 |
-
result = await func(*args, **kwargs)
|
89 |
-
|
90 |
-
# Update the in-memory cache and write it to the file
|
91 |
-
_in_memory_cache[key] = result
|
92 |
-
with open(cache_filename, 'w') as file:
|
93 |
-
json.dump(result, file)
|
94 |
-
|
95 |
-
return result
|
96 |
-
|
97 |
-
return wrapper
|
98 |
-
return decorator
|
99 |
|
100 |
|
101 |
def cache(prefix):
|
@@ -107,27 +75,6 @@ def cache(prefix):
|
|
107 |
return wrapper
|
108 |
return decorator
|
109 |
|
110 |
-
def timeit(func):
|
111 |
-
@wraps(func)
|
112 |
-
async def async_wrapper(*args, **kwargs):
|
113 |
-
start_time = time.time()
|
114 |
-
result = await func(*args, **kwargs) # Awaiting the async function
|
115 |
-
end_time = time.time()
|
116 |
-
print(f"{func.__name__} took {end_time - start_time:.1f} seconds to run.")
|
117 |
-
return result
|
118 |
-
|
119 |
-
@wraps(func)
|
120 |
-
def sync_wrapper(*args, **kwargs):
|
121 |
-
start_time = time.time()
|
122 |
-
result = func(*args, **kwargs) # Calling the sync function
|
123 |
-
end_time = time.time()
|
124 |
-
print(f"{func.__name__} took {end_time - start_time:.1f} seconds to run.")
|
125 |
-
return result
|
126 |
-
|
127 |
-
if asyncio.iscoroutinefunction(func):
|
128 |
-
return async_wrapper
|
129 |
-
else:
|
130 |
-
return sync_wrapper
|
131 |
|
132 |
|
133 |
|
|
|
64 |
return _result
|
65 |
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
def cache(prefix):
|
|
|
75 |
return wrapper
|
76 |
return decorator
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
|
80 |
|