Add rename_column(), fill_down(), transpose_columns_into_rows()

This commit is contained in:
Paul Makepeace 2011-04-25 21:22:12 -04:00
parent d3bd69798b
commit 364bed871b
2 changed files with 80 additions and 4 deletions

View File

@ -335,10 +335,10 @@ class Refine:
def RowsResponseFactory(column_index): def RowsResponseFactory(column_index):
"""Factory for the parsing the output from get_rows(). """Factory for the parsing the output from get_rows().
Uses the project's model's row cell index so that a row can be used Uses the project's model's row cell index so that a row can be used
as a dict by column name.""" as a dict by column name."""
class RowsResponse(object): class RowsResponse(object):
class RefineRows(object): class RefineRows(object):
class RefineRow(object): class RefineRow(object):
@ -415,7 +415,7 @@ class RefineProject:
def get_models(self): def get_models(self):
"""Fill out column metadata. """Fill out column metadata.
column structure is sent in a list of columns in their order. column structure is sent in a list of columns in their order.
The cellIndex is used to find that column's data when returned from The cellIndex is used to find that column's data when returned from
get_rows().""" get_rows()."""
@ -562,7 +562,29 @@ class RefineProject:
self.get_models() self.get_models()
return response return response
def rename_column(self, column, new_column):
response = self.do_json('rename-column', {'oldColumnName': column,
'newColumnName': new_column})
self.get_models()
return response
def blank_down(self, column): def blank_down(self, column):
response = self.do_json('blank-down', {'columnName': column}) response = self.do_json('blank-down', {'columnName': column})
self.get_models() self.get_models()
return response return response
def fill_down(self, column):
response = self.do_json('fill-down', {'columnName': column})
self.get_models()
return response
def transpose_columns_into_rows(self, start_column, column_count,
combined_column_name, separator=':', prepend_column_name=True,
ignore_blank_cells=True):
response = self.do_json('transpose-columns-into-rows', {
'startColumnName': start_column, 'columnCount': column_count,
'combinedColumnName': combined_column_name,
'prependColumnName': prepend_column_name,
'separator': separator, 'ignoreBlankCells': ignore_blank_cells})
self.get_models()
return response

View File

@ -317,5 +317,59 @@ class TutorialTestDuplicateDetection(RefineTestCase):
]) ])
class TutorialTestTransposeColumnsIntoRows(RefineTestCase):
project_file = 'us_economic_assistance.csv'
def test_transpose_columns_into_rows(self):
# Section "5. Structural Editing, Transpose Columns into Rows"
# {1}, {2}, {3}
response = self.project.transpose_columns_into_rows(
'FY1946', 64, 'pair')
self.assertTrue('64 column(s) starting with FY1946' in
response['historyEntry']['description'])
# {4}
response = self.project.add_column('pair', 'year',
'value[2,6].toNumber()')
self.assertTrue('filling 26185 rows' in
response['historyEntry']['description'])
# {5}
response = self.project.text_transform(column='pair',
expression='value.substring(7).toNumber()')
self.assertTrue('transform on 26185 cells' in
response['historyEntry']['description'])
# {6}
response = self.project.rename_column('pair', 'amount')
self.assertTrue('Rename column pair to amount' in
response['historyEntry']['description'])
# {7}
response = self.project.fill_down('country_name')
self.assertTrue('Fill down 23805 cells' in
response['historyEntry']['description'])
response = self.project.fill_down('program_name')
self.assertTrue('Fill down 23805 cells' in
response['historyEntry']['description'])
# spot check of last row for transforms and fill down
response = self.project.get_rows()
row10 = [r for r in response.rows][9]
self.assertEqual(row10['country_name'], 'Afghanistan')
self.assertEqual(row10['program_name'],
'Department of Defense Security Assistance')
self.assertEqual(row10['amount'], 113777303)
class TutorialTestTransposeFixedNumbeOfRowsIntoColumns(RefineTestCase):
project_file = 'fixed-rows.csv'
def test_transpose_fixed_number_of_rows_into_columns(self):
pass
class TutorialTestTransposeVariableNumbeOfRowsIntoColumns(RefineTestCase):
project_file = 'variable-rows.csv'
def test_transpose_variable_number_of_rows_into_columns(self):
pass
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()