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