Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: sobering observation, python vs. perl Date: Thu, 17 Mar 2016 10:52:30 -0500 Lines: 72 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de APpnvE1ON/AntzALceqghw65we8Od6WWRQ1b1k1FXcbg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'difference,': 0.07; 'executed': 0.07; 'line:': 0.07; 'valueerror:': 0.07; '#print': 0.09; 'index': 0.13; 'subject:python': 0.14; '"if"': 0.16; '-tkc': 0.16; 'fd):': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'jumped': 0.16; 'line)': 0.16; 'line),': 0.16; 'match:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'tempted': 0.16; 'wrote:': 0.16; 'string': 0.17; 'try:': 0.18; 'saying': 0.22; '%s"': 0.22; 'aspect': 0.22; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'looks': 0.29; 'print': 0.30; 'code': 0.30; "i'd": 0.31; 'received:10.43': 0.33; 'open': 0.33; 'except': 0.34; 'something': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'charset:us-ascii': 0.37; 'test': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'great': 0.63; 'subject:. ': 0.67; 'smith': 0.76; 'received:23': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1458230140625:2306146544 X-MC-Ingress-Time: 1458230140625 In-Reply-To: X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105103 On 2016-03-17 15:29, Charles T. Smith wrote: > isready = re.compile ("(.*) is ready") > relreq = re.compile (".*release_req") > for fn in sys.argv[1:]: # logfile > name tn = None > with open (fn) as fd: > for line in fd: > #match = re.match ("(.*) is ready", line) > match = isready.match (line) > if match: > tn = match.group(1) > #match = re.match (".*release_req", line) > match = relreq.match (line) > if match: Note that this "match" and "if" get executed for every line > #print "%s: %s" % (tn, line), > print tn > > vs. > > while (<>) { > if (/(.*) is ready/) { > $tn = $1; > } > elsif (/release_req/) { Note this else ^ > print "$tn\n"; > } > } Also, you might just test for string-presence on that second one So what happens if your code looks something like isready = re.compile ("(.*) is ready") for fn in sys.argv[1:]: # logfile name tn = None with open (fn) as fd: for line in fd: match = isready.match (line) if match: tn = match.group(1) elif "release_req" in line: print tn Not saying this will make a great deal of difference, but these two items jumped out at me. I'd even be tempted to just use string manipulations for the isready aspect as well. Something like (untested) IS_READY = " is ready" REL_REQ = "release_req" for n in sys.argv[1:]: tn = None with open(fn) as fd): for line in fd: try: index = line.rindex(IS_READY) except ValueError: if REL_REQ in line: print tn else: tn = line[:index] -tkc