[Yt-svn] yt-commit r1036 - branches/yt-object-serialization/yt/lagos

mturk at wrangler.dreamhost.com mturk at wrangler.dreamhost.com
Wed Dec 24 11:02:25 PST 2008


Author: mturk
Date: Wed Dec 24 11:02:24 2008
New Revision: 1036
URL: http://yt.spacepope.org/changeset/1036

Log:
Adding object serialization:

  some_object.save_object(object_name)

will store the object to the hierarchy's .yt file, which can then be loaded
with:

  pf.h.load_object(object_name)

Additionally,

  some_object.save_object(object_name, filename)

will save it to a shelve object called filename, which can be loaded
independently (and which returns both the parameter file AND the object) using
the shelve module:

  db = shelve.open(filename)
  my_obj = db[name]



Modified:
   branches/yt-object-serialization/yt/lagos/BaseDataTypes.py
   branches/yt-object-serialization/yt/lagos/HierarchyType.py
   branches/yt-object-serialization/yt/lagos/__init__.py

Modified: branches/yt-object-serialization/yt/lagos/BaseDataTypes.py
==============================================================================
--- branches/yt-object-serialization/yt/lagos/BaseDataTypes.py	(original)
+++ branches/yt-object-serialization/yt/lagos/BaseDataTypes.py	Wed Dec 24 11:02:24 2008
@@ -229,6 +229,16 @@
             fid.write("\n")
         fid.close()
 
+    def save_object(self, name, filename = None):
+        if filename is not None:
+            ds = shelve.open(filename)
+            if name in ds:
+                mylog.info("Overwriting %s in %s", name, filename)
+            ds[name] = self
+            ds.close()
+        else:
+            self.hierarchy.save_object(self, name)
+
     def __reduce__(self):
         args = tuple([self.pf._hash(), self._type_name] +
                      [getattr(self, n) for n in self._con_args] +

Modified: branches/yt-object-serialization/yt/lagos/HierarchyType.py
==============================================================================
--- branches/yt-object-serialization/yt/lagos/HierarchyType.py	(original)
+++ branches/yt-object-serialization/yt/lagos/HierarchyType.py	Wed Dec 24 11:02:24 2008
@@ -148,6 +148,7 @@
         try:
             node_loc = self._data_file.getNode(node)
             if name in node_loc and force:
+                mylog.info("Overwriting node %s/%s", node, name)
                 self._data_file.removeNode(node, name, recursive=True)
             if name in node_loc and passthrough:
                 return
@@ -158,6 +159,16 @@
             for i, j in set_attr.items(): arr.setAttr(i,j)
         self._data_file.flush()
 
+    def save_object(self, obj, name):
+        s = cPickle.dumps(obj)
+        self.save_data(s, "/Objects", name, force = True)
+
+    def load_object(self, name):
+        obj = self.get_data("/Objects", name)
+        if obj is None:
+            return
+        return cPickle.loads(obj.read())[1] # Just the object, not the pf
+
     def get_data(self, node, name):
         """
         Return the dataset with a given *name* located at *node* in the

Modified: branches/yt-object-serialization/yt/lagos/__init__.py
==============================================================================
--- branches/yt-object-serialization/yt/lagos/__init__.py	(original)
+++ branches/yt-object-serialization/yt/lagos/__init__.py	Wed Dec 24 11:02:24 2008
@@ -49,7 +49,7 @@
 from math import ceil, floor, log10, pi
 import os, os.path, types, exceptions, re
 from stat import ST_CTIME
-import sets
+import sets, shelve
 
 import time
 



More information about the yt-svn mailing list