第12.2节 Python sys模块导览

本文深入解析了Python中sys模块的功能和用法,包括sys.argv、sys.stdin、sys.stdout、sys.byteorder等常用成员,以及如何使用这些功能进行系统处理。特别介绍了如何通过sys模块获取和设置递归极限值、获取对象的引用数和大小,以及如何退出Python应用。

sys模块包括一些用于系统处理的功能,常用的成员包括:

  1. sys.argv:当前执行进程的命令参数列表,不含执行程序本身的名字;
  2. sys.stdin 、sys.stdout 和 stderr :分别对应标准输入、标准输出和标准错误的文件对象,后者对于发出警告和错误消息非常有用,即使在 stdout 被重定向后也可以看到它们。对他们的操作类似文件io,如sys.stdin.read(),可以读取输入;
  3. sys.byteorder:本地字节顺序的指示符。在大端序(最高有效位优先)操作系统上值为 ‘big’ ,在小端序(最低有效位优先)操作系统上为 ‘little’;
  4. sys.builtin_module_names:所有的被编译进 Python 解释器的模块;
  5. sys.exit([arg]):退出Python应用及解释器;
  6. sys.getdefaultencoding():返回当前字符串编码名称;
  7. sys.getfilesystemencoding():返回用于转换unicode文件名和字节码文件名的编码名称;
  8. sys.getrefcount(object):返回对象的引用数
  9. sys.getrecursionlimit():返回递归的极限值
  10. sys.setrecursionlimit(limit):设置递归的极限值
  11. sys.getsizeof(object[, default]):返回对象的自己数大小;
  12. sys.modules:返回当前已经加载的模块;
  13. sys.path:系统搜索模块的路径;
  14. sys.platform:返回当前的操作系统平台;
  15. sys.prefix:返回Python安装目录;
  16. sys.setcheckinterval(interval):设置Python检查间隔,参数interval代表每隔interval个Python虚拟指令,用于控制Python的定时任务如信号处理的间隔周期;
  17. sys.version:Python解释器版本;
  18. sys.api_version:解释器c语言api的版本;
  19. sys.version_info:Python解释器版本的详细信息;
  20. sys.winver:windows平台注册的Python版本。

老猿Python,跟老猿学Python!
博客地址:https://blog.youkuaiyun.com/LaoYuanPython

请大家多多支持,点赞、评论和加关注!谢谢!

