免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2695 | 回复: 5
打印 上一主题 下一主题

求教脚本语法错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-01 21:56 |只看该作者 |倒序浏览
本帖最后由 小锐同学1 于 2011-06-01 21:57 编辑
  1. [root@LK python]# cat coroutines.py
  2. #!/usr/bin/env python
  3. #coding:utf-8
  4. import time
  5. import sys
  6. ###
  7. #  生成器與協程
  8. ##
  9. ###
  10. #  生成器
  11. #  不斷返回新行
  12. #  @param [in]  f    打開的文件描述符
  13. #  @yield line 新的行
  14. ##
  15. def tail(f):
  16.         f.seek(0,2)     # 移動到文件末尾
  17.                                 # To change the file object’s position, use f.seek(offset, from_what). The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.
  18.         while True:
  19.                 line = f.readline()     # 嘗試讀取一個新的文本行
  20.                 if not line:            # 如果沒有內容,暫時休眠并再次嘗試
  21.                         time.sleep(0.1)
  22.                         continue
  23.                 yield line
  24. ###
  25. #  協程
  26. #  查找匹配關鍵字的輸入行
  27. #  @param [in]  要匹配的關鍵字
  28. #  @print [out] 打印匹配的行
  29. ##
  30. def print_matches(matchtext):
  31.         print "Looking for ",matchtext
  32.         while True:
  33.                 line = (yield)  # 獲取一個文本
  34.                 if matchtext in line:
  35.                         print line

  36. if __name__ == "__main__":
  37.         if len(sys.argv) != 2:
  38.                 sys.exit(1)

  39.         matchers = [ # 一組匹配協程
  40.                 print_matches("python"),
  41.                 print_matches("www.lkcc.info"),
  42.                 print_matches("bbs.lkcc.info")
  43.         ]

  44.         for m in matchers:      # 通過調用next()準備所有的匹配協程, 執行到第一個yield語句
  45.                 m.next()

  46.         f = open(sys.argv[1])
  47.         for line in tail(f):
  48.                 for m in matchers:
  49.                         m.send(line)
复制代码
看似没错,但是执行后就语法错误,找不到错误,求解。
  1. [root@LK python]# ./coroutines.py /var/log/messages
  2.   File "./coroutines.py", line 32
  3.     line = (yield)      # 獲取一個文本
  4.                 ^
  5. SyntaxError: invalid syntax
复制代码
谢谢各位老师

论坛徽章:
0
2 [报告]
发表于 2011-06-01 22:54 |只看该作者
回复 1# 小锐同学1
这个有错误么?看看你的python版本吧。

论坛徽章:
0
3 [报告]
发表于 2011-06-02 08:37 |只看该作者
回复 2# 106033177


    python的版本是 ,红帽5自带的版本。。

论坛徽章:
0
4 [报告]
发表于 2011-06-02 08:51 |只看该作者
回复 3# 小锐同学1
好像2.5开始yield是一个表达式,才可以  val = (yield i) 。

论坛徽章:
0
5 [报告]
发表于 2011-06-02 12:14 |只看该作者
本帖最后由 小锐同学1 于 2011-06-02 12:17 编辑

我再看看

论坛徽章:
0
6 [报告]
发表于 2011-06-02 12:29 |只看该作者
回复 4# 106033177


    编译了一个python3.2rc3之后,那个语句没错了。但是print语句有错,于是改了下, 也不知道对不对,至少没语法错误了,但是,现在说协程没有next这个方法。

http://bbs.chinaunix.net/viewthr ... &extra=page%3D1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP