| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
|
| 4 |
import operator |
| 5 |
from collections import OrderedDict |
| 6 |
from itertools import islice |
| 7 |
from petl.compat import izip_longest, text_type, next |
| 8 |
|
| 9 |
|
| 10 |
from petl.util.base import asindices, Table |
| 11 |
|
| 12 |
|
| 13 |
def listoflists(tbl): |
| 14 |
return [list(row) for row in tbl] |
| 15 |
|
| 16 |
|
| 17 |
Table.listoflists = listoflists |
| 18 |
Table.lol = listoflists |
| 19 |
|
| 20 |
|
| 21 |
def tupleoftuples(tbl): |
| 22 |
return tuple(tuple(row) for row in tbl) |
| 23 |
|
| 24 |
|
| 25 |
Table.tupleoftuples = tupleoftuples |
| 26 |
Table.tot = tupleoftuples |
| 27 |
|
| 28 |
|
| 29 |
def listoftuples(tbl): |
| 30 |
return [tuple(row) for row in tbl] |
| 31 |
|
| 32 |
|
| 33 |
Table.listoftuples = listoftuples |
| 34 |
Table.lot = listoftuples |
| 35 |
|
| 36 |
|
| 37 |
def tupleoflists(tbl): |
| 38 |
return tuple(list(row) for row in tbl) |
| 39 |
|
| 40 |
|
| 41 |
Table.tupleoflists = tupleoflists |
| 42 |
Table.tol = tupleoflists |
| 43 |
|
| 44 |
|
| 45 |
def columns(table, missing=None): |
| 46 |
""" |
| 47 |
Construct a :class:`dict` mapping field names to lists of values. E.g.:: |
| 48 |
|
| 49 |
>>> import petl as etl |
| 50 |
>>> table = [['foo', 'bar'], ['a', 1], ['b', 2], ['b', 3]] |
| 51 |
>>> cols = etl.columns(table) |
| 52 |
>>> cols['foo'] |
| 53 |
['a', 'b', 'b'] |
| 54 |
>>> cols['bar'] |
| 55 |
[1, 2, 3] |
| 56 |
|
| 57 |
See also :func:`petl.util.materialise.facetcolumns`. |
| 58 |
|
| 59 |
""" |
| 60 |
|
| 61 |
cols = OrderedDict() |
| 62 |
it = iter(table) |
| 63 |
try: |
| 64 |
hdr = next(it) |
| 65 |
except StopIteration: |
| 66 |
hdr = [] |
| 67 |
flds = list(map(text_type, hdr)) |
| 68 |
for f in flds: |
| 69 |
cols[f] = list() |
| 70 |
for row in it: |
| 71 |
for f, v in izip_longest(flds, row, fillvalue=missing): |
| 72 |
if f in cols: |
| 73 |
cols[f].append(v) |
| 74 |
return cols |
| 75 |
|
| 76 |
|
| 77 |
Table.columns = columns |
| 78 |
|
| 79 |
|
| 80 |
def facetcolumns(table, key, missing=None): |
| 81 |
""" |
| 82 |
Like :func:`petl.util.materialise.columns` but stratified by values of the |
| 83 |
given key field. E.g.:: |
| 84 |
|
| 85 |
>>> import petl as etl |
| 86 |
>>> table = [['foo', 'bar', 'baz'], |
| 87 |
... ['a', 1, True], |
| 88 |
... ['b', 2, True], |
| 89 |
... ['b', 3]] |
| 90 |
>>> fc = etl.facetcolumns(table, 'foo') |
| 91 |
>>> fc['a'] |
| 92 |
{'foo': ['a'], 'bar': [1], 'baz': [True]} |
| 93 |
>>> fc['b'] |
| 94 |
{'foo': ['b', 'b'], 'bar': [2, 3], 'baz': [True, None]} |
| 95 |
|
| 96 |
""" |
| 97 |
|
| 98 |
fct = dict() |
| 99 |
it = iter(table) |
| 100 |
try: |
| 101 |
hdr = next(it) |
| 102 |
except StopIteration: |
| 103 |
hdr = [] |
| 104 |
flds = list(map(text_type, hdr)) |
| 105 |
indices = asindices(hdr, key) |
| 106 |
assert len(indices) > 0, 'no key field selected' |
| 107 |
getkey = operator.itemgetter(*indices) |
| 108 |
|
| 109 |
for row in it: |
| 110 |
kv = getkey(row) |
| 111 |
if kv not in fct: |
| 112 |
cols = dict() |
| 113 |
for f in flds: |
| 114 |
cols[f] = list() |
| 115 |
fct[kv] = cols |
| 116 |
else: |
| 117 |
cols = fct[kv] |
| 118 |
for f, v in izip_longest(flds, row, fillvalue=missing): |
| 119 |
if f in cols: |
| 120 |
cols[f].append(v) |
| 121 |
|
| 122 |
return fct |
| 123 |
|
| 124 |
|
| 125 |
Table.facetcolumns = facetcolumns |
| 126 |
|
| 127 |
|
| 128 |
def cache(table, n=None): |
| 129 |
""" |
| 130 |
Wrap the table with a cache that caches up to `n` rows as they are initially |
| 131 |
requested via iteration (cache all rows be default). |
| 132 |
|
| 133 |
""" |
| 134 |
|
| 135 |
return CacheView(table, n=n) |
| 136 |
|
| 137 |
|
| 138 |
Table.cache = cache |
| 139 |
|
| 140 |
|
| 141 |
class CacheView(Table): |
| 142 |
|
| 143 |
def __init__(self, inner, n=None): |
| 144 |
self.inner = inner |
| 145 |
self.n = n |
| 146 |
self.cache = list() |
| 147 |
self.cachecomplete = False |
| 148 |
|
| 149 |
def clearcache(self): |
| 150 |
self.cache = list() |
| 151 |
self.cachecomplete = False |
| 152 |
|
| 153 |
def __iter__(self): |
| 154 |
|
| 155 |
# serve whatever is in the cache first |
| 156 |
for row in self.cache: |
| 157 |
yield row |
| 158 |
|
| 159 |
if not self.cachecomplete: |
| 160 |
|
| 161 |
# serve the remainder from the inner iterator |
| 162 |
it = iter(self.inner) |
| 163 |
for row in islice(it, len(self.cache), None): |
| 164 |
# maybe there's more room in the cache? |
| 165 |
if not self.n or len(self.cache) < self.n: |
| 166 |
self.cache.append(row) |
| 167 |
yield row |
| 168 |
|
| 169 |
# does the cache contain a complete copy of the inner table? |
| 170 |
if not self.n or len(self.cache) < self.n: |
| 171 |
self.cachecomplete = True |