def test_topickle_appendpickle(): """Test the topickle and appendpickle functions.""" # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) f = NamedTemporaryFile(delete=False) topickle(table, f.name) def picklereader(fl): try: while True: yield pickle.load(fl) except EOFError: pass # check what it did with open(f.name, 'rb') as o: actual = picklereader(o) ieq(table, actual) # check appending table2 = (('foo', 'bar'), ('d', 7), ('e', 9), ('f', 1)) appendpickle(table2, f.name) # check what it did with open(f.name, 'rb') as o: actual = picklereader(o) expect = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2), ('d', 7), ('e', 9), ('f', 1)) ieq(expect, actual)
def test_topickle_appendpickle(): """Test the topickle and appendpickle functions.""" # exercise function table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2)) f = NamedTemporaryFile(delete=False) topickle(table, f.name) def picklereader(file): try: while True: yield pickle.load(file) except EOFError: pass # check what it did with open(f.name, "rb") as o: actual = picklereader(o) ieq(table, actual) # check appending table2 = (("foo", "bar"), ("d", 7), ("e", 9), ("f", 1)) appendpickle(table2, f.name) # check what it did with open(f.name, "rb") as o: actual = picklereader(o) expect = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2), ("d", 7), ("e", 9), ("f", 1)) ieq(expect, actual)
# appendpickle table = [['foo', 'bar'], ['d', 7], ['e', 42], ['f', 12]] from petl import look, frompickle # inspect an existing pickle file testdat = frompickle('test.dat') look(testdat) # append some data from petl import appendpickle look(table) appendpickle(table, 'test.dat') # look what it did look(testdat) # tosqlite3 table = [['foo', 'bar'], ['a', 1], ['b', 2], ['c', 2]] from petl import tosqlite3, look look(table) # by default, if the table does not already exist, it will be created tosqlite3(table, 'test.db', 'foobar')
def topickle(data, target, append, **kwargs): kwargs = filter_keys(kwargs, ("protocol", "write_header")) if append: return etl.appendpickle(data, target, **kwargs) else: return etl.topickle(data, target, **kwargs)