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