#!/usr/bin/env python #coding: utf-8 import rospy from geometry_msgs.msg import Point import threading import actionlib import time from actionlib_msgs.msg import GoalStatus from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal from geometry_msgs.msg import PoseWithCovarianceStamped from tf_conversions import transformations from ar_track_alvar_msgs.msg import AlvarMarkers from math import pi from std_msgs.msg import String import sys reload(sys) sys.setdefaultencoding('utf-8') class Navigation: def __init__(self): self.set_pose_pub = rospy.Publisher('/initialpose', PoseWithCovarianceStamped, queue_size=5) self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction) self.move_base.wait_for_server(rospy.Duration(60)) def set_pose(self, p): if self.move_base is None: return False x, y, th = p pose = PoseWithCovarianceStamped() pose.header.stamp = rospy.Time.now() pose.header.frame_id = 'map' pose.pose.pose.position.x = x pose.pose.pose.position.y = y q = transformations.quaternion_from_euler(0.0, 0.0, th/180.0*pi) pose.pose.pose.orientation.x = q[0] pose.pose.pose.orientation.y = q[1] pose.pose.pose.orientation.z = q[2] pose.pose.pose.orientation.w = q[3] self.set_pose_pub.publish(pose) return True def _feedback_cb(self, feedback): msg = feedback #rospy.loginfo("[Navi] navigation feedback\r\n%s"%feedback) def goto(self, p): rospy.loginfo("[Navigation] goto %s"%p) goal = MoveBaseGoal() goal.target_pose.header.frame_id = 'map' goal.target_pose.header.stamp = rospy.Time.now() goal.target_pose.pose.position.x = p[0] goal.target_pose.pose.position.y = p[1] q = transformations.quaternion_from_euler(0.0, 0.0, p[2]/180.0*pi) goal.target_pose.pose.orientation.x = q[0] goal.target_pose.pose.orientation.y = q[1] goal.target_pose.pose.orientation.z = q[2] goal.target_pose.pose.orientation.w = q[3] self.move_base.send_goal(goal, self._done_cb, self._active_cb, self._feedback_cb) result = self.move_base.wait_for_result(rospy.Duration(60)) if not result: self.move_base.cancel_goal() rospy.loginfo("Timed out achieving goal") else: state = self.move_base.get_state() if state == GoalStatus.SUCCEEDED: rospy.loginfo("reach goal %s succeeded!"%p) return True def _done_cb(self, status, result): rospy.loginfo("navigation done! status:%d result:%s"%(status, result)) def _active_cb(self): rospy.loginfo("[Navi] navigation has be actived") def cancel(self): self.move_base.cancel_all_goals() return True class ARTracker: def __init__(self): self.ar_sub = rospy.Subscriber('/ar_pose_marker', AlvarMarkers, self.ar_cb) def ar_cb(self,data): global tag_id for marker in data.markers: tag_id = marker.id class Object_position: def __init__(self): self.ar_sub = rospy.Subscriber('/object_position', Point, self.find_cb) self.tts_pub = rospy.Publisher('/voiceWords', String, queue_size=10) def find_cb(self,data): global find_id point_msg = data if(point_msg.z>=1 and point_msg.z<=5): find_id = 1 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=9 and point_msg.z<=15): find_id = 2 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=16 and point_msg.z<=23): find_id = 3 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=25 and point_msg.z<=26): find_id = 4 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=36 and point_msg.z<=40): find_id = 5 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=41 and point_msg.z<=43): find_id = 6 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=70 and point_msg.z<=71): find_id = 7 self.tts_pub.publish(str(find_id)) elif(point_msg.z>=80 and point_msg.z<=81): find_id = 8 self.tts_pub.publish(str(find_id)) else: find_id = 0 #print("id为0,没有识别到!") def process(): rospy.spin() find_id = 0 tag_id = 0 both_id =0 if __name__ == '__main__': rospy.init_node('navigation_demo',anonymous=True) goalListX = rospy.get_param('~goalListX', '2.0, 2.0,2.0') goalListY = rospy.get_param('~goalListY', '2.0, 4.0,2.0') goalListYaw = rospy.get_param('~goalListYaw', '0, 90.0,2.0') goals = [[float(x), float(y), float(yaw)] for (x, y, yaw) in zip(goalListX.split(","),goalListY.split(","),goalListYaw.split(","))] ##为了方便记忆,goals中[0]是终点,[1]到[8]分别对应场上的8个点,9是第一个框的识别点,10是第二个框的识别点,11是第三个框的识别点,12是第四个框的识别点 object_position = Object_position() ar_acker = ARTracker() executor_thread = threading.Thread(target=process).start() navi = Navigation() find_point_flag=[0,0,0,0] have_nav_flag=[0,0,0,0] time.sleep(10) navi.goto(goals[9]) find_point_flag[0]=1 while True: if find_id==1 or tag_id==1: both_id=1 elif find_id==2 or tag_id==2: both_id=2 elif find_id==3 or tag_id==3: both_id=3 elif find_id==4 or tag_id==4: both_id=4 elif find_id==5 or tag_id==5: both_id=5 elif find_id==6 or tag_id==6: both_id=6 elif find_id==7 or tag_id==7: both_id=7 elif find_id==8 or tag_id==8: both_id=8 else: both_id=0 if both_id==0: if have_nav_flag[0]==1 and find_point_flag[1]==0: navi.goto(goals[10]) find_point_flag[1]=1 if have_nav_flag[1]==1 and find_point_flag[2]==0: navi.goto(goals[11]) find_point_flag[2]=1 if have_nav_flag[2]==1 and find_point_flag[3]==0: navi.goto(goals[12]) find_point_flag[3]=1 if have_nav_flag[3]==1: navi.goto(goals[0]) break else: if both_id==1 and have_nav_flag[0]==0: navi.goto(goals[1]) have_nav_flag[0]=1 if both_id==2 and have_nav_flag[0]==0: navi.goto(goals[2]) have_nav_flag[0]=1 if both_id==3 and have_nav_flag[0]==0: navi.goto(goals[3]) have_nav_flag[1]=1 if both_id==4 and have_nav_flag[0]==0: navi.goto(goals[4]) have_nav_flag[1]=1 if both_id==5 and have_nav_flag[0]==0: navi.goto(goals[5]) have_nav_flag[3]=1 if both_id==6 and have_nav_flag[0]==0: navi.goto(goals[6]) have_nav_flag[3]=1 if both_id==7 and have_nav_flag[0]==0: navi.goto(goals[7]) have_nav_flag[4]=1 if both_id==8 and have_nav_flag[0]==0: navi.goto(goals[8]) have_nav_flag[4]=1
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LaoYuanPython

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值