-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Table.fuse. wireservice/csvkit#711.
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
# pylint: disable=W0212 | ||
|
||
from agate.rows import Row | ||
from agate import utils | ||
|
||
|
||
def fuse(self, right_table): | ||
""" | ||
Join two tables by aligning them horizontally without performing any | ||
filtering. This is effectively a "join by row number". | ||
:param right_table: | ||
The "right" table to join to. | ||
:returns: | ||
A new :class:`.Table`. | ||
""" | ||
len_left = len(self._columns) | ||
len_right = len(right_table._columns) | ||
|
||
left_rows = (list(r) for r in self._rows) | ||
right_rows = (list(r) for r in right_table._rows) | ||
|
||
column_names = self._column_names + right_table._column_names | ||
column_types = self._column_types + right_table._column_types | ||
|
||
new_rows = [] | ||
|
||
for left_row in left_rows: | ||
try: | ||
right_row = next(right_rows) | ||
new_rows.append(Row(left_row + right_row, column_names)) | ||
except StopIteration: | ||
new_rows.append(Row(left_row + ([None] * len_right), column_names)) | ||
|
||
for right_row in right_rows: | ||
new_rows.append(Row(([None] * len_left) + right_row, column_names)) | ||
|
||
return self._fork(new_rows, column_names, column_types) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf8 -*- | ||
|
||
from agate import Table | ||
from agate.data_types import * | ||
from agate.testcase import AgateTestCase | ||
|
||
|
||
class TestFuse(AgateTestCase): | ||
def setUp(self): | ||
self.left_rows = ( | ||
(1, 4, 'a'), | ||
(2, 3, 'b'), | ||
(None, 2, 'c') | ||
) | ||
|
||
self.right_rows = ( | ||
(1, 4, 'a'), | ||
(2, 3, 'b'), | ||
(None, 2, 'c') | ||
) | ||
|
||
self.number_type = Number() | ||
self.text_type = Text() | ||
|
||
self.left_column_names = ['one', 'two', 'three'] | ||
self.right_column_names = ['four', 'five', 'six'] | ||
self.column_types = [self.number_type, self.number_type, self.text_type] | ||
|
||
self.left = Table(self.left_rows, self.left_column_names, self.column_types) | ||
self.right = Table(self.right_rows, self.right_column_names, self.column_types) | ||
|
||
def test_fuse(self): | ||
new_table = self.left.fuse(self.right) | ||
|
||
self.assertIsNot(new_table, self.left) | ||
self.assertIsNot(new_table, self.right) | ||
self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'five', 'six']) | ||
self.assertColumnTypes(new_table, [Number, Number, Text, Number, Number, Text]) | ||
self.assertRows(new_table, [ | ||
(1, 4, 'a', 1, 4, 'a'), | ||
(2, 3, 'b', 2, 3, 'b'), | ||
(None, 2, 'c', None, 2, 'c') | ||
]) | ||
|
||
def test_fuse_short_right(self): | ||
right_rows = self.right_rows + ((7, 9, 'z'),) | ||
right = Table(right_rows, self.right_column_names, self.column_types) | ||
|
||
new_table = self.left.fuse(right) | ||
|
||
self.assertIsNot(new_table, self.left) | ||
self.assertIsNot(new_table, right) | ||
self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'five', 'six']) | ||
self.assertColumnTypes(new_table, [Number, Number, Text, Number, Number, Text]) | ||
self.assertRows(new_table, [ | ||
(1, 4, 'a', 1, 4, 'a'), | ||
(2, 3, 'b', 2, 3, 'b'), | ||
(None, 2, 'c', None, 2, 'c'), | ||
(None, None, None, 7, 9, 'z') | ||
]) | ||
|
||
def test_fuse_short_left(self): | ||
left_rows = self.left_rows + ((7, 9, 'z'),) | ||
left = Table(left_rows, self.left_column_names, self.column_types) | ||
|
||
new_table = left.fuse(self.right) | ||
|
||
import sys | ||
new_table.print_table(output=sys.stdout) | ||
|
||
self.assertIsNot(new_table, left) | ||
self.assertIsNot(new_table, self.right) | ||
self.assertColumnNames(new_table, ['one', 'two', 'three', 'four', 'five', 'six']) | ||
self.assertColumnTypes(new_table, [Number, Number, Text, Number, Number, Text]) | ||
self.assertRows(new_table, [ | ||
(1, 4, 'a', 1, 4, 'a'), | ||
(2, 3, 'b', 2, 3, 'b'), | ||
(None, 2, 'c', None, 2, 'c'), | ||
(7, 9, 'z', None, None, None) | ||
]) |