Example #1
0
    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)
Example #2
0
    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))
Example #3
0
    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')
Example #4
0
    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)
        )
Example #5
0
    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
        )
Example #6
0
    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')
Example #7
0
    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)
        )
Example #8
0
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
Example #9
0
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
Example #10
0
 def __init__(self):
     self.parser = FormulaParser(self)
Example #11
0
 def __init__(self):
     self.parser = FormulaParser(self)
Example #12
0
    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))