| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from petl.compat import next |
| 7 |
from petl.errors import ArgumentError |
| 8 |
from petl.test.helpers import ieq, eq_ |
| 9 |
from petl.transform.regex import capture, split, search, searchcomplement, splitdown |
| 10 |
from petl.transform.basics import TransformError |
| 11 |
|
| 12 |
|
| 13 |
def test_capture(): |
| 14 |
|
| 15 |
table = (('id', 'variable', 'value'), |
| 16 |
('1', 'A1', '12'), |
| 17 |
('2', 'A2', '15'), |
| 18 |
('3', 'B1', '18'), |
| 19 |
('4', 'C12', '19')) |
| 20 |
|
| 21 |
expectation = (('id', 'value', 'treat', 'time'), |
| 22 |
('1', '12', 'A', '1'), |
| 23 |
('2', '15', 'A', '2'), |
| 24 |
('3', '18', 'B', '1'), |
| 25 |
('4', '19', 'C', '12')) |
| 26 |
|
| 27 |
result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time')) |
| 28 |
ieq(expectation, result) |
| 29 |
result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time'), |
| 30 |
include_original=False) |
| 31 |
ieq(expectation, result) |
| 32 |
|
| 33 |
# what about including the original field? |
| 34 |
expectation = (('id', 'variable', 'value', 'treat', 'time'), |
| 35 |
('1', 'A1', '12', 'A', '1'), |
| 36 |
('2', 'A2', '15', 'A', '2'), |
| 37 |
('3', 'B1', '18', 'B', '1'), |
| 38 |
('4', 'C12', '19', 'C', '12')) |
| 39 |
result = capture(table, 'variable', '(\\w)(\\d+)', ('treat', 'time'), |
| 40 |
include_original=True) |
| 41 |
ieq(expectation, result) |
| 42 |
|
| 43 |
# what about if number of captured groups is different from new fields? |
| 44 |
expectation = (('id', 'value'), |
| 45 |
('1', '12', 'A', '1'), |
| 46 |
('2', '15', 'A', '2'), |
| 47 |
('3', '18', 'B', '1'), |
| 48 |
('4', '19', 'C', '12')) |
| 49 |
result = capture(table, 'variable', '(\\w)(\\d+)') |
| 50 |
ieq(expectation, result) |
| 51 |
|
| 52 |
|
| 53 |
def test_capture_empty(): |
| 54 |
table = (('foo', 'bar'),) |
| 55 |
expect = (('foo', 'baz', 'qux'),) |
| 56 |
actual = capture(table, 'bar', r'(\w)(\d)', ('baz', 'qux')) |
| 57 |
ieq(expect, actual) |
| 58 |
|
| 59 |
|
| 60 |
def test_capture_headerless(): |
| 61 |
table = [] |
| 62 |
with pytest.raises(ArgumentError): |
| 63 |
for i in capture(table, 'bar', r'(\w)(\d)', ('baz', 'qux')): |
| 64 |
pass |
| 65 |
|
| 66 |
|
| 67 |
def test_capture_nonmatching(): |
| 68 |
|
| 69 |
table = (('id', 'variable', 'value'), |
| 70 |
('1', 'A1', '12'), |
| 71 |
('2', 'A2', '15'), |
| 72 |
('3', 'B1', '18'), |
| 73 |
('4', 'C12', '19')) |
| 74 |
|
| 75 |
expectation = (('id', 'value', 'treat', 'time'), |
| 76 |
('1', '12', 'A', '1'), |
| 77 |
('2', '15', 'A', '2'), |
| 78 |
('3', '18', 'B', '1')) |
| 79 |
|
| 80 |
# default behaviour, raise exception |
| 81 |
result = capture(table, 'variable', r'([A-B])(\d+)', ('treat', 'time')) |
| 82 |
it = iter(result) |
| 83 |
eq_(expectation[0], next(it)) # header |
| 84 |
eq_(expectation[1], next(it)) |
| 85 |
eq_(expectation[2], next(it)) |
| 86 |
eq_(expectation[3], next(it)) |
| 87 |
try: |
| 88 |
next(it) # doesn't match |
| 89 |
except TransformError: |
| 90 |
pass # expected |
| 91 |
else: |
| 92 |
assert False, 'expected exception' |
| 93 |
|
| 94 |
# explicit fill |
| 95 |
result = capture(table, 'variable', r'([A-B])(\d+)', |
| 96 |
newfields=('treat', 'time'), fill=['', 0]) |
| 97 |
it = iter(result) |
| 98 |
eq_(expectation[0], next(it)) # header |
| 99 |
eq_(expectation[1], next(it)) |
| 100 |
eq_(expectation[2], next(it)) |
| 101 |
eq_(expectation[3], next(it)) |
| 102 |
eq_(('4', '19', '', 0), next(it)) |
| 103 |
|
| 104 |
|
| 105 |
def test_split(): |
| 106 |
|
| 107 |
table = (('id', 'variable', 'value'), |
| 108 |
('1', 'parad1', '12'), |
| 109 |
('2', 'parad2', '15'), |
| 110 |
('3', 'tempd1', '18'), |
| 111 |
('4', 'tempd2', '19')) |
| 112 |
|
| 113 |
expectation = (('id', 'value', 'variable', 'day'), |
| 114 |
('1', '12', 'para', '1'), |
| 115 |
('2', '15', 'para', '2'), |
| 116 |
('3', '18', 'temp', '1'), |
| 117 |
('4', '19', 'temp', '2')) |
| 118 |
|
| 119 |
result = split(table, 'variable', 'd', ('variable', 'day')) |
| 120 |
ieq(expectation, result) |
| 121 |
ieq(expectation, result) |
| 122 |
|
| 123 |
# proper regex |
| 124 |
result = split(table, 'variable', '[Dd]', ('variable', 'day')) |
| 125 |
ieq(expectation, result) |
| 126 |
|
| 127 |
# integer field reference |
| 128 |
result = split(table, 1, 'd', ('variable', 'day')) |
| 129 |
ieq(expectation, result) |
| 130 |
|
| 131 |
expectation = (('id', 'variable', 'value', 'variable', 'day'), |
| 132 |
('1', 'parad1', '12', 'para', '1'), |
| 133 |
('2', 'parad2', '15', 'para', '2'), |
| 134 |
('3', 'tempd1', '18', 'temp', '1'), |
| 135 |
('4', 'tempd2', '19', 'temp', '2')) |
| 136 |
|
| 137 |
result = split(table, 'variable', 'd', ('variable', 'day'), |
| 138 |
include_original=True) |
| 139 |
ieq(expectation, result) |
| 140 |
|
| 141 |
# what about if no new fields? |
| 142 |
expectation = (('id', 'value'), |
| 143 |
('1', '12', 'para', '1'), |
| 144 |
('2', '15', 'para', '2'), |
| 145 |
('3', '18', 'temp', '1'), |
| 146 |
('4', '19', 'temp', '2')) |
| 147 |
result = split(table, 'variable', 'd') |
| 148 |
ieq(expectation, result) |
| 149 |
|
| 150 |
|
| 151 |
def test_split_empty(): |
| 152 |
table = (('foo', 'bar'),) |
| 153 |
expect = (('foo', 'baz', 'qux'),) |
| 154 |
actual = split(table, 'bar', 'd', ('baz', 'qux')) |
| 155 |
ieq(expect, actual) |
| 156 |
|
| 157 |
|
| 158 |
def test_split_headerless(): |
| 159 |
table = [] |
| 160 |
with pytest.raises(ArgumentError): |
| 161 |
for i in split(table, 'bar', 'd', ('baz', 'qux')): |
| 162 |
pass |
| 163 |
|
| 164 |
|
| 165 |
def test_search(): |
| 166 |
|
| 167 |
table1 = (('foo', 'bar', 'baz'), |
| 168 |
('orange', 12, 'oranges are nice fruit'), |
| 169 |
('mango', 42, 'I like them'), |
| 170 |
('banana', 74, 'lovely too'), |
| 171 |
('cucumber', 41, 'better than mango')) |
| 172 |
|
| 173 |
# search any field |
| 174 |
table2 = search(table1, '.g.') |
| 175 |
expect2 = (('foo', 'bar', 'baz'), |
| 176 |
('orange', 12, 'oranges are nice fruit'), |
| 177 |
('mango', 42, 'I like them'), |
| 178 |
('cucumber', 41, 'better than mango')) |
| 179 |
ieq(expect2, table2) |
| 180 |
ieq(expect2, table2) |
| 181 |
|
| 182 |
# search a specific field |
| 183 |
table3 = search(table1, 'foo', '.g.') |
| 184 |
expect3 = (('foo', 'bar', 'baz'), |
| 185 |
('orange', 12, 'oranges are nice fruit'), |
| 186 |
('mango', 42, 'I like them')) |
| 187 |
ieq(expect3, table3) |
| 188 |
ieq(expect3, table3) |
| 189 |
|
| 190 |
|
| 191 |
def test_search_2(): |
| 192 |
|
| 193 |
# test ported from selectre |
| 194 |
table = (('foo', 'bar', 'baz'), |
| 195 |
('aa', 4, 9.3), |
| 196 |
('aaa', 2, 88.2), |
| 197 |
('b', 1, 23.3), |
| 198 |
('ccc', 8, 42.0), |
| 199 |
('bb', 7, 100.9), |
| 200 |
('c', 2)) |
| 201 |
actual = search(table, 'foo', '[ab]{2}') |
| 202 |
expect = (('foo', 'bar', 'baz'), |
| 203 |
('aa', 4, 9.3), |
| 204 |
('aaa', 2, 88.2), |
| 205 |
('bb', 7, 100.9)) |
| 206 |
ieq(expect, actual) |
| 207 |
ieq(expect, actual) |
| 208 |
|
| 209 |
|
| 210 |
def test_search_headerless(): |
| 211 |
table = [] |
| 212 |
actual = search(table, 'foo', '[ab]{2}') |
| 213 |
expect = [] |
| 214 |
ieq(expect, actual) |
| 215 |
ieq(expect, actual) |
| 216 |
|
| 217 |
|
| 218 |
def test_searchcomplement(): |
| 219 |
|
| 220 |
table1 = (('foo', 'bar', 'baz'), |
| 221 |
('orange', 12, 'oranges are nice fruit'), |
| 222 |
('mango', 42, 'I like them'), |
| 223 |
('banana', 74, 'lovely too'), |
| 224 |
('cucumber', 41, 'better than mango')) |
| 225 |
|
| 226 |
# search any field |
| 227 |
table2 = searchcomplement(table1, '.g.') |
| 228 |
expect2 = (('foo', 'bar', 'baz'), |
| 229 |
('banana', 74, 'lovely too')) |
| 230 |
ieq(expect2, table2) |
| 231 |
ieq(expect2, table2) |
| 232 |
|
| 233 |
# search a specific field |
| 234 |
table3 = searchcomplement(table1, 'foo', '.g.') |
| 235 |
expect3 = (('foo', 'bar', 'baz'), |
| 236 |
('banana', 74, 'lovely too'), |
| 237 |
('cucumber', 41, 'better than mango')) |
| 238 |
ieq(expect3, table3) |
| 239 |
ieq(expect3, table3) |
| 240 |
|
| 241 |
# search any field, using complement |
| 242 |
table2 = search(table1, '.g.', complement=True) |
| 243 |
expect2 = (('foo', 'bar', 'baz'), |
| 244 |
('banana', 74, 'lovely too')) |
| 245 |
ieq(expect2, table2) |
| 246 |
ieq(expect2, table2) |
| 247 |
|
| 248 |
# search a specific field, using complement |
| 249 |
table3 = search(table1, 'foo', '.g.', complement=True) |
| 250 |
expect3 = (('foo', 'bar', 'baz'), |
| 251 |
('banana', 74, 'lovely too'), |
| 252 |
('cucumber', 41, 'better than mango')) |
| 253 |
ieq(expect3, table3) |
| 254 |
ieq(expect3, table3) |
| 255 |
|
| 256 |
|
| 257 |
def test_search_unicode(): |
| 258 |
tbl = ((u'name', u'id'), |
| 259 |
(u'Արամ Խաչատրյան', 1), |
| 260 |
(u'Johann Strauß', 2), |
| 261 |
(u'Вагиф Сәмәдоғлу', 3), |
| 262 |
(u'章子怡', 4)) |
| 263 |
actual = search(tbl, u'.Խա.') |
| 264 |
expect = ((u'name', u'id'), |
| 265 |
(u'Արամ Խաչատրյան', 1)) |
| 266 |
ieq(expect, actual) |
| 267 |
ieq(expect, actual) |
| 268 |
|
| 269 |
|
| 270 |
def test_splitdown(): |
| 271 |
|
| 272 |
tbl = ((u'name', u'roles'), |
| 273 |
(u'Jane Doe', u'president,engineer,tailor,lawyer'), |
| 274 |
(u'John Doe', u'rocket scientist,optometrist,chef,knight,sailor')) |
| 275 |
|
| 276 |
actual = splitdown(tbl, 'roles', ',') |
| 277 |
expect = ((u'name', u'roles'), |
| 278 |
(u'Jane Doe', u'president'), |
| 279 |
(u'Jane Doe', u'engineer'), |
| 280 |
(u'Jane Doe', u'tailor'), |
| 281 |
(u'Jane Doe', u'lawyer'), |
| 282 |
(u'John Doe', u'rocket scientist'), |
| 283 |
(u'John Doe', u'optometrist'), |
| 284 |
(u'John Doe', u'chef'), |
| 285 |
(u'John Doe', u'knight'), |
| 286 |
(u'John Doe', u'sailor')) |
| 287 |
|
| 288 |
ieq(expect, actual) |
| 289 |
ieq(expect, actual) |
| 290 |
ieq(expect, actual) |
| 291 |
ieq(expect, actual) |
| 292 |
ieq(expect, actual) |
| 293 |
ieq(expect, actual) |
| 294 |
ieq(expect, actual) |
| 295 |
ieq(expect, actual) |
| 296 |
ieq(expect, actual) |
| 297 |
ieq(expect, actual) |
| 298 |
|
| 299 |
|
| 300 |
# TODO test sub() |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
def test_capture_none_value(): |
| 305 |
table = (('id', 'variable'), |
| 306 |
('1', None), |
| 307 |
('2', 'A1')) |
| 308 |
result = capture(table, 'variable', r'(\w)(\d+)', ('treat', 'time'), |
| 309 |
fill=['', '']) |
| 310 |
expect = (('id', 'treat', 'time'), |
| 311 |
('1', '', ''), |
| 312 |
('2', 'A', '1')) |
| 313 |
ieq(expect, result) |