程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python saves intermediate results of numpy data to local files

編輯:Python

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 :

  • If the file doesn't exist , Just keep it array Into this file
  • If the file exists , Read the contents of the file and return

Sample code

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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved