def ask(code, _fast=None): with ProgramContext(): tree = ast.parse(code, 'ask', 'eval') tree = _transform_ast().visit(tree) code = compile(tree, 'ask', 'eval') newglobals = {} add_symbols(code.co_names, newglobals) parsed_code = eval(code, newglobals) parsed_code = parsed_code.literal() if isinstance(parsed_code, Body) else parsed_code return pyEngine.toAnswer(parsed_code.lua, parsed_code.lua.ask(_fast))
def ask(code, _fast=None): with ProgramContext(): tree = ast.parse(code, 'ask', 'eval') tree = _transform_ast().visit(tree) code = compile(tree, 'ask', 'eval') newglobals = {} add_symbols(code.co_names, newglobals) parsed_code = eval(code, newglobals) parsed_code = parsed_code.literal() if isinstance( parsed_code, Body) else parsed_code return pyEngine.toAnswer(parsed_code.lua, parsed_code.lua.ask(_fast))
def __call__ (self, *args, **kwargs): """ called when compiling p(args) """ "time to create a literal !" if self._pyD_name == 'ask': if 1<len(args): raise RuntimeError('Too many arguments for ask !') fast = kwargs['_fast'] if '_fast' in list(kwargs.keys()) else False literal = args[0] if not isinstance(args[0], Body) else args[0].literal() return pyEngine.toAnswer(literal.lua, literal.lua.ask(fast)) elif self._pyD_name == '_sum': if isinstance(args[0], VarSymbol): return Sum_aggregate(args[0], for_each=kwargs.get('for_each', kwargs.get('key', []))) else: return sum(args) elif self._pyD_name == 'concat': return Concat_aggregate(args[0], order_by=kwargs.get('order_by',kwargs.get('key', [])), sep=kwargs['sep']) elif self._pyD_name == '_min': if isinstance(args[0], VarSymbol): return Min_aggregate(args[0], order_by=kwargs.get('order_by',kwargs.get('key', [])),) else: return min(args) elif self._pyD_name == '_max': if isinstance(args[0], VarSymbol): return Max_aggregate(args[0], order_by=kwargs.get('order_by',kwargs.get('key', [])),) else: return max(args) elif self._pyD_name == 'rank': return Rank_aggregate(None, for_each=kwargs.get('for_each', []), order_by=kwargs.get('order_by', [])) elif self._pyD_name == 'running_sum': return Running_sum(args[0], for_each=kwargs.get('for_each', []), order_by=kwargs.get('order_by', [])) elif self._pyD_name == '_len': if isinstance(args[0], VarSymbol): return Len_aggregate(args[0]) else: return len(args[0]) else: new_args, pre_calculations = [], Body() for arg in args: if isinstance(arg, (Operation, Function, Lambda)): Y = Function.newSymbol() new_args.append(Y) pre_calculations = pre_calculations & (Y == arg) else: new_args.append(arg) literal = Literal.make(self._pyD_name, tuple(new_args)) literal.pre_calculations = pre_calculations return literal
def __call__(self, *args, **kwargs): """ called when compiling p(args) """ "time to create a literal !" if self._pyD_name == 'ask': if 1 < len(args): raise RuntimeError('Too many arguments for ask !') fast = kwargs['_fast'] if '_fast' in list(kwargs.keys()) else False literal = args[0] if not isinstance(args[0], Body) else args[0].literal() return pyEngine.toAnswer(literal.lua, literal.lua.ask(fast)) elif self._pyD_name == '_sum': if isinstance(args[0], VarSymbol): return Sum_aggregate(args[0], for_each=kwargs.get( 'for_each', kwargs.get('key', []))) else: return sum(args) elif self._pyD_name == 'concat': return Concat_aggregate(args[0], order_by=kwargs.get( 'order_by', kwargs.get('key', [])), sep=kwargs['sep']) elif self._pyD_name == '_min': if isinstance(args[0], VarSymbol): return Min_aggregate( args[0], order_by=kwargs.get('order_by', kwargs.get('key', [])), ) else: return min(args) elif self._pyD_name == '_max': if isinstance(args[0], VarSymbol): return Max_aggregate( args[0], order_by=kwargs.get('order_by', kwargs.get('key', [])), ) else: return max(args) elif self._pyD_name == 'rank': return Rank_aggregate(None, for_each=kwargs.get('for_each', []), order_by=kwargs.get('order_by', [])) elif self._pyD_name == 'running_sum': return Running_sum(args[0], for_each=kwargs.get('for_each', []), order_by=kwargs.get('order_by', [])) elif self._pyD_name == '_len': if isinstance(args[0], VarSymbol): return Len_aggregate(args[0]) else: return len(args[0]) else: new_args, pre_calculations = [], Body() for arg in args: if isinstance(arg, (Operation, Function, Lambda)): Y = Function.newSymbol() new_args.append(Y) pre_calculations = pre_calculations & (Y == arg) else: new_args.append(arg) literal = Literal.make(self._pyD_name, tuple(new_args)) literal.pre_calculations = pre_calculations return literal