def testpickle(self): file = open("test.bin", "wb") output = typedbytes.Output(file) output.write("123") output.write(MyClass()) file.close() file = open("test.bin", "rb") input = typedbytes.Input(file) self.assertEqual("123", input.read()) self.assertEqual(234, input.read().attr) file.close() os.remove("test.bin")
def testio(self): objects = TestIO.objects file = open("test.bin", "wb") output = typedbytes.Output(file) output.writes(objects) file.close() file = open("test.bin", "rb") input = typedbytes.Input(file) for (index, record) in enumerate(input.reads()): self.assertEqual(objects[index], record) file.close() os.remove("test.bin")
def testwrongio(self): try: file = open("test.bin", "wb") output = typedbytes.Output(file) output.writes([1]) file.close() file = open("test.bin", "rb") input = typedbytes.Input(file) input = typedbytes.PairedInput(file) self.assertRaises(StructError, lambda :list(input.reads())) file.close() finally: os.remove("test.bin")
def testints(self): file = open("speedtest.bin", "wb") output = typedbytes.Output(file) t = time.time() output.writes(xrange(100000)) print "writing ints:", time.time() - t file.close() file = open("speedtest.bin", "rb") input = typedbytes.Input(file) t = time.time() for record in input: pass print "reading ints:", time.time() - t file.close() os.remove("speedtest.bin")
import numpy as np import numpy as np, sys import ctypedbytes import cStringIO import struct # For raw string I/O print '-----------------------------------------------' arr = np.array([23.443,88.33,0.333,8.433,3434.01]) sio = cStringIO.StringIO() myfmt='f'*len(arr) bin=struct.pack(myfmt,*arr) print "bin", bin, len(bin) arr2 = np.array(struct.unpack(myfmt,bin)) print type(arr2) print "extracted", arr2 print '-----------------------------------------------' print "text", len(";".join(map(str,arr))), ";".join(map(str,arr)) print '-----------------------------------------------' sio = cStringIO.StringIO() output = ctypedbytes.Output(sio) output.writes(arr) print sio.getvalue()