+try:
+ from cPickle import loads, dumps
+except ImportError:
+ from pickle import loads, dumps
+
+from copy import deepcopy
+from base64 import b64encode, b64decode
+from zlib import compress, decompress
+import re
+
+from base import BaseModel
+
+MAX_MARKABLE_STRING_LENGTH = 100
+
+class PickledObject(unicode):
+ pass
+
+def dbsafe_encode(value, compress_object=True):
+ if not compress_object:
+ value = b64encode(dumps(deepcopy(value)))
+ else:
+ value = b64encode(compress(dumps(deepcopy(value))))
+ return PickledObject(value)
+
+def dbsafe_decode(value, compress_object=True):
+ if not compress_object:
+ value = loads(b64decode(value))
+ else:
+ value = loads(decompress(b64decode(value)))
+ return value