| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
from collections import Counter |
| 4 |
from petl.compat import next |
| 5 |
from petl.comparison import Comparable |
| 6 |
from petl.util.base import header, Table |
| 7 |
from petl.transform.sorts import sort |
| 8 |
from petl.transform.basics import cut |
| 9 |
|
| 10 |
|
| 11 |
def complement(a, b, presorted=False, buffersize=None, tempdir=None, |
| 12 |
cache=True, strict=False): |
| 13 |
""" |
| 14 |
Return rows in `a` that are not in `b`. E.g.:: |
| 15 |
|
| 16 |
>>> import petl as etl |
| 17 |
>>> a = [['foo', 'bar', 'baz'], |
| 18 |
... ['A', 1, True], |
| 19 |
... ['C', 7, False], |
| 20 |
... ['B', 2, False], |
| 21 |
... ['C', 9, True]] |
| 22 |
>>> b = [['x', 'y', 'z'], |
| 23 |
... ['B', 2, False], |
| 24 |
... ['A', 9, False], |
| 25 |
... ['B', 3, True], |
| 26 |
... ['C', 9, True]] |
| 27 |
>>> aminusb = etl.complement(a, b) |
| 28 |
>>> aminusb |
| 29 |
+-----+-----+-------+ |
| 30 |
| foo | bar | baz | |
| 31 |
+=====+=====+=======+ |
| 32 |
| 'A' | 1 | True | |
| 33 |
+-----+-----+-------+ |
| 34 |
| 'C' | 7 | False | |
| 35 |
+-----+-----+-------+ |
| 36 |
|
| 37 |
>>> bminusa = etl.complement(b, a) |
| 38 |
>>> bminusa |
| 39 |
+-----+---+-------+ |
| 40 |
| x | y | z | |
| 41 |
+=====+===+=======+ |
| 42 |
| 'A' | 9 | False | |
| 43 |
+-----+---+-------+ |
| 44 |
| 'B' | 3 | True | |
| 45 |
+-----+---+-------+ |
| 46 |
|
| 47 |
Note that the field names of each table are ignored - rows are simply |
| 48 |
compared following a lexical sort. See also the |
| 49 |
:func:`petl.transform.setops.recordcomplement` function. |
| 50 |
|
| 51 |
If `presorted` is True, it is assumed that the data are already sorted by |
| 52 |
the given key, and the `buffersize`, `tempdir` and `cache` arguments are |
| 53 |
ignored. Otherwise, the data are sorted, see also the discussion of the |
| 54 |
`buffersize`, `tempdir` and `cache` arguments under the |
| 55 |
:func:`petl.transform.sorts.sort` function. |
| 56 |
|
| 57 |
Note that the default behaviour is not strictly set-like, because |
| 58 |
duplicate rows are counted separately, e.g.:: |
| 59 |
|
| 60 |
>>> a = [['foo', 'bar'], |
| 61 |
... ['A', 1], |
| 62 |
... ['B', 2], |
| 63 |
... ['B', 2], |
| 64 |
... ['C', 7]] |
| 65 |
>>> b = [['foo', 'bar'], |
| 66 |
... ['B', 2]] |
| 67 |
>>> aminusb = etl.complement(a, b) |
| 68 |
>>> aminusb |
| 69 |
+-----+-----+ |
| 70 |
| foo | bar | |
| 71 |
+=====+=====+ |
| 72 |
| 'A' | 1 | |
| 73 |
+-----+-----+ |
| 74 |
| 'B' | 2 | |
| 75 |
+-----+-----+ |
| 76 |
| 'C' | 7 | |
| 77 |
+-----+-----+ |
| 78 |
|
| 79 |
This behaviour can be changed with the `strict` keyword argument, e.g.:: |
| 80 |
|
| 81 |
>>> aminusb = etl.complement(a, b, strict=True) |
| 82 |
>>> aminusb |
| 83 |
+-----+-----+ |
| 84 |
| foo | bar | |
| 85 |
+=====+=====+ |
| 86 |
| 'A' | 1 | |
| 87 |
+-----+-----+ |
| 88 |
| 'C' | 7 | |
| 89 |
+-----+-----+ |
| 90 |
|
| 91 |
.. versionchanged:: 1.1.0 |
| 92 |
|
| 93 |
If `strict` is `True` then strict set-like behaviour is used, i.e., |
| 94 |
only rows in `a` not found in `b` are returned. |
| 95 |
|
| 96 |
""" |
| 97 |
|
| 98 |
return ComplementView(a, b, presorted=presorted, buffersize=buffersize, |
| 99 |
tempdir=tempdir, cache=cache, strict=strict) |
| 100 |
|
| 101 |
|
| 102 |
Table.complement = complement |
| 103 |
|
| 104 |
|
| 105 |
class ComplementView(Table): |
| 106 |
|
| 107 |
def __init__(self, a, b, presorted=False, buffersize=None, tempdir=None, |
| 108 |
cache=True, strict=False): |
| 109 |
if presorted: |
| 110 |
self.a = a |
| 111 |
self.b = b |
| 112 |
else: |
| 113 |
self.a = sort(a, buffersize=buffersize, tempdir=tempdir, |
| 114 |
cache=cache) |
| 115 |
self.b = sort(b, buffersize=buffersize, tempdir=tempdir, |
| 116 |
cache=cache) |
| 117 |
self.strict = strict |
| 118 |
|
| 119 |
def __iter__(self): |
| 120 |
return itercomplement(self.a, self.b, self.strict) |
| 121 |
|
| 122 |
|
| 123 |
def itercomplement(ta, tb, strict): |
| 124 |
# coerce rows to tuples to ensure hashable and comparable |
| 125 |
ita = (tuple(row) for row in iter(ta)) |
| 126 |
itb = (tuple(row) for row in iter(tb)) |
| 127 |
ahdr = tuple(next(ita)) |
| 128 |
next(itb) # ignore b fields |
| 129 |
yield ahdr |
| 130 |
|
| 131 |
try: |
| 132 |
a = next(ita) |
| 133 |
except StopIteration: |
| 134 |
pass |
| 135 |
else: |
| 136 |
try: |
| 137 |
b = next(itb) |
| 138 |
except StopIteration: |
| 139 |
yield a |
| 140 |
for row in ita: |
| 141 |
yield row |
| 142 |
else: |
| 143 |
# we want the elements in a that are not in b |
| 144 |
while True: |
| 145 |
if b is None or Comparable(a) < Comparable(b): |
| 146 |
yield a |
| 147 |
try: |
| 148 |
a = next(ita) |
| 149 |
except StopIteration: |
| 150 |
break |
| 151 |
elif a == b: |
| 152 |
try: |
| 153 |
a = next(ita) |
| 154 |
except StopIteration: |
| 155 |
break |
| 156 |
if not strict: |
| 157 |
try: |
| 158 |
b = next(itb) |
| 159 |
except StopIteration: |
| 160 |
b = None |
| 161 |
else: |
| 162 |
try: |
| 163 |
b = next(itb) |
| 164 |
except StopIteration: |
| 165 |
b = None |
| 166 |
|
| 167 |
|
| 168 |
def recordcomplement(a, b, buffersize=None, tempdir=None, cache=True, |
| 169 |
strict=False): |
| 170 |
""" |
| 171 |
Find records in `a` that are not in `b`. E.g.:: |
| 172 |
|
| 173 |
>>> import petl as etl |
| 174 |
>>> a = [['foo', 'bar', 'baz'], |
| 175 |
... ['A', 1, True], |
| 176 |
... ['C', 7, False], |
| 177 |
... ['B', 2, False], |
| 178 |
... ['C', 9, True]] |
| 179 |
>>> b = [['bar', 'foo', 'baz'], |
| 180 |
... [2, 'B', False], |
| 181 |
... [9, 'A', False], |
| 182 |
... [3, 'B', True], |
| 183 |
... [9, 'C', True]] |
| 184 |
>>> aminusb = etl.recordcomplement(a, b) |
| 185 |
>>> aminusb |
| 186 |
+-----+-----+-------+ |
| 187 |
| foo | bar | baz | |
| 188 |
+=====+=====+=======+ |
| 189 |
| 'A' | 1 | True | |
| 190 |
+-----+-----+-------+ |
| 191 |
| 'C' | 7 | False | |
| 192 |
+-----+-----+-------+ |
| 193 |
|
| 194 |
>>> bminusa = etl.recordcomplement(b, a) |
| 195 |
>>> bminusa |
| 196 |
+-----+-----+-------+ |
| 197 |
| bar | foo | baz | |
| 198 |
+=====+=====+=======+ |
| 199 |
| 3 | 'B' | True | |
| 200 |
+-----+-----+-------+ |
| 201 |
| 9 | 'A' | False | |
| 202 |
+-----+-----+-------+ |
| 203 |
|
| 204 |
Note that both tables must have the same set of fields, but that the order |
| 205 |
of the fields does not matter. See also the |
| 206 |
:func:`petl.transform.setops.complement` function. |
| 207 |
|
| 208 |
See also the discussion of the `buffersize`, `tempdir` and `cache` arguments |
| 209 |
under the :func:`petl.transform.sorts.sort` function. |
| 210 |
|
| 211 |
""" |
| 212 |
|
| 213 |
# TODO possible with only one pass? |
| 214 |
|
| 215 |
ha = header(a) |
| 216 |
hb = header(b) |
| 217 |
assert set(ha) == set(hb), 'both tables must have the same set of fields' |
| 218 |
# make sure fields are in the same order |
| 219 |
bv = cut(b, *ha) |
| 220 |
return complement(a, bv, buffersize=buffersize, tempdir=tempdir, |
| 221 |
cache=cache, strict=strict) |
| 222 |
|
| 223 |
|
| 224 |
Table.recordcomplement = recordcomplement |
| 225 |
|
| 226 |
|
| 227 |
def diff(a, b, presorted=False, buffersize=None, tempdir=None, cache=True, |
| 228 |
strict=False): |
| 229 |
""" |
| 230 |
Find the difference between rows in two tables. Returns a pair of tables. |
| 231 |
E.g.:: |
| 232 |
|
| 233 |
>>> import petl as etl |
| 234 |
>>> a = [['foo', 'bar', 'baz'], |
| 235 |
... ['A', 1, True], |
| 236 |
... ['C', 7, False], |
| 237 |
... ['B', 2, False], |
| 238 |
... ['C', 9, True]] |
| 239 |
>>> b = [['x', 'y', 'z'], |
| 240 |
... ['B', 2, False], |
| 241 |
... ['A', 9, False], |
| 242 |
... ['B', 3, True], |
| 243 |
... ['C', 9, True]] |
| 244 |
>>> added, subtracted = etl.diff(a, b) |
| 245 |
>>> # rows in b not in a |
| 246 |
... added |
| 247 |
+-----+---+-------+ |
| 248 |
| x | y | z | |
| 249 |
+=====+===+=======+ |
| 250 |
| 'A' | 9 | False | |
| 251 |
+-----+---+-------+ |
| 252 |
| 'B' | 3 | True | |
| 253 |
+-----+---+-------+ |
| 254 |
|
| 255 |
>>> # rows in a not in b |
| 256 |
... subtracted |
| 257 |
+-----+-----+-------+ |
| 258 |
| foo | bar | baz | |
| 259 |
+=====+=====+=======+ |
| 260 |
| 'A' | 1 | True | |
| 261 |
+-----+-----+-------+ |
| 262 |
| 'C' | 7 | False | |
| 263 |
+-----+-----+-------+ |
| 264 |
|
| 265 |
Convenient shorthand for ``(complement(b, a), complement(a, b))``. See also |
| 266 |
:func:`petl.transform.setops.complement`. |
| 267 |
|
| 268 |
If `presorted` is True, it is assumed that the data are already sorted by |
| 269 |
the given key, and the `buffersize`, `tempdir` and `cache` arguments are |
| 270 |
ignored. Otherwise, the data are sorted, see also the discussion of the |
| 271 |
`buffersize`, `tempdir` and `cache` arguments under the |
| 272 |
:func:`petl.transform.sorts.sort` function. |
| 273 |
|
| 274 |
.. versionchanged:: 1.1.0 |
| 275 |
|
| 276 |
If `strict` is `True` then strict set-like behaviour is used. |
| 277 |
|
| 278 |
""" |
| 279 |
|
| 280 |
if not presorted: |
| 281 |
a = sort(a) |
| 282 |
b = sort(b) |
| 283 |
added = complement(b, a, presorted=True, buffersize=buffersize, |
| 284 |
tempdir=tempdir, cache=cache, strict=strict) |
| 285 |
subtracted = complement(a, b, presorted=True, buffersize=buffersize, |
| 286 |
tempdir=tempdir, cache=cache, strict=strict) |
| 287 |
return added, subtracted |
| 288 |
|
| 289 |
|
| 290 |
Table.diff = diff |
| 291 |
|
| 292 |
|
| 293 |
def recorddiff(a, b, buffersize=None, tempdir=None, cache=True, strict=False): |
| 294 |
""" |
| 295 |
Find the difference between records in two tables. E.g.:: |
| 296 |
|
| 297 |
>>> import petl as etl |
| 298 |
>>> a = [['foo', 'bar', 'baz'], |
| 299 |
... ['A', 1, True], |
| 300 |
... ['C', 7, False], |
| 301 |
... ['B', 2, False], |
| 302 |
... ['C', 9, True]] |
| 303 |
>>> b = [['bar', 'foo', 'baz'], |
| 304 |
... [2, 'B', False], |
| 305 |
... [9, 'A', False], |
| 306 |
... [3, 'B', True], |
| 307 |
... [9, 'C', True]] |
| 308 |
>>> added, subtracted = etl.recorddiff(a, b) |
| 309 |
>>> added |
| 310 |
+-----+-----+-------+ |
| 311 |
| bar | foo | baz | |
| 312 |
+=====+=====+=======+ |
| 313 |
| 3 | 'B' | True | |
| 314 |
+-----+-----+-------+ |
| 315 |
| 9 | 'A' | False | |
| 316 |
+-----+-----+-------+ |
| 317 |
|
| 318 |
>>> subtracted |
| 319 |
+-----+-----+-------+ |
| 320 |
| foo | bar | baz | |
| 321 |
+=====+=====+=======+ |
| 322 |
| 'A' | 1 | True | |
| 323 |
+-----+-----+-------+ |
| 324 |
| 'C' | 7 | False | |
| 325 |
+-----+-----+-------+ |
| 326 |
|
| 327 |
Convenient shorthand for |
| 328 |
``(recordcomplement(b, a), recordcomplement(a, b))``. See also |
| 329 |
:func:`petl.transform.setops.recordcomplement`. |
| 330 |
|
| 331 |
See also the discussion of the `buffersize`, `tempdir` and `cache` |
| 332 |
arguments under the :func:`petl.transform.sorts.sort` function. |
| 333 |
|
| 334 |
.. versionchanged:: 1.1.0 |
| 335 |
|
| 336 |
If `strict` is `True` then strict set-like behaviour is used. |
| 337 |
|
| 338 |
""" |
| 339 |
|
| 340 |
added = recordcomplement(b, a, buffersize=buffersize, tempdir=tempdir, |
| 341 |
cache=cache, strict=strict) |
| 342 |
subtracted = recordcomplement(a, b, buffersize=buffersize, tempdir=tempdir, |
| 343 |
cache=cache, strict=strict) |
| 344 |
return added, subtracted |
| 345 |
|
| 346 |
|
| 347 |
Table.recorddiff = recorddiff |
| 348 |
|
| 349 |
|
| 350 |
def intersection(a, b, presorted=False, buffersize=None, tempdir=None, |
| 351 |
cache=True): |
| 352 |
""" |
| 353 |
Return rows in `a` that are also in `b`. E.g.:: |
| 354 |
|
| 355 |
>>> import petl as etl |
| 356 |
>>> table1 = [['foo', 'bar', 'baz'], |
| 357 |
... ['A', 1, True], |
| 358 |
... ['C', 7, False], |
| 359 |
... ['B', 2, False], |
| 360 |
... ['C', 9, True]] |
| 361 |
>>> table2 = [['x', 'y', 'z'], |
| 362 |
... ['B', 2, False], |
| 363 |
... ['A', 9, False], |
| 364 |
... ['B', 3, True], |
| 365 |
... ['C', 9, True]] |
| 366 |
>>> table3 = etl.intersection(table1, table2) |
| 367 |
>>> table3 |
| 368 |
+-----+-----+-------+ |
| 369 |
| foo | bar | baz | |
| 370 |
+=====+=====+=======+ |
| 371 |
| 'B' | 2 | False | |
| 372 |
+-----+-----+-------+ |
| 373 |
| 'C' | 9 | True | |
| 374 |
+-----+-----+-------+ |
| 375 |
|
| 376 |
If `presorted` is True, it is assumed that the data are already sorted by |
| 377 |
the given key, and the `buffersize`, `tempdir` and `cache` arguments are |
| 378 |
ignored. Otherwise, the data are sorted, see also the discussion of the |
| 379 |
`buffersize`, `tempdir` and `cache` arguments under the |
| 380 |
:func:`petl.transform.sorts.sort` function. |
| 381 |
|
| 382 |
""" |
| 383 |
|
| 384 |
return IntersectionView(a, b, presorted=presorted, buffersize=buffersize, |
| 385 |
tempdir=tempdir, cache=cache) |
| 386 |
|
| 387 |
|
| 388 |
Table.intersection = intersection |
| 389 |
|
| 390 |
|
| 391 |
class IntersectionView(Table): |
| 392 |
|
| 393 |
def __init__(self, a, b, presorted=False, buffersize=None, tempdir=None, |
| 394 |
cache=True): |
| 395 |
if presorted: |
| 396 |
self.a = a |
| 397 |
self.b = b |
| 398 |
else: |
| 399 |
self.a = sort(a, buffersize=buffersize, tempdir=tempdir, |
| 400 |
cache=cache) |
| 401 |
self.b = sort(b, buffersize=buffersize, tempdir=tempdir, |
| 402 |
cache=cache) |
| 403 |
|
| 404 |
def __iter__(self): |
| 405 |
return iterintersection(self.a, self.b) |
| 406 |
|
| 407 |
|
| 408 |
def iterintersection(a, b): |
| 409 |
ita = iter(a) |
| 410 |
itb = iter(b) |
| 411 |
ahdr = next(ita) |
| 412 |
next(itb) # ignore b header |
| 413 |
yield tuple(ahdr) |
| 414 |
try: |
| 415 |
a = tuple(next(ita)) |
| 416 |
b = tuple(next(itb)) |
| 417 |
while True: |
| 418 |
if Comparable(a) < Comparable(b): |
| 419 |
a = tuple(next(ita)) |
| 420 |
elif a == b: |
| 421 |
yield a |
| 422 |
a = tuple(next(ita)) |
| 423 |
b = tuple(next(itb)) |
| 424 |
else: |
| 425 |
b = tuple(next(itb)) |
| 426 |
except StopIteration: |
| 427 |
pass |
| 428 |
|
| 429 |
|
| 430 |
def hashcomplement(a, b, strict=False): |
| 431 |
""" |
| 432 |
Alternative implementation of :func:`petl.transform.setops.complement`, |
| 433 |
where the complement is executed by constructing an in-memory set for all |
| 434 |
rows found in the right hand table, then iterating over rows from the |
| 435 |
left hand table. |
| 436 |
|
| 437 |
May be faster and/or more resource efficient where the right table is small |
| 438 |
and the left table is large. |
| 439 |
|
| 440 |
.. versionchanged:: 1.1.0 |
| 441 |
|
| 442 |
If `strict` is `True` then strict set-like behaviour is used, i.e., |
| 443 |
only rows in `a` not found in `b` are returned. |
| 444 |
|
| 445 |
""" |
| 446 |
|
| 447 |
return HashComplementView(a, b, strict=strict) |
| 448 |
|
| 449 |
|
| 450 |
Table.hashcomplement = hashcomplement |
| 451 |
|
| 452 |
|
| 453 |
class HashComplementView(Table): |
| 454 |
|
| 455 |
def __init__(self, a, b, strict=False): |
| 456 |
self.a = a |
| 457 |
self.b = b |
| 458 |
self.strict = strict |
| 459 |
|
| 460 |
def __iter__(self): |
| 461 |
return iterhashcomplement(self.a, self.b, self.strict) |
| 462 |
|
| 463 |
|
| 464 |
def iterhashcomplement(a, b, strict): |
| 465 |
ita = iter(a) |
| 466 |
ahdr = next(ita) |
| 467 |
yield tuple(ahdr) |
| 468 |
itb = iter(b) |
| 469 |
next(itb) # discard b header, assume same as a |
| 470 |
|
| 471 |
# N.B., need to account for possibility of duplicate rows |
| 472 |
bcnt = Counter(tuple(row) for row in itb) |
| 473 |
for ar in ita: |
| 474 |
t = tuple(ar) |
| 475 |
if bcnt[t] > 0: |
| 476 |
if not strict: |
| 477 |
bcnt[t] -= 1 |
| 478 |
else: |
| 479 |
yield t |
| 480 |
|
| 481 |
|
| 482 |
def hashintersection(a, b): |
| 483 |
""" |
| 484 |
Alternative implementation of |
| 485 |
:func:`petl.transform.setops.intersection`, where the intersection |
| 486 |
is executed by constructing an in-memory set for all rows found in the |
| 487 |
right hand table, then iterating over rows from the left hand table. |
| 488 |
|
| 489 |
May be faster and/or more resource efficient where the right table is small |
| 490 |
and the left table is large. |
| 491 |
|
| 492 |
""" |
| 493 |
|
| 494 |
return HashIntersectionView(a, b) |
| 495 |
|
| 496 |
|
| 497 |
Table.hashintersection = hashintersection |
| 498 |
|
| 499 |
|
| 500 |
class HashIntersectionView(Table): |
| 501 |
|
| 502 |
def __init__(self, a, b): |
| 503 |
self.a = a |
| 504 |
self.b = b |
| 505 |
|
| 506 |
def __iter__(self): |
| 507 |
return iterhashintersection(self.a, self.b) |
| 508 |
|
| 509 |
|
| 510 |
def iterhashintersection(a, b): |
| 511 |
ita = iter(a) |
| 512 |
ahdr = next(ita) |
| 513 |
yield tuple(ahdr) |
| 514 |
itb = iter(b) |
| 515 |
next(itb) # discard b header, assume same as a |
| 516 |
|
| 517 |
# N.B., need to account for possibility of duplicate rows |
| 518 |
bcnt = Counter(tuple(row) for row in itb) |
| 519 |
for ar in ita: |
| 520 |
t = tuple(ar) |
| 521 |
if bcnt[t] > 0: |
| 522 |
yield t |
| 523 |
bcnt[t] -= 1 |