def test_operations(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) self.failUnlessEqual(Value(4), parser.parse('2 + 2')(None)) self.failUnlessEqual(Value(0), parser.parse('2 - 2')(None)) self.failUnlessEqual(Value(6), parser.parse('2 * 3')(None)) self.failUnlessEqual(Value(2), parser.parse('4 / 2')(None))
def test_trivial_formula(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) self.failUnlessEqual('P0010001', parser.parse('P0010001').table) table = parser.parse('P0010001 + P0010002') self.failUnlessEqual(table.left.table, 'P0010001') self.failUnlessEqual(table.operation, '+') self.failUnlessEqual(table.right.table, 'P0010002')
def test_operator_precedence(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) self.failUnlessEqual( Value(7), parser.parse('1 + 2 * 3')(None) ) self.failUnlessEqual( Value(9), parser.parse('(1 + 2) * 3')(None) )
def test_trivial_formula(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) self.failUnlessEqual( 'P0010001', parser.parse('P0010001').table ) table = parser.parse('P0010001 + P0010002') self.failUnlessEqual(table.left.table, 'P0010001') self.failUnlessEqual(table.operation, '+') self.failUnlessEqual(table.right.table, 'P0010002')
def test_unicode(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) table = parser.parse(u'P049013 + P049040\r\n') self.failUnlessEqual('P049013', table.left.table) self.failUnlessEqual('P049040', table.right.table)
def test_operations(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) self.failUnlessEqual( Value(4), parser.parse('2 + 2')(None) ) self.failUnlessEqual( Value(0), parser.parse('2 - 2')(None) ) self.failUnlessEqual( Value(6), parser.parse('2 * 3')(None) ) self.failUnlessEqual( Value(2), parser.parse('4 / 2')(None) )
def test_unicode(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) table = parser.parse(u'P049013 + P049040\r\n') self.failUnlessEqual( 'P049013', table.left.table ) self.failUnlessEqual( 'P049040', table.right.table )
class CensusBase(object): def __init__(self): self.parser = FormulaParser(self) def data(self, formula, geo_dict,**kwargs): t = self.parser.parse(formula) result = t(geo_dict, **kwargs) return result def _type_value(self, str_value): from decimal import Decimal try: return int(str_value) except ValueError: try: return Decimal(str_value) except InvalidOperation: return None
class CensusBase(object): def __init__(self): self.parser = FormulaParser(self) def data(self, formula, geo_dict, **kwargs): t = self.parser.parse(formula) result = t(geo_dict, **kwargs) return result def _type_value(self, str_value): from decimal import Decimal try: return int(str_value) except ValueError: try: return Decimal(str_value) except InvalidOperation: return None
def test_operator_precedence(self): from census.parse import FormulaParser parser = FormulaParser(MockDatasource()) self.failUnlessEqual(Value(7), parser.parse('1 + 2 * 3')(None)) self.failUnlessEqual(Value(9), parser.parse('(1 + 2) * 3')(None))