__init__.py
changeset 9402 2c48c091b6a2
parent 9046 6fb3f0106301
parent 9299 c5eed908117d
child 9891 3386fd89c914
child 9984 793377697c81
--- a/__init__.py	Tue Jul 02 17:09:04 2013 +0200
+++ b/__init__.py	Mon Jan 13 13:47:47 2014 +0100
@@ -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 = {}