I am trying to set up some code using Pyro to process python code functions on a remote host and get results back. After starting the name server, i would execute this code on the remote host (actually still on localhost):
import Pyro4
class Server(object):
def evaluate(self, func, args):
return func(*args)
def main():
server = Server()
Pyro4.Daemon.serveSimple(
{
server: "server"
},
ns=True)
if __name__ == '__main__':
main()
On the client side i have this code, which is an example of the behaviour i am trying to set up.
import Pyro4
remoteServer = Pyro4.Proxy('PYRONAME:server')
def square(x):
return x**2
print remoteServer.evaluate(square, 4)
However, this code results in the following exception:
/usr/lib/python2.7/site-packages/Pyro4/core.py:155: UserWarning: HMAC_KEY not set,
protocol data may not be secure
warnings.warn("HMAC_KEY not set, protocol data may not be secure")
Traceback (most recent call last):
File "/home/davide/Projects/rempy/example-api-pyro.py", line 7, in <module>
print remoteServer.evaluate(square, 4)
File "/usr/lib/python2.7/site-packages/Pyro4/core.py", line 149, in __call__
return self.__send(self.__name, args, kwargs)
File "/usr/lib/python2.7/site-packages/Pyro4/core.py", line 289, in _pyroInvoke
raise data
AttributeError: 'module' object has no attribute 'square'
It seems to me that the function object is pickled correctly and is sent to the Server instance on the remote host, but there is some problem in the namespace.
How can i solve this problem?
Thanks
I think i know your problem:
the module the function is defiined in is called
'__main__'
it exists in all running versions of python.
pickle does not transfer the source code but a reference
__main__.square
so you have two possibilities:
source square out and make the main module as short as possible such as:
# main.py
def square(x):
return x**2
import Pyro4
def main():
remoteServer = Pyro4.Proxy('PYRONAME:server')
print remoteServer.evaluate(square, 4)
and:
# __main__.py
import main
main.main()
Then the server can import exactly the same module from the file.
or create a module with my code:
class ThisShallNeverBeCalledError(Exception):
pass
class _R(object):
def __init__(self, f, *args):
self.ret = (f, args)
def __reduce__(self):
return self.ret
def __call__(self, *args):
raise ThisShallNeverBeCalledError()
#classmethod
def fromReduce(cls, value):
ret = cls(None)
ret.ret = value
return ret
def dump_and_load(obj):
'''pickle and unpickle the object once'''
s = pickle.dumps(obj)
return pickle.loads(s)
# this string creates an object of an anonymous type that can
# be called to create an R object or that can be reduced by pickle
# and creates another anonymous type when unpickled
# you may not inherit from this MetaR object because it is not a class
PICKLABLE_R_STRING= "type('MetaR', (object,), " \
" {'__call__' : lambda self, f, *args: "\
" type('PICKLABLE_R', "\
" (object,), "\
" {'__reduce__' : lambda self: (f, args), "\
" '__module__' : 'pickleHelp_', "\
" '__name__' : 'PICKLABLE_R', "\
" '__call__' : lambda self: None})(), "\
" '__reduce__' : lambda self: "\
" self(eval, meta_string, "\
" {'meta_string' : meta_string}).__reduce__(), "\
" '__module__' : 'pickleHelp_', "\
" '__name__' : 'R'})()".replace(' ', '')
PICKLABLE_R = _R(eval, PICKLABLE_R_STRING, \
{'meta_string' : PICKLABLE_R_STRING})
R = dump_and_load(PICKLABLE_R)
del PICKLABLE_R, PICKLABLE_R_STRING
PICKLABLE___builtins__ = R(vars, R(__import__, '__builtin__'))
PICKLABLE_FunctionType = R(type, R(eval, 'lambda:None'))
##R.__module__ = __name__
##R.__name__ = 'PICKLABLE_R'
def packCode(code, globals = {}, add_builtins = True, use_same_globals = False, \
check_syntax = True, return_value_variable_name = 'obj',
__name__ = __name__ + '.packCode()'):
'''return an object that executes code in globals when unpickled
use_same_globals
if use_same_globals is True all codes sent through
one pickle connection share the same globals
by default the dont
return_value_variable_name
if a variable with the name in return_value_variable_name exists
in globals after the code execution
it is returned as result of the pickling operation
if not None is returned
__name__
'''
if check_syntax:
compile(code, '', 'exec')
# copying locals is important
# locals is transferred through pickle for all code identical
# copying it prevents different code from beeing executed in same globals
if not use_same_globals:
globals = globals.copy()
if add_builtins:
globals['__builtins__'] = PICKLABLE___builtins__
globals.setdefault('obj', None)
# get the compilation code
# do not marshal or unmarshal code objects because the platforms may vary
code = R(compile, code, __name__, 'exec')
# the final object that can reduce, dump and load itself
obj = R(R(getattr, tuple, '__getitem__'), (
R(R(PICKLABLE_FunctionType, code, globals)),
R(R(getattr, type(globals), 'get'), globals, \
returnValueVariableName, None)
), -1)
return obj
and then send this to the other side:
packCode('''
def square(...):
...
''', return_value_variable_name = 'square')
and the function will come out on the other side, no module code is needed to transefer this python function to the other server side.
If something does not work out please tell me.
Related
Introduction
I am developping a sublime text 3 package.
Suddenly, the command I am adding the the package broke. It throws every time the following error in the console:
Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\Lib\python33\sublime_plugin.py", line 1456, in run_
return self.run(**args)
TypeError: run() got an unexpected keyword argument 'redeploy_module'
This appears in "dirty" sublime as well as in clean state.
This happens when I add a list input handler.
This works
class ProjectNameInputHandler(sublime_plugin.TextInputHandler):
def placeholder(self):
return "Project's name - must already exist"
# def next_input(self, args):
# if 'module' not in args:
# return RedeployModuleInputHandler()
# class RedeployModuleInputHandler(sublime_plugin.ListInputHandler):
# def placeholder(self):
# return "Module to create template for"
# def list_items(self):
# return [("Manager (v1)", "manager"),
# ("Projects (v2)", "projects")]
class RedeployJobCommand(sublime_plugin.WindowCommand):
def run(self, project_name):
plugin_path = str(os.path.dirname(__file__))
template_cli = "\"{}\\template-python-cli\\cli.py\""\
.format(plugin_path)
folder = self.window.extract_variables()['folder']
args = "redeployJob {}".format(project_name)
command = "echo 'python {} {}'".format(template_cli, args)
self.window.run_command("exec", {
"shell_cmd": command,
"working_dir": folder
})
def input(self, args):
if 'project_name' not in args:
return ProjectNameInputHandler()
# elif 'module' not in args:
# return RedeployModuleInputHandler()
This does not work
import sublime_plugin
import os
class ProjectNameInputHandler(sublime_plugin.TextInputHandler):
def placeholder(self):
return "Project's name - must already exist"
def next_input(self, args):
if 'module' not in args:
return RedeployModuleInputHandler()
class RedeployModuleInputHandler(sublime_plugin.ListInputHandler):
def placeholder(self):
return "Module to create template for"
def list_items(self):
return [("Manager (v1)", "manager"),
("Projects (v2)", "projects")]
class RedeployJobCommand(sublime_plugin.WindowCommand):
def run(self, project_name, module):
plugin_path = str(os.path.dirname(__file__))
template_cli = "\"{}\\template-python-cli\\cli.py\""\
.format(plugin_path)
folder = self.window.extract_variables()['folder']
args = "redeployJob {}".format(project_name)
command = "echo 'python {} {}'".format(template_cli, args)
self.window.run_command("exec", {
"shell_cmd": command,
"working_dir": folder
})
def input(self, args):
if 'project_name' not in args:
return ProjectNameInputHandler()
elif 'module' not in args:
return RedeployModuleInputHandler()
It throws the error described in the introduction.
I do not understand why it does that. What I am doing wrong ?
In
def run(self, project_name, module):
rename module to redeploy_module
def run(self, project_name, redeploy_module):
I am very new in python cffi. I have to access my temprature module by using its Index or with its channel name. I am trying with both as you can see in my QmixTC class. I am getting attribute error. In other class, there is no errors. Can someone help me understand where is the problem. I am putting my code as well as error trace. Thanks.
main code with name qmix.py (importing it in to sample code):
class QmixTC (object):
"""
"""
def __init__(self, index=0, handle=None,name=''):
self.dll_dir = DLL_DIR
self.dll_file = os.path.join(self.dll_dir,
'labbCAN_Controller_API.dll')
self._ffi = FFI()
self._ffi.cdef(CONTROLLER_HEADER)
self._dll = self._ffi.dlopen(self.dll_file)
self._handle = self._ffi.new('dev_hdl *', 0)
if handle is None:
self.index = index
self._handle = self._ffi.new('dev_hdl *', 0)
self._call('LCC_GetChannelHandle', self.index, self._handle)
else:
self.index = None
self._handle = handle
self._ch_name="QmixTC_1_DO0_INA"
self._channel = self._ch_name + str(index)
self._call('LCC_LookupChanByName',
bytes(self._channel,'utf8'),
self._handle)
self.name = name
def _call(self, func_name, *args):
func = getattr(self._dll, func_name)
r = func(*args)
r = CHK(r, func_name, *args)
return r
def Setpoint_write (self, setpoint):
"""
Write setpoint value to controller device.
Parameters
[in] ChanHdl Valid handle of open controller channel
[in] fSetPointValue The setpoint value to write
Returns
Error code - ERR_NOERR indicates success
"""
self._call('LCC_WriteSetPoint', self._handle[0], setpoint)
def enable_controllLoop (self, enable):
"""
Enables / disables a control loop.
If the control loop is enabled, then the output value is calculated periodically.
Parameters
ChanHdl Valid handle of a controller channel
Enable 1 = enable, 0 = disable
Returns
Error code - ERR_NOERR indicates success
"""
self._call('LCC_EnableControlLoop', self._handle[0], enable)
def read_temp_value (self, actualvalue):
"""
Read actual value from device.
Parameters
[in] ChanHdl Valid handle of open controller channel
[out] pfActualValue Returns the actual controller value
Returns
Error code - ERR_NOERR indicates success
"""
self._call('LCC_ReadActualValue', self._handle[0], actualvalue)
if __name__ == '__main__':
import os.path as op
dll_dir = op.normpath('C:\\Users\\Ravikumar\\AppData\\Local\\QmixSDK')
config_dir = op.normpath('C:\\Users\\Public\\Documents\\QmixElements\\Projects\\QmixTC_Pump\\Configurations\\QmixTC_pump')
bus = QmixBus(config_dir=config_dir)
bus.open()
bus.start()
controller_0 = QmixTC(index=0)
controller_0.enable_controllLoop(1)
sample program:
from __future__ import division, print_function
from win32api import GetSystemMetrics
import numpy as np
import os
import qmix
import pandas as pd
#%% CHANNEL INITIALIZATION
if __name__ == '__main__':
dll_dir = ('C:\\Users\\Ravikumar\\AppData\\Local\\QmixSDK')
config_dir = ('C:\\Users\\Public\\Documents\\QmixElements\\Projects\\QmixTC_test1\\Configurations\\QmixTC_test1')
qmix_bus = qmix.QmixBus(config_dir=config_dir,dll_dir=dll_dir)
qmix_bus.open()
qmix_bus.start()
controller_0 = qmix.QmixTC(index=0)
controller_0.Setpoint_write(50)
error:
Traceback (most recent call last):
File "<ipython-input-5-40d4a3db9493>", line 17, in <module>
controller_0 = qmix.QmixTC(index=0)
File "qmix.py", line 921, in __init__
self._call('LCC_GetChannelHandle', self.index, self._handle)
File "qmix.py", line 937, in _call
func = getattr(self._dll, func_name)
File "C:\Users\Ravikumar\Anaconda2\lib\site-packages\cffi\api.py", line 875, in __getattr__
make_accessor(name)
File "C:\Users\Ravikumar\Anaconda2\lib\site-packages\cffi\api.py", line 870, in make_accessor
raise AttributeError(name)
AttributeError: LCC_GetChannelHandle
I'm working on a project and this error keeps showing up:
class PRNG:
def __init__(self):
# parameters
# P-256 prime
self.p=115792089210356248762697446949407573530086143415290314195533631308867097853951
self.a=self.p-3
self.b=41058363725152142129326129780047268409114441015993725554835256314039467401291
self.E=curve(self.a,self.b,self.p)
self.E.n=115792089210356248762697446949407573529996955224135760342422259061068512044369
self.P=point(0,46263761741508638697010950048709651021688891777877937875096931459006746039284)
self.k=183521403747637560534595403690771364941493702673414885451510208165414833985
self.Q=mult(self.k,self.P,self.E)
self.t=bytes_to_int(os.urandom(32)) # initial seed
#print self.t
self.output_length=240
self.truncate_length=16
def function_attack(self, nbytes, predicted_state):
calls = ((nbytes*8-1)/self.output_length)+1
out = ''
for i in xrange(calls):
tP=mult(predicted_state,self.P,self.E)
s=tP.x
sQ=mult(s,self.Q,self.E)
r=sQ.x
r_out=r % (2**self.output_length)
self.t=s
out = out + int_to_bytes(r_out, self.output_length/8)
return out[:nbytes]
def function(self, nbytes):
calls = ((nbytes*8-1)/self.output_length)+1
out = ''
for i in xrange(calls):
tP=mult(self.t,self.P,self.E)
s=tP.x
sQ=mult(s,self.Q,self.E)
r=sQ.x
r_out=r % (2**self.output_length)
self.t=s
out = out + int_to_bytes(r_out, self.output_length/8)
return out[:nbytes]
The first method is being called in a separate file and the output is always the following(regardless if I change the name of the local variable calls):
File "C:\file1.py", line 32, in <module>
prng = PRNG()
File "C:\file_where_error_occurs.py", line 286, in __init__
for i in xrange(calls):
NameError: global name 'calls' is not defined
What is python doing?
you have indentation errors ... this is typically caused by mixing tabs and spaces ... most decent editors can fix this for you easily ... to see your indentation errors run your program as
python -tt my_program.py
I'm using Nose and Fudge for unit testing. Consider the following class:
class Foo():
def __init__(self, some_commandline):
self._some_commandline = commandline
def process(self):
stdout, stderr = self._commandline()
...
And a test:
def test_process_commandline(self):
import StringIO
# Setup
fake_stdout = StringIO.StringIO()
fake_stderr = StringIO.StringIO()
fake_stdio = fake_stdout, fake_stderr
fake_cline = (fudge
.Fake('SomeCommandline')
.is_a_stub()
.provides('__call__')
.returns(fake_stdio))
sut = Foo(fake_cline)
# Exercise
sut.process()
# Verify
...
The error I get is:
...
stdout, stderr = self._commandline()
TypeError: 'Fake' object is not iterable
The code I'm stubbing has a return line that looks like this (the real version of "SomeCommandline")
return stdout_str, stderr_str
Why am I getting the TypeError saying Fake is not iterable, and how do i stub this method with fudge?
The stub should be setup using .is_callable() instead of .provides('__call__'):
fake_cline = (fudge
.Fake('SomeCommandline')
.is_callable()
.returns(fake_stdio))
Also, .is_a_stub() is not needed here because we are stubbing out the method, __call__, directly, which is accessed via the class name SomeCommandLine.
Sorry if this question is stupid. I created an unittest class which needs to take given inputs and outputs from outside. Thus, I guess these values should be initiated. However, I met some errors in the following code:
CODE:
import unittest
from StringIO import StringIO
##########Inputs and outputs from outside#######
a=[1,2]
b=[2,3]
out=[3,4]
####################################
def func1(a,b):
return a+b
class MyTestCase(unittest.TestCase):
def __init__(self,a,b,out):
self.a=a
self.b=b
self.out=out
def testMsed(self):
for i in range(self.tot_iter):
print i
fun = func1(self.a[i],self.b[i])
value = self.out[i]
testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("func1",i,value,fun)
self.assertEqual(round(fun,3),round(value,3),testFailureMessage)
if __name__ == '__main__':
f = MyTestCase(a,b,out)
from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream, verbosity=2)
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
print 'Tests run', result.testsRun
However, I got the following error
Traceback (most recent call last):
File "C:testing.py", line 33, in <module>
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
File "C:\Python27\lib\unittest\loader.py", line 310, in makeSuite
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
File "C:\Python27\lib\unittest\loader.py", line 50, in loadTestsFromTestCase
if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class
Can anyone give me some suggestions? Thanks!
The root of the problem is this line,
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
unittest.makeSuite expects a class, not an instance of a class. So just MyTestCase, not MyTestCase(a, b, out). This means that you can't pass parameters to your test case in the manner you are attempting to. You should probably move the code from init to a setUp function. Either access a, b, and out as globals inside setUp or take a look at this link for information regarding passing parameters to a unit test.
By the way, here is the source file within python where the problem originated. Might be informative to read.