def deleteUser(self, uname, host): # Check if we got valid strings assert ( type(uname) == types.StringType ) and ( type(host) == types.StringType ) # Evaluate the function from the prosody API pro_deleteUser = '******'+uname+'","'+host+'")' lua.eval(pro_deleteUser)
def _validate_input(self, rx, txdigest, txsig, tx): """Do the value-specific validation """ digest = SHA256.new(rx['rxbody']).digest() self.key.validate(rx['signature'], digest) body = json.loads(rx['rxbody']) self._context() lua.eval(body['validate'])(body, digest, txsig, tx) return True
def _import_lua_dependencies(lua, lua_globals): """ Imports lua dependencies that are supported by redis lua scripts. The current implementation is fragile to the target platform and lua version and may be disabled if these imports are not needed. Included: - cjson lib. Pending: - base lib. - table lib. - string lib. - math lib. - debug lib. - cmsgpack lib. """ if sys.platform not in ('darwin', 'windows'): import ctypes ctypes.CDLL('liblua5.2.so', mode=ctypes.RTLD_GLOBAL) try: lua_globals.cjson = lua.eval('require "cjson"') except RuntimeError: raise RuntimeError("cjson not installed")
def getArmPose(self): angles = self.interface.joint_angles() robot_pose = lua.eval("{0, 0, 0, 0, 0, 0, 0}") for key in self.joint_names: robot_pose[self.index_corr[key]] = angles[key] return robot_pose
def main(): # Initialize a RobotInterface object # Use the moveit control by setting the augument to True; joint position control is used by default # franka_arm=RobotInterface(True) franka_arm = RobotInterface() d_pos = lua.eval("{-1.29, -0.26, -0.27, -2.34, 0.12, 2.10, 0.48}") # d_pos=lua.eval("{-1.4, -0.56, -0.37, -2.14, 0.52, 1.80, 0.78}") d_vel = lua.eval("{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1}") # d_vel=lua.eval("{0.0, 0., 0.0, 0.0, 0.0, 0.0, 0.0}") # zero_vel=lua.eval("{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}") franka_arm.setArmPose(d_pos) # while not rospy.is_shutdown(): for x in range(0, 50000): franka_arm.setArmVel(d_vel)
def _python_to_lua(pval, call_args=None): """ Convert Python object(s) into Lua object(s), as at times Python object(s) are not compatible with Lua functions """ import lua if pval is None: # Python None --> Lua None return lua.eval("") if isinstance(pval, (list, tuple, set)): # Python list --> Lua table # e.g.: in lrange # in Python returns: [v1, v2, v3] # in Lua returns: {v1, v2, v3} lua_list = lua.eval("{}") lua_table = lua.eval("table") for item in pval: lua_table.insert(lua_list, Script._python_to_lua(item)) return lua_list elif isinstance(pval, dict): # Python dict --> Lua dict # e.g.: in hgetall # in Python returns: {k1:v1, k2:v2, k3:v3} # in Lua returns: {k1, v1, k2, v2, k3, v3} # else in Python: {k1: v1, k2:v2} # in Lua: {[k1]: v1, [k2]: v2} command = call_args[0] if call_args else None if command == 'HGETALL': lua_dict = lua.eval("{}") lua_table = lua.eval("table") for k, v in pval.iteritems(): lua_table.insert(lua_dict, Script._python_to_lua(k)) lua_table.insert(lua_dict, Script._python_to_lua(v)) else: comps = ['["%s"] = "%s"' % (Script._python_to_lua(k), Script._python_to_lua(v)) for k, v in pval.iteritems()] lua_dict = lua.eval('{%s}' % ','.join(comps)) return lua_dict elif isinstance(pval, (str, unicode)): # Python string --> Lua userdata try: return str(pval) except UnicodeEncodeError: return unicode(pval) elif isinstance(pval, bool): command = call_args[0] if call_args else None # Python bool--> Lua boolean if command == "EXISTS": return lua.eval({True: '1', False: '0'}[pval]) return lua.eval(str(pval).lower()) elif isinstance(pval, (int, long, float)): # Python int --> Lua number lua_globals = lua.globals() return lua_globals.tonumber(str(pval)) raise RuntimeError("Invalid Python type: " + str(type(pval)))
def getArmPose(self): rospy.wait_for_message('/franka_state_controller/franka_states', FrankaState) robot_pose = lua.eval("{0, 0, 0, 0, 0, 0, 0}") for key in self.joint_names: robot_pose[self.index_corr[key]] = self.curr_joint_angles[ self.index_corr[key] - 1] return robot_pose
def to_lua_table(instance): '''If Lua and instance is ``dict``, convert to Lua table.''' if isinstance(instance, dict): table = lua.eval('{}') for key, value in instance.items(): table[to_lua_table(key)] = to_lua_table(value) return table return instance
def to_python(var): """ var is a string got with the 'return var' lua instruction. 'return var' returns the pair: var = <type value>. """ if var == None or var == '': return None var = var.strip() typ = var.split()[0] val = var[len(typ) + 1:] return lua.eval(val)
def to_python(var): """ var is a string got with the 'return var' lua instruction. 'return var' returns the pair: var = <type value>. """ if var == None or var== '': return None var = var.strip() typ = var.split()[0] val = var[len(typ)+1:] return lua.eval(val)
def _validate_update(self, inputs, tx, txhexdigest): self._context() js_output = {} js_input = {} def js_setoutput(idx, s): k = '%s:%d' % (txhexdigest, idx) js_output[SHA256.new(k).hexdigest()] = s def js_setinput(k, s): js_input[k] = s lua.globals().setoutput = js_setoutput lua.globals().setinput = js_setinput f = lua.eval(tx['update']) f(inputs, tx) return js_input, js_output
def _import_lua_dependencies(self, lua, lua_globals): """ Imports lua dependencies that are supported by redis lua scripts. Included: - cjson lib. Pending: - base lib. - table lib. - string lib. - math lib. - debug lib. - cmsgpack lib. """ import ctypes ctypes.CDLL('liblua5.2.so', mode=ctypes.RTLD_GLOBAL) try: lua_globals.cjson = lua.eval('require "cjson"') except RuntimeError: raise RuntimeError("cjson not installed")
def profile( name, file='/with/longbeach/conf/core/database.lua', as_type=None ): code = """ function run(scriptfile) local env = setmetatable({}, {__index=_G}) assert(pcall(setfenv(assert(loadfile(scriptfile)), env))) setmetatable(env, nil) return env end """ import lua lua.execute(code) env = lua.eval( "run('{0}')".format(file) ) databases = env.databases if name in databases: t = databases[name] r = {} for i in t: r[i] = t[i] if as_type=='mysqldb': return get_dbparams(r) elif as_type=='url': return "{adapter}://{username}:{password}@{host}/{database}".format(**r) return r
def _python_to_lua(pval): """ Patch MockRedis+Lua for Python 3 compatibility """ # noinspection PyUnresolvedReferences import lua if pval is None: # Python None --> Lua None return lua.eval('') if isinstance(pval, (list, tuple, set)): # Python list --> Lua table # e.g.: in lrange # in Python returns: [v1, v2, v3] # in Lua returns: {v1, v2, v3} lua_list = lua.eval('{}') lua_table = lua.eval('table') for item in pval: lua_table.insert(lua_list, Script._python_to_lua(item)) return lua_list elif isinstance(pval, dict): # Python dict --> Lua dict # e.g.: in hgetall # in Python returns: {k1:v1, k2:v2, k3:v3} # in Lua returns: {k1, v1, k2, v2, k3, v3} lua_dict = lua.eval('{}') lua_table = lua.eval('table') for k, v in six.iteritems(pval): lua_table.insert(lua_dict, Script._python_to_lua(k)) lua_table.insert(lua_dict, Script._python_to_lua(v)) return lua_dict elif isinstance(pval, tuple(set(six.string_types + (six.binary_type, )))): # type: ignore # Python string --> Lua userdata return pval elif isinstance(pval, bool): # Python bool--> Lua boolean return lua.eval(str(pval).lower()) elif isinstance(pval, six.integer_types + (float, )): # type: ignore # Python int --> Lua number lua_globals = lua.globals() return lua_globals.tonumber(str(pval)) raise RuntimeError('Invalid Python type: ' + str(type(pval)))
def _python_to_lua(pval): """ Convert Python object(s) into Lua object(s), as at times Python object(s) are not compatible with Lua functions """ import lua if pval is None: # Python None --> Lua None return lua.eval("") if isinstance(pval, (list, tuple, set)): # Python list --> Lua table # e.g.: in lrange # in Python returns: [v1, v2, v3] # in Lua returns: {v1, v2, v3} lua_list = lua.eval("{}") lua_table = lua.eval("table") for item in pval: lua_table.insert(lua_list, Script._python_to_lua(item)) return lua_list elif isinstance(pval, dict): # Python dict --> Lua dict # e.g.: in hgetall # in Python returns: {k1:v1, k2:v2, k3:v3} # in Lua returns: {k1, v1, k2, v2, k3, v3} lua_dict = lua.eval("{}") lua_table = lua.eval("table") for k, v in pval.iteritems(): lua_table.insert(lua_dict, Script._python_to_lua(k)) lua_table.insert(lua_dict, Script._python_to_lua(v)) return lua_dict elif isinstance(pval, str): # Python string --> Lua userdata return pval elif isinstance(pval, bool): # Python bool--> Lua boolean return lua.eval(str(pval).lower()) elif isinstance(pval, (int, long, float)): # Python int --> Lua number lua_globals = lua.globals() return lua_globals.tonumber(str(pval)) raise RuntimeError("Invalid Python type: " + str(type(pval)))
def eval(self, event, code): try: result = lua.eval(code) except Exception, e: result = e
from time import sleep from time import time import itertools as it import cv2 import numpy as np import argparse import sys from opencv.cv import * from opencv.highgui import * torch = lua.require('torch') lua.require('trepl') lua.require('cunn') # We run the network on GPU dqn = lua.eval("dofile('dqn/NeuralQLearner.lua')") tt = lua.eval("dofile('dqn/TransitionTable.lua')") #lua.execute("dofile('dqn/Scale.lua')") # for the preproc spectator_action_mapping = { 1 : [0,0,0,0,0,0,0,0,0] , 2 : [1,0,0,0,0,0,0,0,0] , 3 : [0,0,0,0,1,0,0,0,0] , 4 : [0,0,0,0,0,1,0,0,0] , 5 : [0,0,0,0,0,0,1,0,0] , 6 : [0,0,0,0,0,0,0,1,0] , 7 : [0,0,0,0,0,0,0,0,1] , 8 : [0,0,0,0,0,1,0,1,0] , 9 : [1,0,0,0,0,1,0,0,0] , 10 : [0,0,0,0,0,1,1,0,0] , 11 : [1,0,0,0,1,0,0,0,0] ,
def style(rule): colour = lua.eval("%s.Colour" % rule) boldQ = lua.eval("%s.Bold" % rule) bold = r"\bfseries" if boldQ else "" return color(colour) + bold
global matrix matrix = get() prev_matrix = list(matrix) total_game = 0.0 win_game = 0.0 output() print '------INIT-------' for k in tqdm(range(20000)): #time.sleep(1.5) if k!= 0 and k%500 == 0: print "win_rate = ", win_game / total_game #print "total_game = ", total_game reward = 0.0 matrix_converted = bit_conversion(matrix) state_str = '{' + str(matrix_converted).strip('[]') + '}' state = lua.eval(state_str) #forward action_num = Brain.forward(state) while True: move(actions[action_num-1]) if matrix != prev_matrix: break else : reward = -10.0 Brain.backward(reward) print "MOVING IMPOSSIBLE" action_num = Brain.forward(state) insert(matrix) matrix_converted = bit_conversion(matrix) prev_matrix = list(matrix) output()
import gym import lua import numpy as np torch = lua.require('torch') lua.require('trepl') lua.require('cunn') # We run the network on GPU dqn = lua.eval("dofile('dqn/NeuralQLearner.lua')") tt = lua.eval("dofile('dqn/TransitionTable.lua')") lua.execute("dofile('dqn/Scale.lua')") # for the preproc env = gym.make('Breakout-v0') possible_actions = lua.toTable({ 1: 0, 2: 2, 3: 3, 4: 1 }) agent = torch.load("out/net-10000.t7") agent.bestq = 0 observation = env.reset() action_index = 4 done = False t=1 while True: t += 1 env.render() observation, reward, done, info = env.step(possible_actions[action_index])