Skip to content

Commit 0a38fae

Browse files
committed
depend on filelock
added lock support bumped version number
1 parent 8c3cdd1 commit 0a38fae

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

cadet/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = 'CADET-python'
22

3-
__version__ = 0.7
3+
__version__ = 0.8
44

55
from .cadet import H5
66
from .cadet import Cadet

cadet/cadet.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import copy
1111
import json
1212

13+
import filelock
14+
import contextlib
15+
1316
from pathlib import Path
1417

1518
class 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

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="CADET",
8-
version="0.7",
8+
version="0.8",
99
author="William Heymann",
1010
author_email="[email protected]",
1111
description="CADET is a python interface to the CADET chromatography simulator",
@@ -16,12 +16,13 @@
1616
install_requires=[
1717
'addict',
1818
'numpy',
19-
'h5py'
19+
'h5py',
20+
'filelock'
2021
],
2122
classifiers=[
2223
"Programming Language :: Python :: 3",
2324
"License :: OSI Approved :: BSD License",
2425
"Operating System :: OS Independent",
2526
],
26-
python_requires='>=3.6',
27+
python_requires='>=3.7',
2728
)

0 commit comments

Comments
 (0)