Skip to content

Commit

Permalink
fixes petl-developers#339 by adding biselect
Browse files Browse the repository at this point in the history
  • Loading branch information
alimanfoo committed Dec 4, 2015
1 parent c658146 commit f5775f6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/transform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Selecting rows
.. autofunction:: petl.transform.selects.selectusingcontext
.. autofunction:: petl.transform.selects.rowlenselect
.. autofunction:: petl.transform.selects.facet
.. autofunction:: petl.transform.selects.biselect


.. _transform_regex:
Expand Down
11 changes: 6 additions & 5 deletions petl/transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

from petl.transform.sorts import sort, mergesort, issorted

from petl.transform.selects import select, selectop, selectcontains, selecteq, \
selectfalse, selectge, selectgt, selectin, selectis, selectisinstance, \
selectisnot, selectle, selectlt, selectne, selectnone, selectnotin, \
selectnotnone, selectrangeclosed, selectrangeopen, selectrangeopenleft, \
selectrangeopenright, selecttrue, selectusingcontext, rowlenselect, facet
from petl.transform.selects import select, selectop, selectcontains, \
selecteq, selectfalse, selectge, selectgt, selectin, selectis, \
selectisinstance, selectisnot, selectle, selectlt, selectne, selectnone, \
selectnotin, selectnotnone, selectrangeclosed, selectrangeopen, \
selectrangeopenleft, selectrangeopenright, selecttrue, \
selectusingcontext, rowlenselect, facet, biselect

from petl.transform.joins import join, leftjoin, rightjoin, outerjoin, \
crossjoin, antijoin, lookupjoin, unjoin
Expand Down
49 changes: 49 additions & 0 deletions petl/transform/selects.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,52 @@ def facet(table, key):


Table.facet = facet


def biselect(table, *args, **kwargs):
"""Return two tables, the first containing selected rows, the second
containing remaining rows. E.g.::
>>> import petl as etl
>>> table1 = [['foo', 'bar', 'baz'],
... ['a', 4, 9.3],
... ['a', 2, 88.2],
... ['b', 1, 23.3],
... ['c', 8, 42.0],
... ['d', 7, 100.9],
... ['c', 2]]
>>> table2, table3 = etl.biselect(table1, lambda rec: rec.foo == 'a')
>>> table2
+-----+-----+------+
| foo | bar | baz |
+=====+=====+======+
| 'a' | 4 | 9.3 |
+-----+-----+------+
| 'a' | 2 | 88.2 |
+-----+-----+------+
>>> table3
+-----+-----+-------+
| foo | bar | baz |
+=====+=====+=======+
| 'b' | 1 | 23.3 |
+-----+-----+-------+
| 'c' | 8 | 42.0 |
+-----+-----+-------+
| 'd' | 7 | 100.9 |
+-----+-----+-------+
| 'c' | 2 | |
+-----+-----+-------+
.. versionadded:: 1.1.0
"""

# override complement kwarg
kwargs['complement'] = False
t1 = select(table, *args, **kwargs)
kwargs['complement'] = True
t2 = select(table, *args, **kwargs)
return t1, t2


Table.biselect = biselect

0 comments on commit f5775f6

Please sign in to comment.