Skip to content

Commit

Permalink
simplified serialisation support more
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjgowers committed Feb 16, 2019
1 parent aa595f5 commit ba1a138
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
11 changes: 0 additions & 11 deletions package/MDAnalysis/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,17 +2078,6 @@ def __init__(self, filename, convert_units=None, **kwargs):

self._ts_kwargs = ts_kwargs

@classmethod
def _unpickle_Reader(cls, filename, timestep, auxs, trans):
new_R = cls(filename)
new_R.add_transformations(trans)
for auxname, auxdata in auxs.items():
new_R.add_aux(auxname, auxdata)

def __reduce__(self):
return (self._unpickle_Reader,
(self.filename, self.ts, self._auxs, self._transformations))

def __len__(self):
return self.n_frames

Expand Down
15 changes: 11 additions & 4 deletions package/MDAnalysis/core/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def _gen_anchor_hash(self):
return self._anchor_uuid
except AttributeError:
# store this so we can later recall it if needed
self._anchor_uuid = uuid.uuid4()
self._anchor_uuid = str(uuid.uuid4())
return self._anchor_uuid

@property
Expand Down Expand Up @@ -770,13 +770,20 @@ def __repr__(self):

@classmethod
def _unpickle_U(cls, top, traj, anchor):
u = cls(top, anchor_name=anchor)
u.load_new(traj)
"""Special method used by __reduce__ to deserialise a Universe"""
# top is a Topology object at this point, but Universe can handle that
u = cls(top)
u.anchor_name = anchor
# maybe this is None, but that's still cool
u.trajectory = traj

return u

def __reduce__(self):
return (self._unpickle_U, (self._topology, self.trajectory.filename, self.anchor_name))
# Can't quite use __setstate__/__getstate__ so go via __reduce__
# Universe's two "legs" of topology and traj both serialise themselves
# the only other state held in Universe is anchor name?
return (self._unpickle_U, (self._topology, self._trajectory, self.anchor_name))

# Properties
@property
Expand Down

0 comments on commit ba1a138

Please sign in to comment.