| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
import tempfile |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from petl.test.helpers import ieq, eq_ |
| 8 |
from petl.io.bcolz import frombcolz, tobcolz, appendbcolz |
| 9 |
|
| 10 |
|
| 11 |
try: |
| 12 |
import bcolz |
| 13 |
except ImportError as e: |
| 14 |
pytest.skip('SKIP bcolz tests: %s' % e, allow_module_level=True) |
| 15 |
else: |
| 16 |
|
| 17 |
def test_frombcolz(): |
| 18 |
|
| 19 |
cols = [ |
| 20 |
['apples', 'oranges', 'pears'], |
| 21 |
[1, 3, 7], |
| 22 |
[2.5, 4.4, .1] |
| 23 |
] |
| 24 |
names = ('foo', 'bar', 'baz') |
| 25 |
rootdir = tempfile.mkdtemp() |
| 26 |
ctbl = bcolz.ctable(cols, names=names, rootdir=rootdir, mode='w') |
| 27 |
ctbl.flush() |
| 28 |
|
| 29 |
expect = [names] + list(zip(*cols)) |
| 30 |
|
| 31 |
# from ctable object |
| 32 |
actual = frombcolz(ctbl) |
| 33 |
ieq(expect, actual) |
| 34 |
ieq(expect, actual) |
| 35 |
|
| 36 |
# from rootdir |
| 37 |
actual = frombcolz(rootdir) |
| 38 |
ieq(expect, actual) |
| 39 |
ieq(expect, actual) |
| 40 |
|
| 41 |
def test_tobcolz(): |
| 42 |
t = [('foo', 'bar', 'baz'), |
| 43 |
('apples', 1, 2.5), |
| 44 |
('oranges', 3, 4.4), |
| 45 |
('pears', 7, .1)] |
| 46 |
|
| 47 |
ctbl = tobcolz(t) |
| 48 |
assert isinstance(ctbl, bcolz.ctable) |
| 49 |
eq_(t[0], tuple(ctbl.names)) |
| 50 |
ieq(t[1:], (tuple(r) for r in ctbl.iter())) |
| 51 |
|
| 52 |
ctbl = tobcolz(t, chunklen=2) |
| 53 |
assert isinstance(ctbl, bcolz.ctable) |
| 54 |
eq_(t[0], tuple(ctbl.names)) |
| 55 |
ieq(t[1:], (tuple(r) for r in ctbl.iter())) |
| 56 |
eq_(2, ctbl.cols[ctbl.names[0]].chunklen) |
| 57 |
|
| 58 |
def test_appendbcolz(): |
| 59 |
t = [('foo', 'bar', 'baz'), |
| 60 |
('apples', 1, 2.5), |
| 61 |
('oranges', 3, 4.4), |
| 62 |
('pears', 7, .1)] |
| 63 |
|
| 64 |
# append to in-memory ctable |
| 65 |
ctbl = tobcolz(t) |
| 66 |
appendbcolz(t, ctbl) |
| 67 |
eq_(t[0], tuple(ctbl.names)) |
| 68 |
ieq(t[1:] + t[1:], (tuple(r) for r in ctbl.iter())) |
| 69 |
|
| 70 |
# append to on-disk ctable |
| 71 |
rootdir = tempfile.mkdtemp() |
| 72 |
tobcolz(t, rootdir=rootdir) |
| 73 |
appendbcolz(t, rootdir) |
| 74 |
ctbl = bcolz.open(rootdir, mode='r') |
| 75 |
eq_(t[0], tuple(ctbl.names)) |
| 76 |
ieq(t[1:] + t[1:], (tuple(r) for r in ctbl.iter())) |