__init__.py
changeset 9299 c5eed908117d
parent 8748 f5027f8d2478
child 9402 2c48c091b6a2
child 9912 a84922befb89
--- a/__init__.py	Thu Oct 17 11:34:03 2013 +0200
+++ b/__init__.py	Fri Aug 23 15:06:27 2013 +0200
@@ -22,6 +22,8 @@
 
 # ignore the pygments UserWarnings
 import warnings
+import cPickle
+import zlib
 warnings.filterwarnings('ignore', category=UserWarning,
                         message='.*was already imported',
                         module='.*pygments')
@@ -120,6 +122,26 @@
         binary.seek(0)
         return binary
 
+    def __eq__(self, other):
+        if not isinstance(other, Binary):
+            return False
+        return self.getvalue(), other.getvalue()
+
+
+    # Binary helpers to store/fetch python objects
+
+    @classmethod
+    def zpickle(cls, obj):
+        """ return a Binary containing a gzipped pickle of obj """
+        retval = cls()
+        retval.write(zlib.compress(cPickle.dumps(obj, protocol=2)))
+        return retval
+
+    def unzpickle(self):
+        """ decompress and loads the stream before returning it """
+        return cPickle.loads(zlib.decompress(self.getvalue()))
+
+
 def str_or_binary(value):
     if isinstance(value, Binary):
         return value
@@ -127,7 +149,6 @@
 BASE_CONVERTERS['Password'] = str_or_binary
 
 
-
 # use this dictionary to rename entity types while keeping bw compat
 ETYPE_NAME_MAP = {}