def doit3(): class foo: pass f = foo() f.a = 1 f.b = f.a f.c = "abc" f.d = f.c f.r = re.compile('aaa\s+[0-9]*') f.r2 = f.r l = [1,2,3] f.u = UserList([l,l]) x = xml_pickle.dumps(f) #print x g = thing_from_sax2(None,x) print f.u, f.r.pattern, f.r2.pattern
def doit3(): class foo: pass f = foo() f.a = 1 f.b = f.a f.c = "abc" f.d = f.c f.r = re.compile('aaa\s+[0-9]*') f.r2 = f.r l = [1, 2, 3] f.u = UserList([l, l]) x = xml_pickle.dumps(f) #print x g = thing_from_sax2(None, x) print f.u, f.r.pattern, f.r2.pattern
#funcs.set_parser() #-- Create some unicode and python strings (and an object that contains them) ustring = u"Alef: %s, Omega: %s" % (unichr(1488), unichr(969)) pstring = "Only US-ASCII characters" estring = "Only US-ASCII with line breaks\n\tthat was a tab" class C: def __init__(self, ustring, pstring, estring): self.ustring = ustring self.pstring = pstring self.estring = estring o = C(ustring, pstring, estring) #-- Try standard pickling cycle (default setInBody() settings) #print '\n------------* Pickle with Python and Unicode strings *------------------' xml = dumps(o) #print xml, #print '------------* Restored attributes from different strings *--------------' o2 = loads(xml) # check types explicitly, since comparison will coerce types if not isinstance(o2.ustring,UnicodeType): raise "AAGH! Didn't get UnicodeType" if not isinstance(o2.pstring,StringType): raise "AAGH! Didn't get StringType for pstring" if not isinstance(o2.estring,StringType): raise "AAGH! Didn't get StringType for estring" #print "UNICODE:", `o2.ustring`, type(o2.ustring) #print "PLAIN: ", o2.pstring, type(o2.pstring) #print "ESCAPED:", o2.estring, type(o2.estring)
#setParanoia(0) # by default, with references x1 = xml_pickle.dumpsp(u) print x1 #del u g = xml_pickle.loads(x1) #print g if u != g: raise "ERROR(1)" # next, using DEEPCOPY #print "------ DEEP COPY ------" #setDeepCopy(1) x2 = xml_pickle.dumps(g) #print x #del g z = xml_pickle.loads(x2) # deepcopy version should be significantly larger #if (len(x2) - len(x1)) < 1000: #raise "ERROR(2)" if z != g: raise "ERROR(3)" print "** OK **"
foo.l.append( re.compile('[foo|bar]?') ) foo.l.append( re.compile('[qrs]+[1-9]?') ) foo.ul = UserList.UserList() foo.ul.append( re.compile('[+\-][0-9]+') ) foo.ul.append( re.compile('(bored yet)?') ) foo.tup = ( re.compile('this is [not]? '), re.compile('a [tuple|list]') ) #print "---PRE-PICKLE---" #printfoo(foo) # turn off extensions so that SREs will be saved as rawpickles #__disable_extensions() x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) checkfoo(foo,bar) # same thing on copy x2 = xml_pickle.dumps(bar) baz = xml_pickle.loads(x2) checkfoo(bar,baz) #print "---XML from original---"
import zif.sedna.persistence.pickle as xml_pickle class Test: pass o1 = Test() o1.s = "o1" o2 = Test() o2.s = "o2" o1.obj1 = o2 o1.obj2 = o2 o2.obj3 = o1 o2.obj4 = o1 xml = xml_pickle.dumps(o1) #print xml z = xml_pickle.loads(xml) # check it if z.s != o1.s or z.obj1.s != o1.obj1.s or \ z.obj2.s != o1.obj2.s or z.obj1.obj3.s != o1.obj1.obj3.s or \ z.obj2.obj4.s != o1.obj2.obj4.s: raise "ERROR(1)" print "** OK **"
def pickling(): x = xml_pickle.dumps(theobject)
#raise "ERROR(5)" ## dump & make sure modname doesn't stick #s = xml_pickle.dumps(p) ##print s #if re.search('module',s): #raise "ERROR(6)" ## remove so it won't be found again below #xml_pickle.remove_class_from_store('Foo') ## now, make sure we haven't broken modnames showing up when they SHOULD :-) ##print "My namespace (__main__) -- SHOULD SEE MODULE NAME IN XML" ## allow it to load my Foo #xml_pickle.setParanoia(0) p = xml_pickle.loads(ud_xml) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != '__main__.Foo': raise "ERROR(7)" # dump & make sure module name is written s = xml_pickle.dumps(p) print s if not re.search('module="__main__"', s): raise "ERROR(8)" if not re.search('class="Foo"', s): raise "ERROR(9)" print "** OK **"
#funcs.set_parser() class foo: pass # so we can unpickle foo #xml_pickle.setParanoia(0) # test the obvious self-refs l = [1, 2] l.append(l) #print l x = xml_pickle.dumps(l) print x g = xml_pickle.loads(x) print g # check values & ref if g[0] != l[0] or g[1] != l[1] or id(g[2]) != id(g): raise "ERROR(1)" d = {'a': 1} d['b'] = d #print d x = xml_pickle.dumps(d) #print x g = xml_pickle.loads(x) #print g # check values & ref
import zif.sedna.persistence.pickle as xml_pickle import gzip from StringIO import StringIO import tempfile # --- Make an object to play with --- class C: pass o = C() o.lst, o.dct = [1, 2], {'spam': 'eggs'} x = xml_pickle.dumps(o, 1, xml_declaration=True) # make sure xml_pickle really gzipped it sio = StringIO(x) gz = gzip.GzipFile('dummy', 'rb', 9, sio) if gz.read(5) != '<?xml': raise "ERROR(1)" # reload object o2 = xml_pickle.loads(x) # check it if o.lst != o2.lst or o.dct != o2.dct: raise "ERROR(2)" temp = tempfile.TemporaryFile()
import lxml.objectify as xo import zif.sedna.persistence.pickle as xp from StringIO import StringIO import funcs #funcs.set_parser() xml = '''<?xml version="1.0"?> <!DOCTYPE Spam SYSTEM "spam.dtd" > <Spam> <Eggs>Some text about eggs.</Eggs> <MoreSpam>Ode to Spam</MoreSpam> </Spam> ''' fh = StringIO(xml) o = xo.fromstring(xml) s = xp.dumps(o) #print "---------- xml_objectify'd object, xml_pickle'd ----------" #print s o2 = xp.loads(s) #print "---------- object after the pickle/unpickle cycle --------" #print xp.dumps(o2) if o.Eggs.PCDATA != o2.Eggs.PCDATA or \ o.MoreSpam.PCDATA != o2.MoreSpam.PCDATA: raise "ERROR(1)" print "** OK **"
o = C() from os import sep fname = sep.join(xml_pickle.__file__.split(sep)[:-1]) + sep + 'pickle.py' o.lst = open(fname).readlines() o.tup = tuple(o.lst) o.dct = {} for i in range(500): o.dct[i] = i print "Size of standard cPickle: ", len(cPickle.dumps(o)) print "Size of binary cPickle: ", len(cPickle.dumps(o, 1)) print "gzip'd standard cPickle: ", len(compress(cPickle.dumps(o))) print "gzip'd binary cPickle: ", len(compress(cPickle.dumps(o, 1))) print "Size of standard xml_pickle: ", len(xml_pickle.dumps(o)) print "Size of gzip'd xml_pickle: ", len(xml_pickle.dumps(o, 1)) #print xml_pickle.dumpsp(o) #----------------------------------------------------------------------- # Second: look at actual compressed and uncompressed (small) pickles #----------------------------------------------------------------------- o = C() o.lst = [1, 2] o.dct = {'spam': 'eggs'} s = xml_pickle.dumps(o) print "--------------- Uncompressed xml_pickle: (%s bytes)---------------" % len( s) print s
from StringIO import StringIO import funcs #funcs.set_parser() xml = '''<?xml version="1.0"?> <!DOCTYPE Spam SYSTEM "spam.dtd" > <Spam> <Eggs>Some text about eggs.</Eggs> <MoreSpam>Ode to Spam</MoreSpam> </Spam> ''' fh = StringIO(xml) o = xo.fromstring(xml) s = xp.dumps(o) #print "---------- xml_objectify'd object, xml_pickle'd ----------" #print s o2 = xp.loads(s) #print "---------- object after the pickle/unpickle cycle --------" #print xp.dumps(o2) if o.Eggs.PCDATA != o2.Eggs.PCDATA or \ o.MoreSpam.PCDATA != o2.MoreSpam.PCDATA: raise "ERROR(1)" print "** OK **"
#print "---PRE-PICKLE---" #printfoo(foo) #__disable_extensions() x1 = xml_pickle.dumpsp(foo) print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) #bar.hi() checkfoo(foo,bar) # same thing on copy x2 = xml_pickle.dumps(bar) baz = xml_pickle.loads(x2) checkfoo(bar,baz) #print "---XML from original---" #print x1 #print "---XML from copy---" #print x2 print "** OK **"
o = C() from os import sep fname = sep.join(xml_pickle.__file__.split(sep)[:-1])+sep+'pickle.py' o.lst = open(fname).readlines() o.tup = tuple(o.lst) o.dct = {} for i in range(500): o.dct[i] = i print "Size of standard cPickle: ", len(cPickle.dumps(o)) print "Size of binary cPickle: ", len(cPickle.dumps(o,1)) print "gzip'd standard cPickle: ", len(compress(cPickle.dumps(o))) print "gzip'd binary cPickle: ", len(compress(cPickle.dumps(o,1))) print "Size of standard xml_pickle: ", len(xml_pickle.dumps(o)) print "Size of gzip'd xml_pickle: ", len(xml_pickle.dumps(o,1)) #print xml_pickle.dumpsp(o) #----------------------------------------------------------------------- # Second: look at actual compressed and uncompressed (small) pickles #----------------------------------------------------------------------- o = C() o.lst = [1,2] o.dct = {'spam':'eggs'} s = xml_pickle.dumps(o) print "--------------- Uncompressed xml_pickle: (%s bytes)---------------" % len(s) print s s = xml_pickle.dumps(o,1)
# this is from a bug report from Even Westvang <*****@*****.**>. # the problem is that new-style classes used as attributes weren't # being pickled correctly. # let it load my classes #xml_pickle.setParanoia(0) class PickleMeOld: def __init__(self): pass class PickleMeNew(object): def __init__(self): pass class Container(object): def __init__(self, klass): self.classRef = klass x = xml_pickle.dumps(Container(PickleMeOld)) o = xml_pickle.loads(x) if o.classRef != PickleMeOld: raise "ERROR(1)" x = xml_pickle.dumps(Container(PickleMeNew)) o = xml_pickle.loads(x) if o.classRef != PickleMeNew: raise "ERROR(2)" print "** OK **"
# by default, with references x1 = xml_pickle.dumpsp(u) print x1 #del u g = xml_pickle.loads(x1) #print g if u != g: raise "ERROR(1)" # next, using DEEPCOPY #print "------ DEEP COPY ------" #setDeepCopy(1) x2 = xml_pickle.dumps(g) #print x #del g z = xml_pickle.loads(x2) # deepcopy version should be significantly larger #if (len(x2) - len(x1)) < 1000: #raise "ERROR(2)" if z != g: raise "ERROR(3)" print "** OK **"
def doit2(): u = UserList([1,2,[(3,4,5),(6,7,8)],3,UserList([0,1,2]),4]) x = xml_pickle.dumps(u) #print x g = thing_from_sax2(None,x) print g
print "xml_pickle load" t1 = time() fh = open('aaa.xml','r') #xml_pickle.setParser("DOM") # default, but just to be sure o = xml_pickle.load(fh) fh.close() print "TIME = %f"%(time()-t1) del o theobject = mk_foo(6) def pickling(): x = xml_pickle.dumps(theobject) thepickle = xml_pickle.dumps(theobject) def unpickling(): obj = xml_pickle.loads(thepickle) def pyxml_marshal(): try: from xml.marshal import generic except ImportError: # Skip comparison with PyXML's xml.marshal if unavailable print "Skipping PyXML xml.marshal" return f = mk_foo(6) print "CREATE XML (xml.marshal style)" t1 = time()
# let it load my classes #xml_pickle.setParanoia(0) class PickleMeOld: def __init__(self): pass class PickleMeNew(object): def __init__(self): pass class Container(object): def __init__(self, klass): self.classRef = klass x = xml_pickle.dumps(Container(PickleMeOld)) o = xml_pickle.loads(x) if o.classRef != PickleMeOld: raise "ERROR(1)" x = xml_pickle.dumps(Container(PickleMeNew)) o = xml_pickle.loads(x) if o.classRef != PickleMeNew: raise "ERROR(2)" print "** OK **"
x = top() x.a = lcombo([4,5,6],1,2) x.a.zz = 10 #print x.a, x.a.a, x.a.b, x.a.zz s = xml_pickle.dumpsp(x) print s g = xml_pickle.loads(s) #print g.a, g.a.a, g.a.b, g.a.zz check_combo(x,g) #print "* DCOMBO" x = top() x.a = dcombo({'a':1,'b':2,'c':3},1,2) x.a.zz = 10 #print x.a, x.a.a, x.a.b, x.a.zz s = xml_pickle.dumps(x) #print s g = xml_pickle.loads(s) #print g.a, g.a.a, g.a.b, g.a.zz check_combo(x,g) #print "* TCOMBO" x = top() x.a = tcombo((10,11,12),1,2) x.a.zz = 10 #print x.a, x.a.a, x.a.b, x.a.zz s = xml_pickle.dumps(x) #print s g = xml_pickle.loads(s) #print g.a, g.a.a, g.a.b, g.a.zz check_combo(x,g)
#print "---PRE-PICKLE---" #printfoo(foo) #__disable_extensions() x1 = xml_pickle.dumpsp(foo) print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #printfoo(bar) #bar.hi() checkfoo(foo, bar) # same thing on copy x2 = xml_pickle.dumps(bar) baz = xml_pickle.loads(x2) checkfoo(bar, baz) #print "---XML from original---" #print x1 #print "---XML from copy---" #print x2 print "** OK **"
pstring = "Only US-ASCII characters" estring = "Only US-ASCII with line breaks\n\tthat was a tab" class C: def __init__(self, ustring, pstring, estring): self.ustring = ustring self.pstring = pstring self.estring = estring o = C(ustring, pstring, estring) #-- Try standard pickling cycle (default setInBody() settings) #print '\n------------* Pickle with Python and Unicode strings *------------------' xml = dumps(o) #print xml, #print '------------* Restored attributes from different strings *--------------' o2 = loads(xml) # check types explicitly, since comparison will coerce types if not isinstance(o2.ustring, UnicodeType): raise "AAGH! Didn't get UnicodeType" if not isinstance(o2.pstring, StringType): raise "AAGH! Didn't get StringType for pstring" if not isinstance(o2.estring, StringType): raise "AAGH! Didn't get StringType for estring" #print "UNICODE:", `o2.ustring`, type(o2.ustring) #print "PLAIN: ", o2.pstring, type(o2.pstring) #print "ESCAPED:", o2.estring, type(o2.estring)
fh = open('aaa.xml', 'r') #xml_pickle.setParser("DOM") # default, but just to be sure o = xml_pickle.load(fh) fh.close() print "TIME = %f" % (time() - t1) del o theobject = mk_foo(6) def pickling(): x = xml_pickle.dumps(theobject) thepickle = xml_pickle.dumps(theobject) def unpickling(): obj = xml_pickle.loads(thepickle) def pyxml_marshal(): try: from xml.marshal import generic except ImportError: # Skip comparison with PyXML's xml.marshal if unavailable print "Skipping PyXML xml.marshal" return f = mk_foo(6)
import funcs #funcs.set_parser() class foo: pass #xml_pickle.setParanoia(0) f = foo() f.b = test_ftypes_i.gimme_bfunc() f.p = test_ftypes_i.gimme_pfunc() f.f = foo print f.b, f.p, f.f x = xml_pickle.dumps(f) print x g = xml_pickle.loads(x) print g.b, g.p, g.f # check it for attr in ['b','p','f']: if getattr(f,attr) != getattr(g,attr): raise "ERROR(1)" print "** OK **"
def doit2(): u = UserList([1, 2, [(3, 4, 5), (6, 7, 8)], 3, UserList([0, 1, 2]), 4]) x = xml_pickle.dumps(u) #print x g = thing_from_sax2(None, x) print g
import zif.sedna.persistence.pickle as xml_pickle import sys import funcs #funcs.set_parser() class foo: pass f = foo() f.a = (1, 2, 3) # method 1 -- StreamWriter is an uncompressed StringIO x = xml_pickle.dumps(f) # check header (to ensure correct method used) + contents #if x[0:5] == '<?xml': #print "OK" #else: #print "ERROR" #sys.exit(1) g = xml_pickle.loads(x) if g.a == (1, 2, 3): print "OK" else: print "ERROR" sys.exit(1)
foo.ud['TwoTwo'] = re.compile('world$') foo.l = [] foo.l.append(re.compile('[foo|bar]?')) foo.l.append(re.compile('[qrs]+[1-9]?')) foo.ul = UserList.UserList() foo.ul.append(re.compile('[+\-][0-9]+')) foo.ul.append(re.compile('(bored yet)?')) foo.tup = (re.compile('this is [not]? '), re.compile('a [tuple|list]')) #print "---PRE-PICKLE---" #printfoo(foo) x1 = xml_pickle.dumps(foo) #print x1 #print "---POST-PICKLE---" bar = xml_pickle.loads(x1) #print str(dir(bar)) #printfoo(bar) checkfoo(foo, bar) # same thing on copy x2 = xml_pickle.dumps(bar) baz = xml_pickle.loads(x2) checkfoo(bar, baz)
## dump & make sure modname doesn't stick #s = xml_pickle.dumps(p) ##print s #if re.search('module',s): #raise "ERROR(6)" ## remove so it won't be found again below #xml_pickle.remove_class_from_store('Foo') ## now, make sure we haven't broken modnames showing up when they SHOULD :-) ##print "My namespace (__main__) -- SHOULD SEE MODULE NAME IN XML" ## allow it to load my Foo #xml_pickle.setParanoia(0) p = xml_pickle.loads(ud_xml) # print it so we can see the modname #print "Fullname = "+str(p) if str(p.__class__) != '__main__.Foo': raise "ERROR(7)" # dump & make sure module name is written s = xml_pickle.dumps(p) print s if not re.search('module="__main__"',s): raise "ERROR(8)" if not re.search('class="Foo"',s): raise "ERROR(9)" print "** OK **"