2006年11月08日 星期三 09:58
ÏÂÃæÊÇpexpect ÖеÄdf.pyµÄÀý×Ó£º #!/usr/bin/env python """This collects filesystem capacity info using the 'df' command. Tuples of filesystem name and percentage are stored in a list. A simple report is printed. Filesystems over 95% capacity are highlighted. Note that this does not parse filesystem names after the first space, so names with spaces in them will be truncated. This will produce ambiguous results for automount filesystems on Apple OSX. """ import pexpect child = pexpect.spawn ('df') # parse 'df' output into a list. pattern = "\n(\S+).*?([0-9]+)%" filesystem_list = [] for dummy in range (0, 1000): i = child.expect ([pattern, pexpect.EOF]) if i == 0: filesystem_list.append (child.match.groups()) else: break # Print report print for m in filesystem_list: s = "Filesystem %s is at %s%%" % (m[0], m[1]) # highlight filesystems over 95% capacity if int(m[1]) > 95: s = '! ' + s else: s = ' ' + s print s ÎÒÔÚ»úÆ÷ÉÏÖ´ÐÐdf ÃüÁ»ñÈ¡ÈçϽá¹û£º Îļþϵͳ 512¿é ÒÑÓà ¿ÉÓà ÈÝÁ¿ °²×°ÔÚ root_domain#root 2097152 222092 1852288 11% / /proc 0 0 0 100% /proc usr_domain#usr 20971520 15316376 5260720 75% /usr usr_domain#var 20971520 205480 5260720 4% /var ÒÀÎÒµÄÀí½â£º pattern = "\n(\S+).*?([0-9]+)%" Ó¦¸ÃÆ¥Å䣺ÒÔ»»ÐпªÊ¼£¬·Ç¿Õ×Ö·û¶à¸ö£¬µ½Êý×Ö¼Ó%½áÊø¡£ Ò²¾ÍÊÇÆ¥Å䣺 root_domain#root 2097152 222092 1852288 11% / µ«Ö´ÐУº child.expect ([pattern, pexpect.EOF]) ºó£¬µÃµ½µÄ½á¹û£º child.match.groups() ÊÇ ['root_domain#root'£¬'11''] ÇëÎÊÕâ¸öÕýÔò±í´ïʽÔõôÀí½â£¿ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061108/2fc48a8d/attachment.html
2006年11月08日 星期三 10:23
Hi 无论我是在linux环境下直接运行python进入编辑页面,还是在window下用自带的python的GUI。python的__doc__方法返回的字符串都是不换行的,其实人家已经写了\n,请问怎么设置能让他按照这个换行那?要把看起来太费劲了
2006年11月08日 星期三 10:35
>>> def temp(): ... ''' docstring1 ... docstring2 ... ''' ... pass ... >>> print temp.__doc__ docstring1 docstring2 >>> On 11/8/06, 天才狐狸 <mem_fox at 263.net> wrote: > > Hi > > 无论我是在linux环境下直接运行python进入编辑页面,还是在window下用自带的python的GUI。python的__doc__方法返回的字符串都是不换行的,其实人家已经写了\n,请问怎么设置能让他按照这个换行那?要把看起来太费劲了 > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- http://codeplayer.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061108/c8bb1fd5/attachment.html
2006年11月08日 星期三 11:08
但是如果直接输入 'A'.__doc__ 这个怎能换行哪?看,输出里面明明有2个\n 'str(object) -> string\n\nReturn a nice string representation of the object.\nIf the argument is a string, the return value is the same object.' ----- Original Message ----- From: yi huang To: python-chinese at lists.python.cn Sent: Wednesday, November 08, 2006 10:35 AM Subject: Re: [python-chinese]__doc__的输出能换行吗? >>> def temp(): ... ''' docstring1 ... docstring2 ... ''' ... pass ... >>> print temp.__doc__ docstring1 docstring2 >>> On 11/8/06, 天才狐狸 <mem_fox at 263.net> wrote: Hi 无论我是在linux环境下直接运行python进入编辑页面,还是在window下用自带的python的GUI。python的__doc__方法返回的字符串都是不换行的,其实人家已经写了\n,请问怎么设置能让他按照这个换行那?要把看起来太费劲了 _______________________________________________ python-chinese Post: send python-chinese at lists.python.cn Subscribe: send subscribe to python-chinese-request at lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -- http://codeplayer.blogspot.com/ _______________________________________________ python-chinese Post: send python-chinese at lists.python.cn Subscribe: send subscribe to python-chinese-request at lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年11月08日 星期三 11:17
pattern = "\n(\S+).*?([0-9]+)%" 应该匹配:以换行开始,一连串非空字符,一连串任意字符,到数字加%结束。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20061108/3bac9603/attachment-0001.htm
2006年11月08日 星期三 11:34
on 2006-11-08 10:00:09 gao. tan(gao.tan at gmail.com) wrote: >下面是pexpect 中的df.py的例子: >#!/usr/bin/env python >"""This collects filesystem capacity info using the 'df' command. >Tuples of filesystem name and percentage are stored in a list. >A simple report is printed. Filesystems over 95% capacity are highlighted. >Note that this does not parse filesystem names after the first space, so >names with spaces in them will be truncated. This will produce ambiguous >results for automount filesystems on Apple OSX. >""" >import pexpect > >child = pexpect.spawn ('df') > ># parse 'df' output into a list. >pattern = "\n(\S+).*?([0-9]+)%" >filesystem_list = [] >for dummy in range (0, 1000): > i = child.expect ([pattern, pexpect.EOF]) > if i == 0: > filesystem_list.append (child.match.groups()) > else: > break > ># Print report >print >for m in filesystem_list: > s = "Filesystem %s is at %s%%" % (m[0], m[1]) > # highlight filesystems over 95% capacity > if int(m[1]) > 95: > s = '! ' + s > else: > s = ' ' + s > print s > > >我在机器上执行df 命令,获取如下结果: >文件系统 512块 已用 可用 容量 安装在 >root_domain#root 2097152 222092 1852288 11% / >/proc 0 0 >0 100% /proc >usr_domain#usr 20971520 15316376 5260720 75% /usr >usr_domain#var 20971520 205480 5260720 4% /var > >依我的理解: > >pattern = "\n(\S+).*?([0-9]+)%" >应该匹配:以换行开始,非空字符多个,到数字加%结束。 >也就是匹配: > >root_domain#root 2097152 222092 1852288 11% / > >但执行: >child.expect ([pattern, pexpect.EOF]) > >后,得到的结果: >child.match.groups() 是 ['root_domain#root','11''] > >请问这个正则表达式怎么理解? >_______________________________________________ pattern = "\n(\S+).*?([0-9]+)%" 应该匹配:以换行开始,一连串非空字符,一连串任意字符,到数字加%结束。
2006年11月08日 星期三 11:35
On 11/8/06, 天才狐狸 <mem_fox在263.net> wrote: > 但是如果直接输入 'A'.__doc__ 这个怎能换行哪?看,输出里面明明有2个\n > 'str(object) -> string\n\nReturn a nice string representation of the object.\nIf the argument is a string, the return value is the same object.' > 直接看的话它只是一个字符串,而换行符不过是一个字符而已。真正的视觉上的换行是需要通过打印输出才看得到的。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年11月08日 星期三 13:00
°´ÕÕchenyang.cq Ëù˵, root_domain#root 2097152 222092 1852288 11% / Ó¦¸ÃÆ¥Åä root_domain#root 2097152 222092 1852288 11% / µ«Êä³öÈ·ÊÇ['root_domain#root'£¬'11''], ÄѵÀÊÇchild.match.groupsÔÙÆð×÷ÓÃ? ÔÚ06-11-8£¬chenyang.cq <chenyang.cq在gmail.com> дµÀ£º > > pattern = "\n(\S+).*?([0-9]+)%" > Ó¦¸ÃÆ¥Å䣺ÒÔ»»ÐпªÊ¼£¬Ò»Á¬´®·Ç¿Õ×Ö·û£¬Ò»Á¬´®ÈÎÒâ×Ö·û£¬µ½Êý×Ö¼Ó%½áÊø¡£ > > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061108/24ae5c17/attachment.html
2006年11月08日 星期三 15:57
On 11/8/06, gao. tan <gao.tan在gmail.com> wrote: > 按照chenyang.cq 所说, > root_domain#root 2097152 222092 1852288 11% / > 应该匹配 > root_domain#root 2097152 222092 1852288 11% / > > 但输出确是['root_domain#root','11''], > 难道是 child.match.groups再起作用? > group()与groups()是两个不同的东西啊。一个是整个匹配结果,一个是分组(括号中的内容)的结果,当然不同了。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
Zeuux © 2025
京ICP备05028076号