You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.7 KiB
82 lines
1.7 KiB
"""
|
|
Data reading and writing.
|
|
|
|
Authors
|
|
* qingenz123@126.com (Qingen ZHAO) 2022
|
|
|
|
"""
|
|
|
|
import os
|
|
import pickle
|
|
|
|
def save_pkl(obj, file):
|
|
"""Save an object in pkl format.
|
|
|
|
Arguments
|
|
---------
|
|
obj : object
|
|
Object to save in pkl format
|
|
file : str
|
|
Path to the output file
|
|
sampling_rate : int
|
|
Sampling rate of the audio file, TODO: this is not used?
|
|
|
|
Example
|
|
-------
|
|
>>> tmpfile = os.path.join(getfixture('tmpdir'), "example.pkl")
|
|
>>> save_pkl([1, 2, 3, 4, 5], tmpfile)
|
|
>>> load_pkl(tmpfile)
|
|
[1, 2, 3, 4, 5]
|
|
"""
|
|
with open(file, "wb") as f:
|
|
pickle.dump(obj, f)
|
|
|
|
def load_pickle(pickle_path):
|
|
"""Utility function for loading .pkl pickle files.
|
|
|
|
Arguments
|
|
---------
|
|
pickle_path : str
|
|
Path to pickle file.
|
|
|
|
Returns
|
|
-------
|
|
out : object
|
|
Python object loaded from pickle.
|
|
"""
|
|
with open(pickle_path, "rb") as f:
|
|
out = pickle.load(f)
|
|
return out
|
|
|
|
def load_pkl(file):
|
|
"""Loads a pkl file.
|
|
|
|
For an example, see `save_pkl`.
|
|
|
|
Arguments
|
|
---------
|
|
file : str
|
|
Path to the input pkl file.
|
|
|
|
Returns
|
|
-------
|
|
The loaded object.
|
|
"""
|
|
|
|
# Deals with the situation where two processes are trying
|
|
# to access the same label dictionary by creating a lock
|
|
count = 100
|
|
while count > 0:
|
|
if os.path.isfile(file + ".lock"):
|
|
time.sleep(1)
|
|
count -= 1
|
|
else:
|
|
break
|
|
|
|
try:
|
|
open(file + ".lock", "w").close()
|
|
with open(file, "rb") as f:
|
|
return pickle.load(f)
|
|
finally:
|
|
if os.path.isfile(file + ".lock"):
|
|
os.remove(file + ".lock") |