This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
54 lines (47 loc) · 1.57 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
import os
from os import path
import pickle
# Relative path of pickle cache from working directory
PICKLE_DIR = "pickles/"
def savePickle(object, name):
"""
Caches object as [name][epoch time].pkl
:param object: Object to cache
:param name: name in cache
:return: None
"""
epoch = time.time()
filename = name + str(epoch) + ".pkl" # Save name
fullPath = path.join(PICKLE_DIR, filename) # Save path
# Get permissions and save the file
with open(fullPath, "w") as outfile:
pickle.dump(object, outfile)
def getMostRecentPickle(name):
"""
Finds the most recent pickle in the cache with the given name
:param name: Savename of pickle (without epoch time)
:return: Object in cache on success, None on miss
"""
# List the directory
fileNames = [f for f in os.listdir(PICKLE_DIR) if name in f]
# If the directory is not empty...
if len(fileNames) != 0:
# Sort in descending order by epoch time
fileNames.sort()
fileNames.reverse()
# Open, load, and return pickle
with open(path.join(PICKLE_DIR, fileNames[0]), "r") as pickleFile:
return pickle.load(pickleFile)
# Return None on cache miss
else:
return None
def pickleExists(name):
"""
Returns True if there is a pickle with name in cache, False otherwise. Used to prevent
cache misses
:param name: Name to look for in cache
:return: True on hit, False on miss
"""
fileNames = [f for f in os.listdir(PICKLE_DIR) if name in f]
return not len(fileNames) == 0