1010import copy
1111import json
1212
13+ import filelock
14+ import contextlib
15+
1316from pathlib import Path
1417
1518class H5 ():
@@ -27,21 +30,34 @@ def __init__(self, *data):
2730 for i in data :
2831 self .root .update (copy .deepcopy (i ))
2932
30- def load (self , paths = None , update = False ):
33+ def load (self , paths = None , update = False , lock = False ):
3134 if self .filename is not None :
32- with h5py .File (self .filename , 'r' ) as h5file :
33- data = Dict (recursively_load (h5file , '/' , self .inverse_transform , paths ))
34- if update :
35- self .root .update (data )
36- else :
37- self .root = data
35+
36+ if lock is True :
37+ lock_file = filelock .FileLock (self .filename + '.lock' )
38+ else :
39+ lock_file = contextlib .nullcontext ()
40+
41+ with lock_file :
42+ with h5py .File (self .filename , 'r' ) as h5file :
43+ data = Dict (recursively_load (h5file , '/' , self .inverse_transform , paths ))
44+ if update :
45+ self .root .update (data )
46+ else :
47+ self .root = data
3848 else :
3949 print ('Filename must be set before load can be used' )
4050
41- def save (self ):
51+ def save (self , lock = False ):
4252 if self .filename is not None :
43- with h5py .File (self .filename , 'w' ) as h5file :
44- recursively_save (h5file , '/' , self .root , self .transform )
53+ if lock is True :
54+ lock_file = filelock .FileLock (self .filename + '.lock' )
55+ else :
56+ lock_file = contextlib .nullcontext ()
57+
58+ with lock_file :
59+ with h5py .File (self .filename , 'w' ) as h5file :
60+ recursively_save (h5file , '/' , self .root , self .transform )
4561 else :
4662 print ("Filename must be set before save can be used" )
4763
@@ -59,11 +75,17 @@ def load_json(self, filename, update=False):
5975 else :
6076 self .root = data
6177
62- def append (self ):
78+ def append (self , lock = False ):
6379 "This can only be used to write new keys to the system, this is faster than having to read the data before writing it"
6480 if self .filename is not None :
65- with h5py .File (self .filename , 'a' ) as h5file :
66- recursively_save (h5file , '/' , self .root , self .transform )
81+ if lock is True :
82+ lock_file = filelock .FileLock (self .filename + '.lock' )
83+ else :
84+ lock_file = contextlib .nullcontext ()
85+
86+ with lock_file :
87+ with h5py .File (self .filename , 'a' ) as h5file :
88+ recursively_save (h5file , '/' , self .root , self .transform )
6789 else :
6890 print ("Filename must be set before save can be used" )
6991
0 commit comments