ResponseRows now created by a factory to allow the column cell index to be used to index rows by column name.

This commit is contained in:
Paul Makepeace 2011-04-25 11:51:01 -04:00
parent d769f761b9
commit 10dfc41bcb
2 changed files with 45 additions and 33 deletions

View File

@ -333,32 +333,43 @@ class Refine:
raise Exception('Project not created') raise Exception('Project not created')
class RowsResponse(object): def RowsResponseFactory(column_index):
class RefineRows(object): """Factory for the parsing the output from get_rows().
class RefineRow(object):
def __init__(self, row_response): Uses the project's model's row cell index so that a row can be used
self.flagged = row_response['flagged'] as a dict by column name."""
self.starred = row_response['starred']
self.index = row_response['i'] class RowsResponse(object):
self.row = [c['v'] if c else None class RefineRows(object):
for c in row_response['cells']] class RefineRow(object):
def __init__(self, row_response):
self.flagged = row_response['flagged']
self.starred = row_response['starred']
self.index = row_response['i']
self.row = [c['v'] if c else None
for c in row_response['cells']]
def __getitem__(self, column):
return self.row[column_index[column]]
def __init__(self, rows_response): def __init__(self, rows_response):
self.rows_response = rows_response self.rows_response = rows_response
def __iter__(self): def __iter__(self):
for row_response in self.rows_response: for row_response in self.rows_response:
yield self.RefineRow(row_response) yield self.RefineRow(row_response)
def __len__(self): def __len__(self):
return len(self.rows_response) return len(self.rows_response)
def __init__(self, response): def __init__(self, response):
self.mode = response['mode'] self.mode = response['mode']
self.filtered = response['filtered'] self.filtered = response['filtered']
self.start = response['start'] self.start = response['start']
self.limit = response['limit'] self.limit = response['limit']
self.total = response['total'] self.total = response['total']
self.pool = response['pool'] # {"reconCandidates": {},"recons": {}} # 'pool': {"reconCandidates": {},"recons": {}}
self.rows = self.RefineRows(response['rows']) self.pool = response['pool']
self.rows = self.RefineRows(response['rows'])
return RowsResponse
class RefineProject: class RefineProject:
@ -370,7 +381,8 @@ class RefineProject:
if url.query: if url.query:
# Parse out the project ID and create a base server URL # Parse out the project ID and create a base server URL
project_id = url.query[8:] # skip project= project_id = url.query[8:] # skip project=
server = urlparse.urlunparse((url.scheme, url.netloc, '', '', '', '')) server = urlparse.urlunparse((
url.scheme, url.netloc, '', '', '', ''))
server = RefineServer(server) server = RefineServer(server)
self.server = server self.server = server
if not project_id and not project_name: if not project_id and not project_name:
@ -380,9 +392,9 @@ class RefineProject:
project_name or project_id) project_name or project_id)
self.project_id = project_id self.project_id = project_id
self.project_name = project_name self.project_name = project_name
self.columns = [] # columns & column_index filled in by get_models() self.columns = [] # following filled in by get_models()
self.column_index = {} # index into data from get_rows()
self.column_order = {} # order of column in UI self.column_order = {} # order of column in UI
self.rows_response_factory = None
self.get_models() self.get_models()
self.engine = Engine() self.engine = Engine()
self.sorting = Sorting() self.sorting = Sorting()
@ -411,12 +423,14 @@ class RefineProject:
columns = column_model['columns'] columns = column_model['columns']
# Pre-extend the list in python # Pre-extend the list in python
self.columns = [None] * len(columns) self.columns = [None] * len(columns)
column_index = {}
for i, column in enumerate(columns): for i, column in enumerate(columns):
cell_index, name = column['cellIndex'], column['name'] cell_index, name = column['cellIndex'], column['name']
self.column_order[name] = i self.column_order[name] = i
self.column_index[name] = cell_index column_index[name] = cell_index
self.columns[i] = name self.columns[i] = name
self.key_column = column_model['keyColumnName'] self.key_column = column_model['keyColumnName']
self.rows_response_factory = RowsResponseFactory(column_index)
# TODO: implement rest # TODO: implement rest
def wait_until_idle(self, polling_delay=0.5): def wait_until_idle(self, polling_delay=0.5):
@ -462,7 +476,7 @@ class RefineProject:
self.sorting = Sorting(sort_by) self.sorting = Sorting(sort_by)
response = self.do_json('get-rows', {'sorting': self.sorting.as_json(), response = self.do_json('get-rows', {'sorting': self.sorting.as_json(),
'start': start, 'limit': limit}) 'start': start, 'limit': limit})
return RowsResponse(response) return self.rows_response_factory(response)
def reorder_rows(self, sort_by=None): def reorder_rows(self, sort_by=None):
if sort_by is not None: if sort_by is not None:
@ -540,7 +554,7 @@ class RefineProject:
def add_column(self, column, new_column, expression='value', def add_column(self, column, new_column, expression='value',
column_insert_index=None, on_error='set-to-blank'): column_insert_index=None, on_error='set-to-blank'):
if column_insert_index is None: if column_insert_index is None:
column_insert_index = self.column_index[column] + 1 column_insert_index = self.column_order[column] + 1
response = self.do_json('add-column', {'baseColumnName': column, response = self.do_json('add-column', {'baseColumnName': column,
'newColumnName': new_column, 'expression': expression, 'newColumnName': new_column, 'expression': expression,
'columnInsertIndex': column_insert_index, 'onError': on_error}) 'columnInsertIndex': column_insert_index, 'onError': on_error})

View File

@ -285,9 +285,7 @@ class TutorialTestDuplicateDetection(RefineTestCase):
response['historyEntry']['description']) response['historyEntry']['description'])
response = self.project.get_rows() response = self.project.get_rows()
self.assertEqual(self.project.column_order['count'], 1) # i.e. 2nd self.assertEqual(self.project.column_order['count'], 1) # i.e. 2nd
# XXX retrieving column data is pretty vile counts = [r['count'] for r in response.rows]
counts = [r.row[self.project.column_index['count']]
for r in response.rows]
self.assertEqual(counts, [2, 2, 1, 1, 3, 3, 3, 1, 2, 2]) self.assertEqual(counts, [2, 2, 1, 1, 3, 3, 3, 1, 2, 2])