Skip to content

Commit

Permalink
Added __reduce__() and __setstate__() methods to database classes.
Browse files Browse the repository at this point in the history
These methods are needed in order to be able to properly pickle and unpickle
the database. Note that these classes - ThermoDatabase, KineticsDatabase,
and StatmechDatabase - need to be new-style classes (i.e. derived from
object) for __reduce__() and __setstate__() to be used by the pickler.

Closes #103.
  • Loading branch information
jwallen committed Nov 29, 2012
1 parent fd20c2f commit 53cc496
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
21 changes: 20 additions & 1 deletion rmgpy/data/kinetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3417,7 +3417,7 @@ def filterReactions(reactants, products, reactionList):
return reactions

###########
class KineticsDatabase:
class KineticsDatabase(object):
"""
A class for working with the RMG kinetics database.
"""
Expand All @@ -3441,6 +3441,25 @@ def __init__(self):
}
self.global_context = {}

def __reduce__(self):
"""
A helper function used when pickling a KineticsDatabase object.
"""
d = {
'families': self.families,
'libraries': self.libraries,
'libraryOrder': self.libraryOrder,
}
return (KineticsDatabase, (), d)

def __setstate__(self, d):
"""
A helper function used when unpickling a KineticsDatabase object.
"""
self.families = d['families']
self.libraries = d['libraries']
self.libraryOrder = d['libraryOrder']

def load(self, path, families=None, libraries=None, depositories=None):
"""
Load the kinetics database from the given `path` on disk, where `path`
Expand Down
23 changes: 22 additions & 1 deletion rmgpy/data/statmech.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def getStatmechData(self, molecule, thermoModel):

################################################################################

class StatmechDatabase:
class StatmechDatabase(object):
"""
A class for working with the RMG statistical mechanics (frequencies) database.
"""
Expand All @@ -441,6 +441,27 @@ def __init__(self):
}
self.global_context = {}

def __reduce__(self):
"""
A helper function used when pickling a StatmechDatabase object.
"""
d = {
'depository': self.depository,
'libraries': self.libraries,
'groups': self.groups,
'libraryOrder': self.libraryOrder,
}
return (StatmechDatabase, (), d)

def __setstate__(self, d):
"""
A helper function used when unpickling a StatmechDatabase object.
"""
self.depository = d['depository']
self.libraries = d['libraries']
self.groups = d['groups']
self.libraryOrder = d['libraryOrder']

def load(self, path, libraries=None, depository=True):
"""
Load the statmech database from the given `path` on disk, where `path`
Expand Down
23 changes: 22 additions & 1 deletion rmgpy/data/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def processOldLibraryEntry(self, data):

################################################################################

class ThermoDatabase:
class ThermoDatabase(object):
"""
A class for working with the RMG thermodynamics database.
"""
Expand All @@ -333,6 +333,27 @@ def __init__(self):
}
self.global_context = {}

def __reduce__(self):
"""
A helper function used when pickling a ThermoDatabase object.
"""
d = {
'depository': self.depository,
'libraries': self.libraries,
'groups': self.groups,
'libraryOrder': self.libraryOrder,
}
return (ThermoDatabase, (), d)

def __setstate__(self, d):
"""
A helper function used when unpickling a ThermoDatabase object.
"""
self.depository = d['depository']
self.libraries = d['libraries']
self.groups = d['groups']
self.libraryOrder = d['libraryOrder']

def load(self, path, libraries=None, depository=True):
"""
Load the thermo database from the given `path` on disk, where `path`
Expand Down

0 comments on commit 53cc496

Please sign in to comment.