def fldtype(self): """return fldtype (qtype) of a scalar""" func_str = q_consts.scalar_func_str.format(fn_name="fldtype") func = executor.eval_lua(func_str) result = func(self.base_scalar) return result
def set_name(self, name): """sets the name of a reducer""" func_str = vec_func_arg_str.format(fn_name="set_name") func = executor.eval_lua(func_str) result = func(self.base_reducer, name) return self
def persist(self, is_persist): """sets the persist flag for vector""" func_str = q_consts.vec_func_arg_str.format(fn_name="persist") func = executor.eval_lua(func_str) result = func(self.base_vec, is_persist) return self
def get_name(self): """returns the name of a vector""" func_str = q_consts.vec_func_str.format(fn_name="get_name") func = executor.eval_lua(func_str) result = func(self.base_vec) return result
def num_elements(self): """returns the num_elements of vector""" func_str = q_consts.vec_func_str.format(fn_name="num_elements") func = executor.eval_lua(func_str) result = func(self.base_vec) return result
def memo(self, is_memo): """sets the memo value for vector""" func_str = q_consts.vec_func_arg_str.format(fn_name="memo") func = executor.eval_lua(func_str) result = func(self.base_vec, is_memo) return self
def get_name(self): """returns the name of a reducer""" func_str = vec_func_str.format(fn_name="get_name") func = executor.eval_lua(func_str) result = func(self.base_reducer) return result
def is_memo(self): """returns memo value for vector""" func_str = q_consts.vec_func_str.format(fn_name="is_memo") func = executor.eval_lua(func_str) result = func(self.base_vec) return result
def call_lua_op(op_name, *args): """ this functions calls the given Q-lua function with specified arguments Parameters: op_name: operation (Q-lua function) name (is a string) args: arguments to be passed to specified function Return: execution result of a function """ # convert the python objects to lua args_table = util.pack_args(args) try: func = executor.eval_lua(q_consts.lua_op_fn_str) result = func(op_name, args_table) except Exception as e: # TODO: Handle operator level failures properly print(str(e)) result = None if result: # wrap the lua objects to python result = util.wrap_output(op_name, result) return result
def __check_and_execute(self, other, operation): """ checks the given value and executes specified scalar operation Parameters: other: a given value with which a scalar is getting operated operation: operation specification (is a string) Returns: the scalar object """ if not (is_p_scalar(other) or isinstance(other, int) or isinstance(other, float)): raise Exception("Second argument type {} is not supported".format(type(other))) # converts int and floats to scalar if isinstance(other, int): other = PScalar(other, q_consts.I8) elif isinstance(other, float): other = PScalar(other, q_consts.F8) else: # it's a PScalar, nothing to do pass other = other.get_base_scalar() func_str = q_consts.scalar_arith_func_str.format(op=operation) func = executor.eval_lua(func_str) result = func(self.base_scalar, other) return PScalar(base_scalar=result)
def abs(self): """convert scalar to absolute""" func_str = q_consts.scalar_func_str.format(fn_name="abs") func = executor.eval_lua(func_str) result = func(self.base_scalar) return result
def conv(self, qtype): """convert scalar to other qtype""" func_str = q_consts.scalar_func_arg_str.format(fn_name="conv") func = executor.eval_lua(func_str) result = func(self.base_scalar, qtype) return self
def qtype(self): """returns the qtype (field type) of vector""" func_str = q_consts.vec_func_str.format(fn_name="qtype") func = executor.eval_lua(func_str) result = func(self.base_vec) return result
def value(self): """returns value of a reducer""" func_str = vec_func_str.format(fn_name="value") func = executor.eval_lua(func_str) result = func(self.base_reducer) return PScalar(base_scalar=result)
def __init__(self, val=None, qtype=None, base_scalar=None): if base_scalar: self.base_scalar = base_scalar else: if not val or not qtype: raise Exception("Provide appropriate argument to PScalar constructor") # create a base scalar object func = executor.eval_lua(q_consts.create_scalar_str) self.base_scalar = func(val, qtype)
def length(self): """ returns the vector length this method is applicable only for eval'ed vectors """ func_str = q_consts.vec_func_str.format(fn_name="length") func = executor.eval_lua(func_str) result = func(self.base_vec) return result
def to_table(in_val): """ converts input list or dict to table Parameters: in_val: a list or dict Returns: returns a lua table """ func = None if type(in_val) == list: func = executor.eval_lua(list_to_table_str) elif type(in_val) == dict: func = executor.eval_lua(dict_to_table_str) in_val = lupa.as_attrgetter(in_val) else: print("Error") return func(in_val)
def to_str(self): """ convert scalar to string Returns: returns a string representation of scalar """ func_str = q_consts.scalar_func_str.format(fn_name="to_str") func = executor.eval_lua(func_str) result = func(self.base_scalar) return result
def set_name(self, name): """ sets the name of a vector Parameters: name: name to be assigned to vector """ func_str = q_consts.vec_func_arg_str.format(fn_name="set_name") func = executor.eval_lua(func_str) result = func(self.base_vec, name) return self
def to_cmem(self): """ convert scalar to cmem Returns: returns cmem object associated with scalar """ func_str = q_consts.scalar_func_str.format(fn_name="to_cmem") func = executor.eval_lua(func_str) result = func(self.base_scalar) return result
def eval(self): """ evaluate the vector Returns: Returns the PVector object """ func_str = q_consts.vec_func_str.format(fn_name="eval") func = executor.eval_lua(func_str) result = func(self.base_vec) return self
def to_num(self): """ converts a scalar to number Returns: returns a numeric value associated with scalar """ func_str = q_consts.scalar_func_str.format(fn_name="to_num") try: func = executor.eval_lua(func_str) result = func(self.base_scalar) except Exception as e: raise Exception("Failed to convert scalar to number") return result
def eval(self): """ evaluates the reducer Returns: returns a tuple of scalars (what we get from Q-lau) """ func_str = vec_func_str.format(fn_name="eval") func = executor.eval_lua(func_str) result = func(self.base_reducer) new_result = [] if type(result) == tuple: for val in result: new_result.append(PScalar(base_scalar=val)) result = tuple(new_result) return result
return t """ q_operators = [str(x) for x in dict(lua_executor.execute_lua(q_op_str)).keys()] # for i in q_operators: # print(i) func_str = \ """ function(a, b) return a + b end """ func = lua_executor.eval_lua(func_str) try: print(func(5, 9)) print(func(None, 9)) except Exception as e: print(str(e)) print("Done") print("=======================") in_val = [1, 2, 3, 4] res = utils.to_table(in_val) print(type(res)) print(list(res)) print(dict(res))