| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
|
| 4 |
import os |
| 5 |
import gc |
| 6 |
import logging |
| 7 |
from datetime import datetime |
| 8 |
import platform |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from petl.compat import next |
| 13 |
|
| 14 |
from petl.errors import FieldSelectionError |
| 15 |
from petl.test.helpers import ieq, eq_ |
| 16 |
from petl.util import nrows |
| 17 |
from petl.transform.basics import cat |
| 18 |
from petl.transform.sorts import sort, mergesort, issorted |
| 19 |
|
| 20 |
|
| 21 |
logger = logging.getLogger(__name__) |
| 22 |
debug = logger.debug |
| 23 |
|
| 24 |
|
| 25 |
def test_sort_1(): |
| 26 |
|
| 27 |
table = (('foo', 'bar'), |
| 28 |
('C', '2'), |
| 29 |
('A', '9'), |
| 30 |
('A', '6'), |
| 31 |
('F', '1'), |
| 32 |
('D', '10')) |
| 33 |
|
| 34 |
result = sort(table, 'foo') |
| 35 |
expectation = (('foo', 'bar'), |
| 36 |
('A', '9'), |
| 37 |
('A', '6'), |
| 38 |
('C', '2'), |
| 39 |
('D', '10'), |
| 40 |
('F', '1')) |
| 41 |
ieq(expectation, result) |
| 42 |
|
| 43 |
|
| 44 |
def test_sort_2(): |
| 45 |
|
| 46 |
table = (('foo', 'bar'), |
| 47 |
('C', '2'), |
| 48 |
('A', '9'), |
| 49 |
('A', '6'), |
| 50 |
('F', '1'), |
| 51 |
('D', '10')) |
| 52 |
|
| 53 |
result = sort(table, key=('foo', 'bar')) |
| 54 |
expectation = (('foo', 'bar'), |
| 55 |
('A', '6'), |
| 56 |
('A', '9'), |
| 57 |
('C', '2'), |
| 58 |
('D', '10'), |
| 59 |
('F', '1')) |
| 60 |
ieq(expectation, result) |
| 61 |
|
| 62 |
result = sort(table) # default is lexical sort |
| 63 |
expectation = (('foo', 'bar'), |
| 64 |
('A', '6'), |
| 65 |
('A', '9'), |
| 66 |
('C', '2'), |
| 67 |
('D', '10'), |
| 68 |
('F', '1')) |
| 69 |
ieq(expectation, result) |
| 70 |
|
| 71 |
|
| 72 |
def test_sort_3(): |
| 73 |
|
| 74 |
table = (('foo', 'bar'), |
| 75 |
('C', '2'), |
| 76 |
('A', '9'), |
| 77 |
('A', '6'), |
| 78 |
('F', '1'), |
| 79 |
('D', '10')) |
| 80 |
|
| 81 |
result = sort(table, 'bar') |
| 82 |
expectation = (('foo', 'bar'), |
| 83 |
('F', '1'), |
| 84 |
('D', '10'), |
| 85 |
('C', '2'), |
| 86 |
('A', '6'), |
| 87 |
('A', '9')) |
| 88 |
ieq(expectation, result) |
| 89 |
|
| 90 |
|
| 91 |
def test_sort_4(): |
| 92 |
|
| 93 |
table = (('foo', 'bar'), |
| 94 |
('C', 2), |
| 95 |
('A', 9), |
| 96 |
('A', 6), |
| 97 |
('F', 1), |
| 98 |
('D', 10)) |
| 99 |
|
| 100 |
result = sort(table, 'bar') |
| 101 |
expectation = (('foo', 'bar'), |
| 102 |
('F', 1), |
| 103 |
('C', 2), |
| 104 |
('A', 6), |
| 105 |
('A', 9), |
| 106 |
('D', 10)) |
| 107 |
ieq(expectation, result) |
| 108 |
|
| 109 |
|
| 110 |
def test_sort_5(): |
| 111 |
|
| 112 |
table = (('foo', 'bar'), |
| 113 |
(2.3, 2), |
| 114 |
(1.2, 9), |
| 115 |
(2.3, 6), |
| 116 |
(3.2, 1), |
| 117 |
(1.2, 10)) |
| 118 |
|
| 119 |
expectation = (('foo', 'bar'), |
| 120 |
(1.2, 9), |
| 121 |
(1.2, 10), |
| 122 |
(2.3, 2), |
| 123 |
(2.3, 6), |
| 124 |
(3.2, 1)) |
| 125 |
|
| 126 |
# can use either field names or indices (from 1) to specify sort key |
| 127 |
result = sort(table, key=('foo', 'bar')) |
| 128 |
ieq(expectation, result) |
| 129 |
result = sort(table, key=(0, 1)) |
| 130 |
ieq(expectation, result) |
| 131 |
result = sort(table, key=('foo', 1)) |
| 132 |
ieq(expectation, result) |
| 133 |
result = sort(table, key=(0, 'bar')) |
| 134 |
ieq(expectation, result) |
| 135 |
|
| 136 |
|
| 137 |
def test_sort_6(): |
| 138 |
|
| 139 |
table = (('foo', 'bar'), |
| 140 |
(2.3, 2), |
| 141 |
(1.2, 9), |
| 142 |
(2.3, 6), |
| 143 |
(3.2, 1), |
| 144 |
(1.2, 10)) |
| 145 |
|
| 146 |
expectation = (('foo', 'bar'), |
| 147 |
(3.2, 1), |
| 148 |
(2.3, 6), |
| 149 |
(2.3, 2), |
| 150 |
(1.2, 10), |
| 151 |
(1.2, 9)) |
| 152 |
|
| 153 |
result = sort(table, key=('foo', 'bar'), reverse=True) |
| 154 |
ieq(expectation, result) |
| 155 |
|
| 156 |
|
| 157 |
def test_sort_buffered(): |
| 158 |
|
| 159 |
table = (('foo', 'bar'), |
| 160 |
('C', 2), |
| 161 |
('A', 9), |
| 162 |
('A', 6), |
| 163 |
('F', 1), |
| 164 |
('D', 10)) |
| 165 |
|
| 166 |
# test sort forwards |
| 167 |
expectation = (('foo', 'bar'), |
| 168 |
('F', 1), |
| 169 |
('C', 2), |
| 170 |
('A', 6), |
| 171 |
('A', 9), |
| 172 |
('D', 10)) |
| 173 |
result = sort(table, 'bar') |
| 174 |
ieq(expectation, result) |
| 175 |
result = sort(table, 'bar', buffersize=2) |
| 176 |
ieq(expectation, result) |
| 177 |
|
| 178 |
# sort in reverse |
| 179 |
expectation = (('foo', 'bar'), |
| 180 |
('D', 10), |
| 181 |
('A', 9), |
| 182 |
('A', 6), |
| 183 |
('C', 2), |
| 184 |
('F', 1)) |
| 185 |
|
| 186 |
result = sort(table, 'bar', reverse=True) |
| 187 |
ieq(expectation, result) |
| 188 |
result = sort(table, 'bar', reverse=True, buffersize=2) |
| 189 |
ieq(expectation, result) |
| 190 |
|
| 191 |
# no key |
| 192 |
expectation = (('foo', 'bar'), |
| 193 |
('F', 1), |
| 194 |
('D', 10), |
| 195 |
('C', 2), |
| 196 |
('A', 9), |
| 197 |
('A', 6)) |
| 198 |
result = sort(table, reverse=True) |
| 199 |
ieq(expectation, result) |
| 200 |
result = sort(table, reverse=True, buffersize=2) |
| 201 |
ieq(expectation, result) |
| 202 |
|
| 203 |
|
| 204 |
def test_sort_buffered_tempdir(): |
| 205 |
|
| 206 |
table = (('foo', 'bar'), |
| 207 |
('C', 2), |
| 208 |
('A', 9), |
| 209 |
('A', 6), |
| 210 |
('F', 1), |
| 211 |
('D', 10)) |
| 212 |
|
| 213 |
# test sort forwards |
| 214 |
expectation = (('foo', 'bar'), |
| 215 |
('F', 1), |
| 216 |
('C', 2), |
| 217 |
('A', 6), |
| 218 |
('A', 9), |
| 219 |
('D', 10)) |
| 220 |
result = sort(table, 'bar') |
| 221 |
ieq(expectation, result) |
| 222 |
tempdir = 'tmp' |
| 223 |
if not os.path.exists(tempdir): |
| 224 |
os.mkdir(tempdir) |
| 225 |
result = sort(table, 'bar', buffersize=2, tempdir=tempdir) |
| 226 |
ieq(expectation, result) |
| 227 |
|
| 228 |
|
| 229 |
def test_sort_buffered_independent(): |
| 230 |
|
| 231 |
table = (('foo', 'bar'), |
| 232 |
('C', 2), |
| 233 |
('A', 9), |
| 234 |
('A', 6), |
| 235 |
('F', 1), |
| 236 |
('D', 10)) |
| 237 |
expectation = (('foo', 'bar'), |
| 238 |
('F', 1), |
| 239 |
('C', 2), |
| 240 |
('A', 6), |
| 241 |
('A', 9), |
| 242 |
('D', 10)) |
| 243 |
|
| 244 |
result = sort(table, 'bar', buffersize=4) |
| 245 |
nrows(result) # cause data to be cached |
| 246 |
# check that two row iterators are independent, i.e., consuming rows |
| 247 |
# from one does not affect the other |
| 248 |
it1 = iter(result) |
| 249 |
it2 = iter(result) |
| 250 |
eq_(expectation[0], next(it1)) |
| 251 |
eq_(expectation[1], next(it1)) |
| 252 |
eq_(expectation[0], next(it2)) |
| 253 |
eq_(expectation[1], next(it2)) |
| 254 |
eq_(expectation[2], next(it2)) |
| 255 |
eq_(expectation[2], next(it1)) |
| 256 |
|
| 257 |
|
| 258 |
def _get_names(l): |
| 259 |
return [x.name for x in l] |
| 260 |
|
| 261 |
|
| 262 |
def test_sort_buffered_cleanup(): |
| 263 |
|
| 264 |
table = (('foo', 'bar'), |
| 265 |
('C', 2), |
| 266 |
('A', 9), |
| 267 |
('A', 6), |
| 268 |
('F', 1), |
| 269 |
('D', 10)) |
| 270 |
result = sort(table, 'bar', buffersize=2) |
| 271 |
debug('initially filecache should be empty') |
| 272 |
eq_(None, result._filecache) |
| 273 |
debug('pull rows through, should populate file cache') |
| 274 |
eq_(5, nrows(result)) |
| 275 |
eq_(3, len(result._filecache)) |
| 276 |
debug('check all files exist') |
| 277 |
filenames = _get_names(result._filecache) |
| 278 |
for fn in filenames: |
| 279 |
assert os.path.exists(fn), fn |
| 280 |
debug('delete object and garbage collect') |
| 281 |
del result |
| 282 |
gc.collect() |
| 283 |
debug('check all files have been deleted') |
| 284 |
for fn in filenames: |
| 285 |
assert not os.path.exists(fn), fn |
| 286 |
|
| 287 |
|
| 288 |
@pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='SKIP sort cleanup test (PyPy)') |
| 289 |
def test_sort_buffered_cleanup_open_iterator(): |
| 290 |
|
| 291 |
table = (('foo', 'bar'), |
| 292 |
('C', 2), |
| 293 |
('A', 9), |
| 294 |
('A', 6), |
| 295 |
('F', 1), |
| 296 |
('D', 10)) |
| 297 |
# check if cleanup is robust against open iterators |
| 298 |
result = sort(table, 'bar', buffersize=2) |
| 299 |
debug('pull rows through, should populate file cache') |
| 300 |
eq_(5, nrows(result)) |
| 301 |
eq_(3, len(result._filecache)) |
| 302 |
debug('check all files exist') |
| 303 |
filenames = _get_names(result._filecache) |
| 304 |
for fn in filenames: |
| 305 |
assert os.path.exists(fn), fn |
| 306 |
debug(filenames) |
| 307 |
debug('open an iterator') |
| 308 |
it = iter(result) |
| 309 |
next(it) |
| 310 |
next(it) |
| 311 |
debug('delete objects and garbage collect') |
| 312 |
del result |
| 313 |
del it |
| 314 |
gc.collect() |
| 315 |
for fn in filenames: |
| 316 |
assert not os.path.exists(fn), fn |
| 317 |
|
| 318 |
|
| 319 |
def test_sort_empty(): |
| 320 |
table = (('foo', 'bar'),) |
| 321 |
expect = (('foo', 'bar'),) |
| 322 |
actual = sort(table) |
| 323 |
ieq(expect, actual) |
| 324 |
|
| 325 |
|
| 326 |
def test_sort_none(): |
| 327 |
|
| 328 |
table = (('foo', 'bar'), |
| 329 |
('C', 2), |
| 330 |
('A', 9), |
| 331 |
('A', None), |
| 332 |
('F', 1), |
| 333 |
('D', 10)) |
| 334 |
|
| 335 |
result = sort(table, 'bar') |
| 336 |
print(list(result)) |
| 337 |
expectation = (('foo', 'bar'), |
| 338 |
('A', None), |
| 339 |
('F', 1), |
| 340 |
('C', 2), |
| 341 |
('A', 9), |
| 342 |
('D', 10)) |
| 343 |
ieq(expectation, result) |
| 344 |
|
| 345 |
dt = datetime.now().replace |
| 346 |
|
| 347 |
table = (('foo', 'bar'), |
| 348 |
('C', dt(hour=5)), |
| 349 |
('A', dt(hour=1)), |
| 350 |
('A', None), |
| 351 |
('F', dt(hour=9)), |
| 352 |
('D', dt(hour=17))) |
| 353 |
|
| 354 |
result = sort(table, 'bar') |
| 355 |
expectation = (('foo', 'bar'), |
| 356 |
('A', None), |
| 357 |
('A', dt(hour=1)), |
| 358 |
('C', dt(hour=5)), |
| 359 |
('F', dt(hour=9)), |
| 360 |
('D', dt(hour=17))) |
| 361 |
ieq(expectation, result) |
| 362 |
|
| 363 |
|
| 364 |
def test_sort_headerless_no_keys(): |
| 365 |
""" |
| 366 |
Sorting a headerless table without specifying cols should be a no-op. |
| 367 |
""" |
| 368 |
table = [] |
| 369 |
result = sort(table) |
| 370 |
expectation = [] |
| 371 |
ieq(expectation, result) |
| 372 |
|
| 373 |
|
| 374 |
def test_sort_headerless_explicit(): |
| 375 |
""" |
| 376 |
But if you specify keys, they must exist. |
| 377 |
""" |
| 378 |
table = [] |
| 379 |
with pytest.raises(FieldSelectionError): |
| 380 |
for i in sort(table, 'foo'): |
| 381 |
pass |
| 382 |
|
| 383 |
# TODO test sort with native comparison |
| 384 |
|
| 385 |
|
| 386 |
def test_mergesort_1(): |
| 387 |
|
| 388 |
table1 = (('foo', 'bar'), |
| 389 |
('A', 6), |
| 390 |
('C', 2), |
| 391 |
('D', 10), |
| 392 |
('A', 9), |
| 393 |
('F', 1)) |
| 394 |
|
| 395 |
table2 = (('foo', 'bar'), |
| 396 |
('B', 3), |
| 397 |
('D', 10), |
| 398 |
('A', 10), |
| 399 |
('F', 4)) |
| 400 |
|
| 401 |
# should be same as concatenate then sort (but more efficient, esp. when |
| 402 |
# presorted) |
| 403 |
expect = sort(cat(table1, table2)) |
| 404 |
|
| 405 |
actual = mergesort(table1, table2) |
| 406 |
ieq(expect, actual) |
| 407 |
ieq(expect, actual) |
| 408 |
|
| 409 |
actual = mergesort(sort(table1), sort(table2), presorted=True) |
| 410 |
ieq(expect, actual) |
| 411 |
ieq(expect, actual) |
| 412 |
|
| 413 |
|
| 414 |
def test_mergesort_2(): |
| 415 |
|
| 416 |
table1 = (('foo', 'bar'), |
| 417 |
('A', 9), |
| 418 |
('C', 2), |
| 419 |
('D', 10), |
| 420 |
('A', 6), |
| 421 |
('F', 1)) |
| 422 |
|
| 423 |
table2 = (('foo', 'baz'), |
| 424 |
('B', 3), |
| 425 |
('D', 10), |
| 426 |
('A', 10), |
| 427 |
('F', 4)) |
| 428 |
|
| 429 |
# should be same as concatenate then sort (but more efficient, esp. when |
| 430 |
# presorted) |
| 431 |
expect = sort(cat(table1, table2), key='foo') |
| 432 |
|
| 433 |
actual = mergesort(table1, table2, key='foo') |
| 434 |
ieq(expect, actual) |
| 435 |
ieq(expect, actual) |
| 436 |
|
| 437 |
actual = mergesort(sort(table1, key='foo'), sort(table2, key='foo'), |
| 438 |
key='foo', presorted=True) |
| 439 |
ieq(expect, actual) |
| 440 |
ieq(expect, actual) |
| 441 |
|
| 442 |
|
| 443 |
def test_mergesort_3(): |
| 444 |
|
| 445 |
table1 = (('foo', 'bar'), |
| 446 |
('A', 9), |
| 447 |
('C', 2), |
| 448 |
('D', 10), |
| 449 |
('A', 6), |
| 450 |
('F', 1)) |
| 451 |
|
| 452 |
table2 = (('foo', 'baz'), |
| 453 |
('B', 3), |
| 454 |
('D', 10), |
| 455 |
('A', 10), |
| 456 |
('F', 4)) |
| 457 |
|
| 458 |
# should be same as concatenate then sort (but more efficient, esp. when |
| 459 |
# presorted) |
| 460 |
expect = sort(cat(table1, table2), key='foo', reverse=True) |
| 461 |
|
| 462 |
actual = mergesort(table1, table2, key='foo', reverse=True) |
| 463 |
ieq(expect, actual) |
| 464 |
ieq(expect, actual) |
| 465 |
|
| 466 |
actual = mergesort(sort(table1, key='foo', reverse=True), |
| 467 |
sort(table2, key='foo', reverse=True), |
| 468 |
key='foo', reverse=True, presorted=True) |
| 469 |
ieq(expect, actual) |
| 470 |
ieq(expect, actual) |
| 471 |
|
| 472 |
|
| 473 |
def test_mergesort_4(): |
| 474 |
|
| 475 |
table1 = (('foo', 'bar', 'baz'), |
| 476 |
(1, 'A', True), |
| 477 |
(2, 'B', None), |
| 478 |
(4, 'C', True)) |
| 479 |
table2 = (('bar', 'baz', 'quux'), |
| 480 |
('A', True, 42.0), |
| 481 |
('B', False, 79.3), |
| 482 |
('C', False, 12.4)) |
| 483 |
|
| 484 |
expect = sort(cat(table1, table2), key='bar') |
| 485 |
|
| 486 |
actual = mergesort(table1, table2, key='bar') |
| 487 |
ieq(expect, actual) |
| 488 |
ieq(expect, actual) |
| 489 |
|
| 490 |
|
| 491 |
def test_mergesort_empty(): |
| 492 |
|
| 493 |
table1 = (('foo', 'bar'), |
| 494 |
('A', 9), |
| 495 |
('C', 2), |
| 496 |
('D', 10), |
| 497 |
('F', 1)) |
| 498 |
|
| 499 |
table2 = (('foo', 'bar'),) |
| 500 |
|
| 501 |
expect = table1 |
| 502 |
actual = mergesort(table1, table2, key='foo') |
| 503 |
ieq(expect, actual) |
| 504 |
ieq(expect, actual) |
| 505 |
|
| 506 |
|
| 507 |
def test_issorted(): |
| 508 |
|
| 509 |
table1 = (('foo', 'bar', 'baz'), |
| 510 |
('a', 1, True), |
| 511 |
('b', 3, True), |
| 512 |
('b', 2)) |
| 513 |
assert issorted(table1, key='foo') |
| 514 |
assert not issorted(table1, key='foo', reverse=True) |
| 515 |
assert not issorted(table1, key='foo', strict=True) |
| 516 |
|
| 517 |
table2 = (('foo', 'bar', 'baz'), |
| 518 |
('b', 2, True), |
| 519 |
('a', 1, True), |
| 520 |
('b', 3)) |
| 521 |
assert not issorted(table2, key='foo') |
| 522 |
|
| 523 |
table3 = (('foo', 'bar', 'baz'), |
| 524 |
('a', 1, True), |
| 525 |
('b', 2, True), |
| 526 |
('b', 3)) |
| 527 |
assert issorted(table3, key=('foo', 'bar')) |
| 528 |
assert issorted(table3) |
| 529 |
|
| 530 |
table4 = (('foo', 'bar', 'baz'), |
| 531 |
('a', 1, True), |
| 532 |
('b', 3, True), |
| 533 |
('b', 2)) |
| 534 |
assert not issorted(table4, key=('foo', 'bar')) |
| 535 |
assert not issorted(table4) |
| 536 |
|
| 537 |
table5 = (('foo', 'bar', 'baz'), |
| 538 |
('b', 3, True), |
| 539 |
('b', 2), |
| 540 |
('a', 1, True)) |
| 541 |
assert not issorted(table5, key='foo') |
| 542 |
assert issorted(table5, key='foo', reverse=True) |
| 543 |
assert not issorted(table5, key='foo', reverse=True, strict=True) |
| 544 |
|
| 545 |
|
| 546 |
def test_sort_missing_cell_numeric(): |
| 547 |
""" Sorting table with missing values raises IndexError #385 """ |
| 548 |
tbl = (('a', 'b'), ('4',), ('2', '1'), ('1',)) |
| 549 |
expect = (('a', 'b'), ('1',), ('2', '1'), ('4',)) |
| 550 |
|
| 551 |
tbl_sorted = sort(tbl) |
| 552 |
ieq(expect, tbl_sorted) |
| 553 |
|
| 554 |
|
| 555 |
def test_sort_missing_cell_text(): |
| 556 |
""" Sorting table with missing values raises IndexError #385 """ |
| 557 |
tbl = (('a', 'b', 'c'), ('C',), ('A', '4', '5')) |
| 558 |
expect = (('a', 'b', 'c'), ('A', '4', '5'), ('C',)) |
| 559 |
|
| 560 |
tbl_sorted = sort(tbl) |
| 561 |
ieq(expect, tbl_sorted) |