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')
def to_camel(attr):
"""convert this_attr_name to thisAttrName."""
return re.sub(r'_(.)', lambda x: x.group(1).upper(), attr)
class Facet(object):
def __init__(self, column, expression='value', omit_blank=False, omit_error=False, select_blank=False, select_error=False, invert=False):
self.column = column
def __init__(self, column, type, expression='value', **options):
self.type = type
self.column_name = column
self.name = column # XXX not sure what the difference is yet
self.selections = []
self.selection = []
self.expression = expression
self.invert = invert
self.omit_blank = omit_blank
self.omit_error = omit_error
self.select_blank = select_blank
self.select_error = select_error
for k, v in options.items():
setattr(self, k, v)
def as_dict(self):
return {
'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,
}
return dict([(to_camel(k), v) for k, v in self.__dict__.items()])
def include(self, selection):
for s in self.selections:
for s in self.selection:
if s['v']['v'] == selection:
return
self.selections.append({'v': {'v': selection, 'l': selection}})
self.selection.append({'v': {'v': selection, 'l': 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):
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):

View File

@ -13,37 +13,41 @@ import os
import sys
import unittest
import urllib
from google.refine import Facet, Engine, FacetsResponse
from google.refine import Facet, TextFacet, NumericFacet, Engine, FacetsResponse
class FacetTest(unittest.TestCase):
def test_init(self):
facet = Facet('column name')
facet = TextFacet('column name')
engine = Engine(facet)
self.assertEqual(facet.selection, [])
self.assertTrue(str(engine))
def test_serialize(self):
engine = Engine()
engine_json = engine.as_json()
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):
facet = Facet(column='Party Code')
facet = TextFacet(column='Party Code')
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), 2)
def test_selections(self):
facet = Facet('column name')
facet = TextFacet('column name')
facet.include('element')
self.assertEqual(len(facet.selections), 1)
self.assertEqual(len(facet.selection), 1)
facet.include('element 2')
self.assertEqual(len(facet.selections), 2)
self.assertEqual(len(facet.selection), 2)
facet.exclude('element')
self.assertEqual(len(facet.selections), 1)
self.assertEqual(len(facet.selection), 1)
facet.reset()
self.assertEqual(len(facet.selections), 0)
self.assertEqual(len(facet.selection), 0)
def test_facets_response(self):

View File

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