Add get_rows() and supporting row classes. Add len() to engine for number of facets. Add test for Engine.add_facet()
This commit is contained in:
parent
0586e55ea8
commit
26bc2030a2
|
@ -44,6 +44,7 @@ class Facet(object):
|
|||
'invert': self.invert,
|
||||
}
|
||||
|
||||
|
||||
class FacetResponse(object):
|
||||
def __init__(self, facet):
|
||||
self.name = facet['name']
|
||||
|
@ -82,12 +83,16 @@ class Engine(object):
|
|||
'mode': self.mode,
|
||||
}
|
||||
|
||||
def __len__(self):
|
||||
return len(self.facets)
|
||||
|
||||
def as_json(self):
|
||||
return json.dumps(self.as_dict())
|
||||
|
||||
def add_facet(self, facet):
|
||||
self.facets.append(facet)
|
||||
|
||||
|
||||
class RefineServer(object):
|
||||
"""Communicate with a Refine server."""
|
||||
|
||||
|
@ -218,6 +223,32 @@ class Refine:
|
|||
raise Exception('Project not created')
|
||||
|
||||
|
||||
class RowsResponse(object):
|
||||
class RefineRows(object):
|
||||
class RefineRow(object):
|
||||
def __init__(self, row_response):
|
||||
self.flagged = row_response['flagged']
|
||||
self.starred = row_response['starred']
|
||||
self.row = [c['v'] if c else None for c in row_response['cells']]
|
||||
|
||||
def __init__(self, rows_response):
|
||||
self.rows_response = rows_response
|
||||
def __iter__(self):
|
||||
for row_response in self.rows_response:
|
||||
yield self.RefineRow(row_response)
|
||||
def __len__(self):
|
||||
return len(self.rows_response)
|
||||
|
||||
def __init__(self, response):
|
||||
self.mode = response['mode']
|
||||
self.filtered = response['filtered']
|
||||
self.start = response['start']
|
||||
self.limit = response['limit']
|
||||
self.total = response['total']
|
||||
self.pool = response['pool'] # {"reconCandidates": {},"recons": {}}
|
||||
self.rows = self.RefineRows(response['rows'])
|
||||
|
||||
|
||||
class RefineProject:
|
||||
"""A Google Refine project."""
|
||||
def __init__(self, server, project_id=None, project_name=None):
|
||||
|
@ -239,6 +270,7 @@ class RefineProject:
|
|||
self.columns = [] # columns & column_index filled in by get_models()
|
||||
self.column_index = {}
|
||||
self.get_models()
|
||||
self.engine = Engine()
|
||||
|
||||
def do_raw(self, command, data):
|
||||
"""Issue a command to the server & return a response object."""
|
||||
|
@ -296,8 +328,14 @@ class RefineProject:
|
|||
response_json = self.do_json('delete-project')
|
||||
return 'code' in response_json and response_json['code'] == 'ok'
|
||||
|
||||
def text_facet(self, facets=None, engine=None, mode='row-based'):
|
||||
if not engine:
|
||||
engine = Engine(facets, mode)
|
||||
response = self.do_json('compute-facets', {'engine': engine.as_json()})
|
||||
def text_facet(self, facets=None):
|
||||
if facets:
|
||||
self.engine = Engine(facets)
|
||||
response = self.do_json('compute-facets',
|
||||
{'engine': self.engine.as_json()})
|
||||
return FacetsResponse(response)
|
||||
|
||||
def get_rows(self, engine=None, start=0, limit=10):
|
||||
response = self.do_json('get-rows', {'start': start, 'limit': limit})
|
||||
return RowsResponse(response)
|
||||
|
||||
|
|
|
@ -22,13 +22,20 @@ class FacetTest(unittest.TestCase):
|
|||
self.assertTrue(str(engine))
|
||||
facet2 = Facet('Ethnicity')
|
||||
engine.add_facet(facet2)
|
||||
print engine.as_json()
|
||||
self.assertEqual(len(engine.facets), 2)
|
||||
self.assertEqual(len(engine), 2)
|
||||
|
||||
def test_serialize(self):
|
||||
engine = Engine()
|
||||
engine_json = engine.as_json()
|
||||
self.assertEqual(engine_json, '{"facets": [], "mode": "row-based"}')
|
||||
|
||||
def test_add_facet(self):
|
||||
facet = Facet(column='Party Code')
|
||||
engine = Engine(facet)
|
||||
engine.add_facet(Facet(column='Ethnicity'))
|
||||
self.assertEqual(len(engine.facets), 2)
|
||||
|
||||
def test_facets_response(self):
|
||||
response = """{"facets":[{"name":"Party Code","expression":"value","columnName":"Party Code","invert":false,"choices":[{"v":{"v":"D","l":"D"},"c":3700,"s":false},{"v":{"v":"R","l":"R"},"c":1613,"s":false},{"v":{"v":"N","l":"N"},"c":15,"s":false},{"v":{"v":"O","l":"O"},"c":184,"s":false}],"blankChoice":{"s":false,"c":1446}}],"mode":"row-based"}"""
|
||||
response = json.loads(response)
|
||||
|
|
|
@ -66,6 +66,15 @@ class RefineTest(RefineTestCase):
|
|||
class TutorialTestFacets(RefineTestCase):
|
||||
project_file = 'louisiana-elected-officials.csv'
|
||||
|
||||
def test_get_rows(self):
|
||||
response = self.project.get_rows(limit=10)
|
||||
self.assertEqual(len(response.rows), 10)
|
||||
self.assertEqual(response.limit, 10)
|
||||
self.assertEqual(response.total, 6958)
|
||||
for row in response.rows:
|
||||
self.assertFalse(row.flagged)
|
||||
self.assertFalse(row.starred)
|
||||
|
||||
def test_basic_facet(self):
|
||||
facet = Facet(column='Party Code')
|
||||
facets = self.project.text_facet(facet)
|
||||
|
@ -74,10 +83,10 @@ class TutorialTestFacets(RefineTestCase):
|
|||
self.assertEqual(pc.choices['D'].count, 3700)
|
||||
self.assertEqual(pc.choices['N'].count, 15)
|
||||
self.assertEqual(pc.blank_choice.count, 1446)
|
||||
|
||||
engine = Engine(facet)
|
||||
engine.add_facet(Facet(column='Ethnicity'))
|
||||
facets = self.project.text_facet(engine=engine)
|
||||
self.project.engine = engine
|
||||
facets = self.project.text_facet()
|
||||
e = facets.facets[1]
|
||||
self.assertEqual(e.choices['B'].count, 1255)
|
||||
self.assertEqual(e.choices['W'].count, 4469)
|
||||
|
|
Loading…
Reference in New Issue