tag:blogger.com,1999:blog-14462287.post480849078083503494..comments2023-08-27T09:14:32.081-04:00Comments on Speno's Pythonic Avocado: This is getting awk-wardUnknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-14462287.post-70225948934592431042008-04-19T15:16:00.000-04:002008-04-19T15:16:00.000-04:00You could implify the 4-liner try/except block set...You could implify the 4-liner try/except block setting pat to a 1-liner such as:<BR/><BR/>pat = (sys.argv+[None])[1]<BR/><BR/>or (in Python 2.5 or better):<BR/><BR/>pat = sys.argv[1] if len(sys.argv)>1 else None<BR/><BR/>Also, iter(sys.stdin.readline, "") could be simplified into sys.stdout (in an iteration context, a file object is an iterator on the file's textlines).<BR/><BR/>AlexAlexhttps://www.blogger.com/profile/05338312873524129257noreply@blogger.comtag:blogger.com,1999:blog-14462287.post-75294393430185169752008-04-18T11:55:00.000-04:002008-04-18T11:55:00.000-04:00Doh! Ryan squeaked in there while I was trying to ...Doh! Ryan squeaked in there while I was trying to get some sort of indenting to work.Anonymoushttps://www.blogger.com/profile/01328153281272988632noreply@blogger.comtag:blogger.com,1999:blog-14462287.post-13238145182979146522008-04-18T11:48:00.000-04:002008-04-18T11:48:00.000-04:00You could use itertools.cycle and itertools.izip t...You could use itertools.cycle and itertools.izip to get the correct values for green on each line.<BR/><BR/><BR/>#!/usr/bin/python -u<BR/><BR/>import sys, itertools as it<BR/><BR/>try:<BR/> pat = sys.argv[1]<BR/>except IndexError:<BR/> pat = None<BR/><BR/><BR/>for green, line in it.izip(it.cycle((True,False)),iter(sys.stdin.readline, ""):<BR/> if (pat and pat in line) or (not pat and green):<BR/> print "\x1b[7m%s\x1b[0m" % line.rstrip()<BR/> else:<BR/> print line,Anonymoushttps://www.blogger.com/profile/01328153281272988632noreply@blogger.comtag:blogger.com,1999:blog-14462287.post-40467010956542018002008-04-18T11:46:00.000-04:002008-04-18T11:46:00.000-04:00Why don't you use itertools?for line , green in iz...Why don't you use itertools?<BR/><BR/>for line , green in izip( lines , cycle( ( True , False ) ) ):Ryanhttps://www.blogger.com/profile/17320149001761524903noreply@blogger.comtag:blogger.com,1999:blog-14462287.post-58520589981144126512008-04-18T10:16:00.000-04:002008-04-18T10:16:00.000-04:00For kicks, I wrote this up using pyline. It doesn'...For kicks, I wrote this up using <A HREF="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/437932" REL="nofollow">pyline</A>. It doesn't handle the optional pattern, though, so it's not totally equivalent.<BR/><BR/>Matching a pattern:<BR/><BR/>pyline "('pattern' in line or num%2) and '\x1b[7m%s\x1b[0m'%line or line"<BR/><BR/>No matching:<BR/><BR/>pyline "num % 2 and '\x1b[7m%s\x1b[0m' % line or line"<BR/><BR/>If you're interested, I'd adapt pyline to accept additional command line parameters, letting a one-liner work more like your example does, and handle an optional pattern case.Graham Fawcetthttps://www.blogger.com/profile/03670078611251538949noreply@blogger.com