Create NumericFacet and change old Facet to TextFacet, with both as subclasses of Facet. Fix selections -> selection.

This commit is contained in:
Paul Makepeace 2011-04-24 17:20:53 -04:00
parent 9607c30bfa
commit 263c991804
3 changed files with 59 additions and 38 deletions

View File

@ -19,43 +19,60 @@ REFINE_HOST = os.environ.get('GOOGLE_REFINE_HOST', '127.0.0.1')
REFINE_PORT = os.environ.get('GOOGLE_REFINE_PORT', '3333') REFINE_PORT = os.environ.get('GOOGLE_REFINE_PORT', '3333')
def to_camel(attr):
"""convert this_attr_name to thisAttrName."""
return re.sub(r'_(.)', lambda x: x.group(1).upper(), attr)
class Facet(object): class Facet(object):
def __init__(self, column, expression='value', omit_blank=False, omit_error=False, select_blank=False, select_error=False, invert=False): def __init__(self, column, type, expression='value', **options):
self.column = column self.type = type
self.column_name = column
self.name = column # XXX not sure what the difference is yet self.name = column # XXX not sure what the difference is yet
self.selections = [] self.selection = []
self.expression = expression self.expression = expression
self.invert = invert for k, v in options.items():
self.omit_blank = omit_blank setattr(self, k, v)
self.omit_error = omit_error
self.select_blank = select_blank
self.select_error = select_error
def as_dict(self): def as_dict(self):
return { return dict([(to_camel(k), v) for k, v in self.__dict__.items()])
'type': 'list',
'name': self.column,
'columnName': self.column,
'expression': self.expression,
'selection': self.selections,
'omitBlank': self.omit_blank,
'omitError': self.omit_error,
'selectBlank': self.select_blank,
'selectError': self.select_error,
'invert': self.invert,
}
def include(self, selection): def include(self, selection):
for s in self.selections: for s in self.selection:
if s['v']['v'] == selection: if s['v']['v'] == selection:
return return
self.selections.append({'v': {'v': selection, 'l': selection}}) self.selection.append({'v': {'v': selection, 'l': selection}})
def exclude(self, selection): def exclude(self, selection):
self.selections = [s for s in self.selections if s['v']['v'] != selection] self.selection = [s for s in self.selection if s['v']['v'] != selection]
def reset(self): def reset(self):
self.selections = [] self.selection = []
class TextFacet(Facet):
def __init__(self, column, omit_blank=False, omit_error=False, select_blank=False, select_error=False, invert=False, **options):
super(TextFacet, self).__init__(
column,
type='list',
omit_blank=omit_blank,
omit_error=omit_error,
select_blank=select_blank,
select_error=select_error,
invert=invert,
**options)
class NumericFacet(Facet):
def __init__(self, column, select_blank=True, select_error=True, select_non_numeric=True, select_numeric=True, **options):
super(NumericFacet, self).__init__(
column,
type='range',
select_blank=select_blank,
select_error=select_error,
select_non_numeric=select_non_numeric,
select_numeric=select_numeric,
**options)
class FacetResponse(object): class FacetResponse(object):

View File

