Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added python exec program arg capability

  • Loading branch information...
commit c8c0d17f699c27a1611f4f9209cc03616e197737 1 parent dfae3f1
@guruofquality authored
Showing with 33 additions and 0 deletions.
  1. +28 −0 python/pmt_rpc.py
  2. +5 −0 python/qa_pmt_rpc.py
View
28 python/pmt_rpc.py
@@ -43,6 +43,19 @@ class pmt_rpc(gr.block):
Thats the same args, kwargs passed before.
The result is the return value.
Error is null or a string error message.
+
+ Special python exec programming arg:
+ If the arg is a string starting with #!,
+ the handler will execute the string,
+ and the new arg will get the value of
+ the "arg" variable. Example:
+
+ #!
+ import numpy
+ foo = numpy.array([1, 2, 3])
+ bar = sum(foo)
+ arg = max(bar, 0) #arg is the result
+
"""
def __init__(self, obj, result_msg = True):
@@ -71,6 +84,16 @@ def work(self, input_items, output_items):
except Exception as ex: msg.value = pmt.from_python(str(ex))
if self._result_msg: self.post_msg(0, msg)
+ @staticmethod
+ def _exec_arg(arg):
+ if isinstance(arg, str) and arg.startswith('#!'):
+ d = dict()
+ try: exec(arg, d)
+ except: pass
+ try: return d['arg']
+ except KeyError: pass
+ return arg
+
def handle_request(self, fcn_name, request):
#try to parse the request
try:
@@ -81,6 +104,11 @@ def handle_request(self, fcn_name, request):
err = 'cannot parse request for %s, expected tuple of args, kwargs'%fcn_name
return request, None, err
+ #exec python code and squash down to objects
+ args = map(self._exec_arg, args)
+ for key, val in kwargs.iteritems():
+ kwargs[key] = self._exec_arg(val)
+
#fly through dots to get the fcn pointer
try:
fcn_ptr = self._obj
View
5 python/qa_pmt_rpc.py
@@ -29,6 +29,10 @@
try: import pmt
except ImportError: from gruel import pmt
+my_mini_prog = """#!
+arg='hey this works'
+"""
+
class control_block(gr.block):
def __init__(self):
gr.block.__init__(
@@ -56,6 +60,7 @@ def work(self, input_items, output_items):
assert(result == 42*2)
self.post_msg(0, pmt.from_python("_control_block.test_print"), pmt.from_python((('hello',), None)))
+ self.post_msg(0, pmt.from_python("_control_block.test_print"), pmt.from_python(((my_mini_prog,), None)))
return -1
Please sign in to comment.
Something went wrong with that request. Please try again.