Skip to content

Commit

Permalink
cache pickling support for reactions added.
Browse files Browse the repository at this point in the history
  • Loading branch information
stsouko committed Mar 8, 2021
1 parent c4a517b commit 42417ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CGRtools/containers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ def __setstate__(self, state):
self._parsed_mapping = state['parsed_mapping']
self.__meta = state['meta']
self.__name = state.get('name', '') # 4.0.9 compatibility
if 'cache' in state:
if 'cache' in state: # >= 4.1.15
self.__dict__.update(state['cache'])

@classmethod
def pickle_save_cache(cls, arg: bool):
"""
Store cache of Graph into pickle for speedup loading
"""
cls.__class_cache__['save_cache'] = arg

def __len__(self):
Expand Down
30 changes: 25 additions & 5 deletions CGRtools/containers/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ def __getitem__(self, item):
raise KeyError('invalid attribute')

def __getstate__(self):
return dict(reactants=self.__reactants, products=self.__products, reagents=self.__reagents, meta=self.__meta,
name=self.__name)
state = {'reactants': self.__reactants, 'products': self.__products, 'reagents': self.__reagents,
'meta': self.__meta, 'name': self.__name, 'arrow': self._arrow, 'signs': self._signs}

if MoleculeContainer.__class_cache__.get('save_cache', False):
state['cache'] = self.__dict__
return state

def __setstate__(self, state):
if next(iter(state)) == 'reagents': # 3.0 compatibility
Expand All @@ -129,8 +133,21 @@ def __setstate__(self, state):
self.__reagents = state['reagents']
self.__meta = state['meta']
self.__name = state.get('name', '') # 4.0.9 compatibility
self._arrow = None
self._signs = None
if 'signs' in state: # >= 4.1.15
self._arrow = state['arrow']
self._signs = state['signs']
else:
self._arrow = None
self._signs = None
if 'cache' in state: # >= 4.1.15
self.__dict__.update(state['cache'])

@classmethod
def pickle_save_cache(cls, arg: bool):
"""
Store cache of reaction into pickle for speedup loading
"""
MoleculeContainer.__class_cache__['save_cache'] = arg

@property
def reactants(self) -> Tuple[graphs, ...]:
Expand Down Expand Up @@ -225,7 +242,10 @@ def __bytes__(self):
return sha512(str(self).encode()).digest()

def __bool__(self):
return bool(self.__reactants or self.__products or self.__reagents)
"""
Exists both reactants and products
"""
return bool(self.__reactants and self.__products)

@cached_method
def __str__(self):
Expand Down

0 comments on commit 42417ad

Please sign in to comment.