def test_ast_sqllite(self):
     ds = SQLDataSource(
         dialect='sqlite',
         database='sample_data.db',
         table='sample_tbl',
     )
     nodes = [
         (Name('input'),
          Call(Name('dbConnect'), Call(Name('dbDriver'),
                                       Constant('SQLite')),
               (Name('dbname'), Constant('sample_data.db')))),
         (Name('input_table'), Constant('sample_tbl')),
     ]
     self.assertEqual(ds.ast(), nodes)
Ejemplo n.º 2
0
    def test_numerical_control(self):
        breaks = [0, 30, 60, 90, 120]
        control = NumericalControl(name='age_bin',
                                   expression='age',
                                   auto_breaks=False,
                                   breaks=breaks)
        ast = Call(
            Name('def_control'),
            (Name('age_bin'),
             Call(Name('cut'), Raw('age'),
                  (Name('breaks'), Call(Name('c'), map(Constant, breaks))),
                  (Name('right'), Constant(False)))))
        self.assertEqual(control.ast(), ast)

        control.auto_breaks = True
        control.num_breaks = 5
        control.closed_on_left = False
        ast = Call(Name('def_control'), (Name('age_bin'),
                                         Call(Name('cut'), Raw('age'),
                                              (Name('breaks'), Constant(5)))))
        self.assertEqual(control.ast(), ast)
 def test_ast_mysql(self):
     ds = SQLDataSource(
         dialect='mysql',
         host='example.com',
         user='******',
         password='******',
         database='sample_data',
         table='sample_tbl',
     )
     nodes = [
         (Name('input'),
          Call(Name('dbConnect'), Call(Name('dbDriver'), Constant('MySQL')),
               (Name('dbname'), Constant('sample_data')),
               (Name('user'), Constant('frodo')),
               (Name('password'), Constant('friend')),
               (Name('host'), Constant('example.com')))),
         (Name('input_table'), Constant('sample_tbl')),
     ]
     self.assertEqual(ds.ast(), nodes)
Ejemplo n.º 4
0
 def test_ast_rds(self):
     ds = FileDataSource(path='foo.RDS')
     target = Call(Name('readRDS'), Constant('foo.RDS'))
     self.assertEqual(ds.ast(), target)
Ejemplo n.º 5
0
 def test_ast_xls(self):
     ds = FileDataSource(path='foo.xls')
     target = Call(Name('read.xlsx'), Constant('foo.xls'), Constant(1))
     self.assertEqual(ds.ast(), target)
Ejemplo n.º 6
0
 def test_ast_tsv(self):
     ds = FileDataSource(path='foo.tab')
     self.assertEqual(ds.ast(), Constant('foo.tab'))
Ejemplo n.º 7
0
def cast_to_constant(val):
    """ Ensure that the value is a Constant.
    """
    if not isinstance(val, Constant):
        val = Constant(val)
    return val
Ejemplo n.º 8
0
def Product(*args):
    """ Macro to multiply an arbitrary number of arguments.
    """
    args = star_args_to_list(args)
    return OperatorReduce(Name('*'), args, Constant(1))
Ejemplo n.º 9
0
def Sum(*args):
    """ Macro to add an arbitrary number of arguments.
    """
    args = star_args_to_list(args)
    return OperatorReduce(Name('+'), args, Constant(0))