Skip to content

Commit

Permalink
depend on filelock
Browse files Browse the repository at this point in the history
added lock support
bumped version number
  • Loading branch information
Immudzen committed Jan 4, 2021
1 parent 8c3cdd1 commit 0a38fae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cadet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = 'CADET-python'

__version__ = 0.7
__version__ = 0.8

from .cadet import H5
from .cadet import Cadet
48 changes: 35 additions & 13 deletions cadet/cadet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import copy
import json

import filelock
import contextlib

from pathlib import Path

class H5():
Expand All @@ -27,21 +30,34 @@ def __init__(self, *data):
for i in data:
self.root.update(copy.deepcopy(i))

def load(self, paths=None, update=False):
def load(self, paths=None, update=False, lock=False):
if self.filename is not None:
with h5py.File(self.filename, 'r') as h5file:
data = Dict(recursively_load(h5file, '/', self.inverse_transform, paths))
if update:
self.root.update(data)
else:
self.root = data

if lock is True:
lock_file = filelock.FileLock(self.filename + '.lock')
else:
lock_file = contextlib.nullcontext()

with lock_file:
with h5py.File(self.filename, 'r') as h5file:
data = Dict(recursively_load(h5file, '/', self.inverse_transform, paths))
if update:
self.root.update(data)
else:
self.root = data
else:
print('Filename must be set before load can be used')

def save(self):
def save(self, lock=False):
if self.filename is not None:
with h5py.File(self.filename, 'w') as h5file:
recursively_save(h5file, '/', self.root, self.transform)
if lock is True:
lock_file = filelock.FileLock(self.filename + '.lock')
else:
lock_file = contextlib.nullcontext()

with lock_file:
with h5py.File(self.filename, 'w') as h5file:
recursively_save(h5file, '/', self.root, self.transform)
else:
print("Filename must be set before save can be used")

Expand All @@ -59,11 +75,17 @@ def load_json(self, filename, update=False):
else:
self.root = data

def append(self):
def append(self, lock=False):
"This can only be used to write new keys to the system, this is faster than having to read the data before writing it"
if self.filename is not None:
with h5py.File(self.filename, 'a') as h5file:
recursively_save(h5file, '/', self.root, self.transform)
if lock is True:
lock_file = filelock.FileLock(self.filename + '.lock')
else:
lock_file = contextlib.nullcontext()

with lock_file:
with h5py.File(self.filename, 'a') as h5file:
recursively_save(h5file, '/', self.root, self.transform)
else:
print("Filename must be set before save can be used")

Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="CADET",
version="0.7",
version="0.8",
author="William Heymann",
author_email="[email protected]",
description="CADET is a python interface to the CADET chromatography simulator",
Expand All @@ -16,12 +16,13 @@
install_requires=[
'addict',
'numpy',
'h5py'
'h5py',
'filelock'
],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
python_requires='>=3.7',
)

0 comments on commit 0a38fae

Please sign in to comment.