| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
# standard library dependencies |
| 6 |
from petl.compat import pickle, next |
| 7 |
|
| 8 |
|
| 9 |
# internal dependencies |
| 10 |
from petl.util.base import Table |
| 11 |
from petl.io.sources import read_source_from_arg, write_source_from_arg |
| 12 |
|
| 13 |
|
| 14 |
def frompickle(source=None): |
| 15 |
""" |
| 16 |
Extract a table From data pickled in the given file. The rows in the |
| 17 |
table should have been pickled to the file one at a time. E.g.:: |
| 18 |
|
| 19 |
>>> import petl as etl |
| 20 |
>>> import pickle |
| 21 |
>>> # set up a file to demonstrate with |
| 22 |
... with open('example.p', 'wb') as f: |
| 23 |
... pickle.dump(['foo', 'bar'], f) |
| 24 |
... pickle.dump(['a', 1], f) |
| 25 |
... pickle.dump(['b', 2], f) |
| 26 |
... pickle.dump(['c', 2.5], f) |
| 27 |
... |
| 28 |
>>> # now demonstrate the use of frompickle() |
| 29 |
... table1 = etl.frompickle('example.p') |
| 30 |
>>> table1 |
| 31 |
+-----+-----+ |
| 32 |
| foo | bar | |
| 33 |
+=====+=====+ |
| 34 |
| 'a' | 1 | |
| 35 |
+-----+-----+ |
| 36 |
| 'b' | 2 | |
| 37 |
+-----+-----+ |
| 38 |
| 'c' | 2.5 | |
| 39 |
+-----+-----+ |
| 40 |
|
| 41 |
|
| 42 |
""" |
| 43 |
|
| 44 |
source = read_source_from_arg(source) |
| 45 |
return PickleView(source) |
| 46 |
|
| 47 |
|
| 48 |
class PickleView(Table): |
| 49 |
|
| 50 |
def __init__(self, source): |
| 51 |
self.source = source |
| 52 |
|
| 53 |
def __iter__(self): |
| 54 |
with self.source.open('rb') as f: |
| 55 |
try: |
| 56 |
while True: |
| 57 |
yield tuple(pickle.load(f)) |
| 58 |
except EOFError: |
| 59 |
pass |
| 60 |
|
| 61 |
|
| 62 |
def topickle(table, source=None, protocol=-1, write_header=True): |
| 63 |
""" |
| 64 |
Write the table to a pickle file. E.g.:: |
| 65 |
|
| 66 |
>>> import petl as etl |
| 67 |
>>> table1 = [['foo', 'bar'], |
| 68 |
... ['a', 1], |
| 69 |
... ['b', 2], |
| 70 |
... ['c', 2]] |
| 71 |
>>> etl.topickle(table1, 'example.p') |
| 72 |
>>> # look what it did |
| 73 |
... table2 = etl.frompickle('example.p') |
| 74 |
>>> table2 |
| 75 |
+-----+-----+ |
| 76 |
| foo | bar | |
| 77 |
+=====+=====+ |
| 78 |
| 'a' | 1 | |
| 79 |
+-----+-----+ |
| 80 |
| 'b' | 2 | |
| 81 |
+-----+-----+ |
| 82 |
| 'c' | 2 | |
| 83 |
+-----+-----+ |
| 84 |
|
| 85 |
Note that if a file already exists at the given location, it will be |
| 86 |
overwritten. |
| 87 |
|
| 88 |
The pickle file format preserves type information, i.e., reading and writing |
| 89 |
is round-trippable for tables with non-string data values. |
| 90 |
|
| 91 |
""" |
| 92 |
|
| 93 |
_writepickle(table, source=source, mode='wb', protocol=protocol, |
| 94 |
write_header=write_header) |
| 95 |
|
| 96 |
|
| 97 |
Table.topickle = topickle |
| 98 |
|
| 99 |
|
| 100 |
def appendpickle(table, source=None, protocol=-1, write_header=False): |
| 101 |
""" |
| 102 |
Append data to an existing pickle file. I.e., |
| 103 |
as :func:`petl.io.pickle.topickle` but the file is opened in append mode. |
| 104 |
|
| 105 |
Note that no attempt is made to check that the fields or row lengths are |
| 106 |
consistent with the existing data, the data rows from the table are simply |
| 107 |
appended to the file. |
| 108 |
|
| 109 |
""" |
| 110 |
|
| 111 |
_writepickle(table, source=source, mode='ab', protocol=protocol, |
| 112 |
write_header=write_header) |
| 113 |
|
| 114 |
|
| 115 |
Table.appendpickle = appendpickle |
| 116 |
|
| 117 |
|
| 118 |
def _writepickle(table, source, mode, protocol, write_header): |
| 119 |
source = write_source_from_arg(source, mode) |
| 120 |
with source.open(mode) as f: |
| 121 |
it = iter(table) |
| 122 |
try: |
| 123 |
hdr = next(it) |
| 124 |
except StopIteration: |
| 125 |
return |
| 126 |
if write_header: |
| 127 |
pickle.dump(hdr, f, protocol) |
| 128 |
for row in it: |
| 129 |
pickle.dump(row, f, protocol) |
| 130 |
|
| 131 |
|
| 132 |
def teepickle(table, source=None, protocol=-1, write_header=True): |
| 133 |
""" |
| 134 |
Return a table that writes rows to a pickle file as they are iterated |
| 135 |
over. |
| 136 |
|
| 137 |
""" |
| 138 |
|
| 139 |
return TeePickleView(table, source=source, protocol=protocol, |
| 140 |
write_header=write_header) |
| 141 |
|
| 142 |
|
| 143 |
Table.teepickle = teepickle |
| 144 |
|
| 145 |
|
| 146 |
class TeePickleView(Table): |
| 147 |
|
| 148 |
def __init__(self, table, source=None, protocol=-1, write_header=True): |
| 149 |
self.table = table |
| 150 |
self.source = source |
| 151 |
self.protocol = protocol |
| 152 |
self.write_header = write_header |
| 153 |
|
| 154 |
def __iter__(self): |
| 155 |
protocol = self.protocol |
| 156 |
source = write_source_from_arg(self.source) |
| 157 |
with source.open('wb') as f: |
| 158 |
it = iter(self.table) |
| 159 |
try: |
| 160 |
hdr = next(it) |
| 161 |
except StopIteration: |
| 162 |
return |
| 163 |
if self.write_header: |
| 164 |
pickle.dump(hdr, f, protocol) |
| 165 |
yield tuple(hdr) |
| 166 |
for row in it: |
| 167 |
pickle.dump(row, f, protocol) |
| 168 |
yield tuple(row) |