Example #1
0
    def eval_separate(self, node, args):
        n_cols = robjects.r('ncol(' + args[0] + ')')[0]
        self.assertArg(node,
                       args,
                       index=1,
                       cond=lambda x: x <= n_cols,
                       capture_indices=[0])

        ret_df_name = get_fresh_name()
        _script = '{ret_df} <- separate({table}, {col1}, c("{TMP1}", "{TMP2}"))'.format(
            ret_df=ret_df_name,
            table=args[0],
            col1=str(args[1]),
            TMP1=get_fresh_col(),
            TMP2=get_fresh_col())
        try:
            ret_val = robjects.r(_script)
            return ret_df_name
        except:
            logger.error('Error in interpreting separate...')
            raise GeneralError()
Example #2
0
    def eval_spread(self, node, args):
        n_cols = robjects.r('ncol(' + args[0] + ')')[0]
        first_idx = int(args[1])
        self.assertArg(node, args,
                index=1,
                cond=lambda x: x <= n_cols,
                capture_indices=[0])
        self.assertArg(node, args,
                index=2,
                cond=lambda x: x <= n_cols and x > first_idx,
                capture_indices=[0, 1])

        ret_df_name = get_fresh_name()
        _script = '{ret_df} <- spread({table}, {col1}, {col2})'.format(
                  ret_df=ret_df_name, table=args[0], col1=str(args[1]), col2=str(args[2]))
        try:
            ret_val = robjects.r(_script)
            return ret_df_name
        except:
            logger.error('Error in interpreting spread...')
            raise GeneralError()
Example #3
0
	def eval_select(self, node, args):
		global conn
		# n_cols = robjects.r('ncol(' + args[0] + ')')[0]
		# self.assertArg(node, args, index=1, cond=lambda x: max(list(map(lambda y: int(y), x))) <= n_cols, capture_indices=[0])

		ret_df_name = get_fresh_name()
		_script = '{type} {cols} from {table};'.format(
				   ret_df=ret_df_name, type=str(args[0][0]), table=getNaturalJoins(args[1]), cols=get_collist(args[2]))
		logger.error(_script)
		output = ""
		try:
			c = conn.cursor()
			c.execute(_script)
			rows = c.fetchall()
			for r in rows:
				output += str(r)
			return output
		except sqlite3.Error as er:
			return output
			logger.error('er:', er.message)
			raise GeneralError()
Example #4
0
    def eval_mutate(self, node, args):
        n_cols = robjects.r('ncol(' + args[0] + ')')[0]
        self.assertArg(node,
                       args,
                       index=2,
                       cond=lambda x: x <= n_cols,
                       capture_indices=[0])
        self.assertArg(node,
                       args,
                       index=3,
                       cond=lambda x: x <= n_cols,
                       capture_indices=[0])
        self.assertArg(node,
                       args,
                       index=2,
                       cond=lambda x: get_type(args[0], str(x)) == 'numeric',
                       capture_indices=[0])
        self.assertArg(node,
                       args,
                       index=3,
                       cond=lambda x: get_type(args[0], str(x)) == 'numeric',
                       capture_indices=[0])

        ret_df_name = get_fresh_name()
        _script = '{ret_df} <- {table} %>% mutate({TMP}=.[[{col1}]] {op} .[[{col2}]])'.format(
            ret_df=ret_df_name,
            table=args[0],
            TMP='mutate_a',
            op=args[1],
            col1=str(args[2]),
            col2=str(args[3]))
        # _script = '{ret_df} <- {table} %>% mutate({TMP}=.[[{col1}]] {op} .[[{col2}]])'.format(
        #           ret_df=ret_df_name, table=args[0], TMP=get_fresh_col(), op=args[1], col1=str(args[2]), col2=str(args[3]))
        try:
            ret_val = robjects.r(_script)
            return ret_df_name
        except:
            logger.error('Error in interpreting mutate...')
            raise GeneralError()
Example #5
0
    def eval_group_by(self, node, args):
        n_cols = robjects.r('ncol(' + args[0] + ')')[0]
        self.assertArg(
            node,
            args,
            index=1,
            cond=lambda x: max(list(map(lambda y: int(y), x))) <= n_cols,
            capture_indices=[0])
        # self.assertArg(node, args,
        #         index=1,
        #                cond=lambda x: len(x) == 1,
        #         capture_indices=[0])

        ret_df_name = get_fresh_name()
        _script = '{ret_df} <- group_by_at({table}, {cols})'.format(
            ret_df=ret_df_name, table=args[0], cols=get_collist(args[1]))
        try:
            ret_val = robjects.r(_script)
            return ret_df_name
        except:
            logger.error('Error in interpreting group_by...')
            raise GeneralError()
Example #6
0
 def eval_chr(self, node, args):
     self.typecheck(node, args, [int])
     if args[0] not in range(0x110000):
         raise GeneralError()
     return chr(args[0])
Example #7
0
 def eval_get_mfn(self, node, args):
     if type_checker([type(d) for d in args], [function_types, int]):
         return args[0](args[1])
     else:
         raise GeneralError()
Example #8
0
 def fn_mul(node, args):
     if type_checker([type(d) for d in args], [int]):
         return args[0] * p
     else:
         raise GeneralError()
Example #9
0
 def eval_is_odd(self, node, args):
     if type_checker([type(d) for d in args], [int]):
         return args[0] % 2 != 0
     else:
         raise GeneralError()
Example #10
0
 def eval_lt_zero(self, node, args):
     if type_checker([type(d) for d in args], [int]):
         return args[0] < 0
     else:
         raise GeneralError()
Example #11
0
 def eval_mul(self, node, args):
     if type_checker([type(d) for d in args], [int, int]):
         return args[0] * args[1]
     else:
         raise GeneralError()
Example #12
0
 def eval_neg(self, node, args):
     if type_checker([type(d) for d in args], [int]):
         return -args[0]
     else:
         raise GeneralError()
Example #13
0
 def eval_zipwith(self, node, args):
     # call like "fn( [args] )"
     if type_checker([type(d) for d in args], [function_types, list, list]):
         return [args[0](node, [x, y]) for (x, y) in zip(args[1], args[2])]
     else:
         raise GeneralError()
Example #14
0
 def eval_map(self, node, args):
     # call like "fn( [args] )"
     if type_checker([type(d) for d in args], [function_types, list]):
         return [args[0](node, [d_item]) for d_item in args[1]]
     else:
         raise GeneralError()
Example #15
0
 def eval_sum(self, node, args):
     if type_checker([type(d) for d in args], [list]):
         return sum(args[0])
     else:
         raise GeneralError()
Example #16
0
 def eval_reverse(self, node, args):
     if type_checker([type(d) for d in args], [list]):
         return args[0][::-1]
     else:
         raise GeneralError()