@ -13,37 +13,41 @@ import os
import sys import sys
import unittest import unittest
import urllib import urllib
from google.refine import Facet, Engine, FacetsResponse from google.refine import Facet, TextFacet, NumericFacet, Engine, FacetsResponse
class FacetTest(unittest.TestCase): class FacetTest(unittest.TestCase):
def test_init(self): def test_init(self):
facet = Facet('column name') facet = TextFacet('column name')
engine = Engine(facet) engine = Engine(facet)
self.assertEqual(facet.selection, [])
self.assertTrue(str(engine)) self.assertTrue(str(engine))
def test_serialize(self): def test_serialize(self):
engine = Engine() engine = Engine()
engine_json = engine.as_json() engine_json = engine.as_json()
self.assertEqual(engine_json, '{"facets": [], "mode": "row-based"}') self.assertEqual(engine_json, '{"facets": [], "mode": "row-based"}')
facet = TextFacet(column='column')
self.assertEqual(facet.as_dict(), {'selectError': False, 'name': 'column', 'selection': [], 'expression': 'value', 'invert': False, 'columnName': 'column', 'selectBlank': False, 'omitBlank': False, 'type': 'list', 'omitError': False})
facet = NumericFacet(column='column')
self.assertEqual(facet.as_dict(), {'selectBlank': True, 'name': 'column', 'selectError': True, 'expression': 'value', 'selection': [], 'selectNumeric': True, 'columnName': 'column', 'selectNonNumeric': True, 'type': 'range'})
def test_add_facet(self): def test_add_facet(self):
facet = Facet(column='Party Code') facet = TextFacet(column='Party Code')
engine = Engine(facet) engine = Engine(facet)
engine.add_facet(Facet(column='Ethnicity')) engine.add_facet(TextFacet(column='Ethnicity'))
self.assertEqual(len(engine.facets), 2) self.assertEqual(len(engine.facets), 2)
self.assertEqual(len(engine), 2) self.assertEqual(len(engine), 2)
def test_selections(self): def test_selections(self):
facet = Facet('column name') facet = TextFacet('column name')
facet.include('element') facet.include('element')
self.assertEqual(len(facet.selections), 1) self.assertEqual(len(facet.selection), 1)
facet.include('element 2') facet.include('element 2')
self.assertEqual(len(facet.selections), 2) self.assertEqual(len(facet.selection), 2)
facet.exclude('element') facet.exclude('element')
self.assertEqual(len(facet.selections), 1) self.assertEqual(len(facet.selection), 1)
facet.reset() facet.reset()
self.assertEqual(len(facet.selections), 0) self.assertEqual(len(facet.selection), 0)
def test_facets_response(self): def test_facets_response(self):

View File

@ -11,7 +11,7 @@ import sys
import os import os
import unittest import unittest
from google.refine import REFINE_HOST, REFINE_PORT from google.refine import REFINE_HOST, REFINE_PORT
from google.refine import Facet, Engine from google.refine import TextFacet, Engine
from google.refine import RefineServer, Refine, RefineProject from google.refine import RefineServer, Refine, RefineProject
PATH_TO_TEST_DATA = os.path.join('google', 'test', 'data') PATH_TO_TEST_DATA = os.path.join('google', 'test', 'data')
@ -79,7 +79,7 @@ class TutorialTestFacets(RefineTestCase):
def test_basic_facet(self): def test_basic_facet(self):
# {4} # {4}
party_code_facet = Facet(column='Party Code') party_code_facet = TextFacet(column='Party Code')
response = self.project.text_facet(party_code_facet) response = self.project.text_facet(party_code_facet)
pc = response.facets[0] pc = response.facets[0]
self.assertEqual(pc.name, 'Party Code') self.assertEqual(pc.name, 'Party Code')
@ -88,7 +88,7 @@ class TutorialTestFacets(RefineTestCase):
self.assertEqual(pc.blank_choice.count, 1446) self.assertEqual(pc.blank_choice.count, 1446)
# {5}, {6} # {5}, {6}
engine = Engine(party_code_facet) engine = Engine(party_code_facet)
ethnicity_facet = Facet(column='Ethnicity') ethnicity_facet = TextFacet(column='Ethnicity')
engine.add_facet(ethnicity_facet) engine.add_facet(ethnicity_facet)
self.project.engine = engine self.project.engine = engine
response = self.project.text_facet() response = self.project.text_facet()
@ -119,7 +119,7 @@ class TutorialTestFacets(RefineTestCase):
response = self.project.get_rows() response = self.project.get_rows()
self.assertEqual(response.filtered, 6958) self.assertEqual(response.filtered, 6958)
# {11} # {11}
office_title_facet = Facet('Office Title') office_title_facet = TextFacet('Office Title')
self.project.engine.add_facet(office_title_facet) self.project.engine.add_facet(office_title_facet)
response = self.project.text_facet() response = self.project.text_facet()
self.assertEqual(len(response.facets[2].choices), 76) self.assertEqual(len(response.facets[2].choices), 76)