When we save some intermediate results of data , It's often troublesome , Write a tool function here :
def get_data(mark: str = None, array: np.ndarray = None) -> np.ndarray:
""" Used to record some numpy The data of Pass a mark Used to identify the meaning of data , And one more array The data of If mark The data represented does not exist , Just keep it array To local file If mark The data represented exists , Read the corresponding local file and return """
assert mark is not None
os.chdir(os.getcwd())
hash_file = hashlib.md5(mark.encode()).hexdigest() + ".pkl"
if os.path.exists(hash_file):
with open(hash_file, 'rb') as file:
array = pickle.load(file)
print(' Read local file ')
else:
assert array is not None
with open(hash_file, 'wb') as file:
pickle.dump(array, file)
print(' Save data to local file ')
return array
When we use mark
When representing a data , Would be right mark String use MD5 encryption , Get the encrypted string , Then take this as the file name :
array
Into this file import numpy as np
import pickle
import os
import hashlib
def get_data(mark: str = None, array: np.ndarray = None) -> np.ndarray:
""" Used to record some numpy The data of Pass a mark Used to identify the meaning of data , And one more array The data of If mark The data represented does not exist , Just keep it array To local file If mark The data represented exists , Read the corresponding local file and return """
assert mark is not None
os.chdir(os.getcwd())
hash_file = hashlib.md5(mark.encode()).hexdigest() + ".pkl"
if os.path.exists(hash_file):
with open(hash_file, 'rb') as file:
array = pickle.load(file)
print(' Read local file ')
else:
assert array is not None
with open(hash_file, 'wb') as file:
pickle.dump(array, file)
print(' Save data to local file ')
return array
def main():
array = np.array(np.random.random(size=(4, 5, 6, 7)))
hash_value = get_data(mark=" Data identification ", array=array)
if __name__ == '__main__':
main()