python exec 返回值_当我需要返回值时,python -exec返回一个非类...

exec将始终返回None.从documentation:

exec(object[, globals[, locals]])

This function supports dynamic

execution of Python code. object must be either a string or a code

object. If it is a string, the string is parsed as a suite of Python

statements which is then executed (unless a syntax error occurs). [1]

If it is a code object, it is simply executed. In all cases, the code

that’s executed is expected to be valid as file input (see the section

“File input” in the Reference Manual). Be aware that the return and

yield statements may not be used outside of function definitions even

within the context of code passed to the exec() function. The return

value is None.

这是一个相当奇怪的要求.但你可以像这样捕获输出:

>>> s = """The number {% (c) print(x) %} is a random number between 1 and 6

... inclusive. If we multiply it by 2, we get {% (d) print(2*x) %}.

...

... What's interesting is that the statements may appear out of order in the

... document. {% (a) import random %} Thus I might generate the random

... number in a location in the document well after referencing it.

... {% (b) x = random.randint(1,6) %}"""

>>> import re

>>> stmts = re.findall(r'{%\s*\((\w*)\)\s*(.*)%}',s)

>>> stmts

[('c', 'print(x) '), ('d', 'print(2*x) '), ('a', 'import random '), ('b', 'x = random.randint(1,6) ')]

现在,您必须将输出重定向到某些可以在以后操作的流:

>>> import io

>>> import sys

>>> stream = io.StringIO()

>>> stdout = sys.stdout # this keeps stdout so we can set it back

>>> sys.stdout = stream

>>> for _, statement in sorted(stmts):

... exec(statement)

...

>>> sys.stdout = stdout # remember to reset stdout!

现在,您可以获得打印的值:

>>> stream.getvalue()

'5\n10\n'

>>> stream.getvalue().split()

['5', '10']

虽然,我认为更简单的方法是将命名空间传递给dict:

>>> namespace = {}

>>> for _, statement in sorted(stmts):

... exec(statement, namespace)

...

5

10

>>> namespace.keys()

dict_keys(['__builtins__', 'random', 'x'])

命名空间将加载正常的__builtins__,除非您自己提供.因此,要在执行的代码中创建每个名称,您可以找到namspace.keys dictview和包含字符串“__builtins__”的集合之间的区别.

>>> namespace.keys()

dict_keys(['__builtins__', 'random', 'x'])

>>> vals = namespace.keys() - {'__builtins__'}

>>> vals

{'random', 'x'}

>>> for val in vals:

... print(namespace[val])

...

5

>>>

虽然,如果您使用的是python 3.4> =将stdout重定向到某个流更容易:

>>> import contextlib

>>> stream = io.StringIO()

>>> with contextlib.redirect_stdout(stream):

... for _, statement in stmts:

... exec(statement)

...

>>> stream.getvalue()

'5\n10\n'

>>> stream.getvalue().split()

['5', '10']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将多个点云转化为moveit中的场景(scene)数据,可以通过以下Python非类函数实现: ```python import rospy import moveit_msgs.msg as moveit_msg from sensor_msgs.msg import PointCloud2 import sensor_msgs.point_cloud2 as pc2 import numpy as np def pointcloud_to_scene(cloud_list): # 初始化moveit_msgs CollisionObject类型的场景 scene_msg = moveit_msg.PlanningScene() scene_msg.is_diff = True # 循环遍历点云列表,将每个点云转化为moveit_msgs CollisionObject类型的障碍物 for idx, cloud in enumerate(cloud_list): # 创建一个moveit_msgs CollisionObject类型的障碍物,类型为MESH object_msg = moveit_msg.CollisionObject() object_msg.id = "cloud_obj_" + str(idx) object_msg.header.frame_id = "camera_depth_optical_frame" # 根据具体情况修改 object_msg.operation = object_msg.ADD # 从点云中获取点的坐标数据 points = [] for p in pc2.read_points(cloud, skip_nans=True): points.append([p[0], p[1], p[2]]) points = np.array(points) # 创建一个moveit_msgs Mesh类型的形状 shape_msg = moveit_msg.SolidPrimitive() shape_msg.type = shape_msg.BOX shape_msg.dimensions = [0.1, 0.1, 0.1] # 根据具体情况修改 # 将Mesh形状和点云数据合并为一个障碍物 mesh_msg = moveit_msg.Mesh() mesh_msg.vertices = [moveit_msg.Point(x=p[0], y=p[1], z=p[2]) for p in points] mesh_msg.triangles = [] shape_msg.meshes.append(mesh_msg) object_msg.primitives.append(shape_msg) # 将障碍物添加到场景中 scene_msg.world.collision_objects.append(object_msg) return scene_msg ``` 以上代码中,我们通过`PointCloud2`类型的消息获取点云数据,并将其转化为`moveit_msgs`中的`Mesh`类型的形状数据,最后将转化好的形状和障碍物信息添加到场景中。需要注意的是,代码中的`header.frame_id`和`shape_msg.dimensions`需要根据具体情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值