python画管柱图_Linux:传送到Python(ncurses)脚本,stdin和termios

#!/usr/bin/env python# http://www.tuxradar.com/content/code-project-build-ncurses-ui-python# http://diveintopython.net/scripts_and_streams/stdin_stdout_stderr.html# http://bytes.com/topic/python/answers/42283-curses-disable-readline-replace-stdin## NOTE: press 'q' to exit curses - Ctrl-C will screw up yer terminal# ./testcurses.py "blabla" # works fine (curseswin shows)# ./testcurses.py - # works fine, (type, enter, curseswins shows):# echo "blabla" | ./testcurses.py "sdsd" # fails to raise curses window## NOTE: when without pipe: termios.tcgetattr(sys.__stdin__.fileno()): [27906, 5, 1215, 35387, 15, 15, ['\x03',# NOTE: when with pipe | : termios.tcgetattr(sys.__stdin__.fileno()): termios.error: (22, 'Invalid argument')importcursesimportsysimportosimportatexitimporttermiosdefopenAnything(source):"""URI, filename, or string --> streamhttp://diveintopython.net/xml_processing/index.html#kgp.diveinThis function lets you define parsers that take any input source(URL, pathname to local or network file, or actual data as a string)and deal with it in a uniform manner. Returned object is guaranteedto have all the basic stdio read methods (read, readline, readlines).Just .close() the object when you're done with it."""ifhasattr(source,"read"):returnsourceifsource=='-':importsysreturnsys.stdin# try to open with urllib (if source is http, ftp, or file URL)importurllibtry:returnurllib.urlopen(source)except(IOError,OSError):pass# try to open with native open function (if source is pathname)try:returnopen(source)except(IOError,OSError):pass# treat source as stringimportStringIOreturnStringIO.StringIO(str(source))defmain(argv):printargv,len(argv)print"stdout/stdin (obj):",sys.__stdout__,sys.__stdin__print"stdout/stdin (fn):",sys.__stdout__.fileno(),sys.__stdin__.fileno()print"env(TERM):",os.environ.get('TERM'),os.environ.get("TERM","unknown")stdin_term_attr=0stdout_term_attr=0try:stdin_term_attr=termios.tcgetattr(sys.__stdin__.fileno())except:stdin_term_attr="%s::%s"%(sys.exc_info()[0],sys.exc_info()[1])try:stdout_term_attr=termios.tcgetattr(sys.__stdout__.fileno())except:stdout_term_attr=`sys.exc_info()[0]`+"::"+`sys.exc_info()[1]`print"stdin_termios_attr",stdin_term_attrprint"stdout_termios_attr",stdout_term_attrfname=""iflen(argv):fname=argv[0]writetxt="Python curses in action!"iffname!="":print"opening",fnamefobj=openAnything(fname)print"obj",fobjwritetxt=fobj.readline(100)# max 100 chars readprint"wr",writetxtfobj.close()print"at end"sys.stderr.write("before ")print"curses",writetxttry:myscreen=curses.initscr()#~ atexit.register(curses.endwin)except:print"Unexpected error:",sys.exc_info()[0]sys.stderr.write("after initscr")# this won't show, even if curseswin runs finemyscreen.border(0)myscreen.addstr(12,25,writetxt)myscreen.refresh()myscreen.getch()#~ curses.endwin()atexit.register(curses.endwin)sys.stderr.write("after end")# this won't show, even if curseswin runs fine# run the main function - with arguments passed to script:if__name__=="__main__":main(sys.argv[1:])sys.stderr.write("after main1")# these won't show either,sys.stderr.write("after main2")# (.. even if curseswin runs fine ..)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值