Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(at': 0.04; 'skip:[ 20': 0.04; 'sufficient': 0.05; 'expressions': 0.07; 'string': 0.09; 'literal': 0.09; 'parsing': 0.09; 'solution,': 0.09; 'effect.': 0.16; 'localhost': 0.16; 'mangled': 0.16; 'readable': 0.16; 'roy': 0.16; 'too).': 0.16; 'files.': 0.16; ':-)': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; "python's": 0.19; 'rules': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'skip:{ 20': 0.24; 'together.': 0.24; '(or': 0.24; 'suggested': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; '----': 0.29; 'cool': 0.30; 'direction': 0.30; 'code': 0.31; 'easier': 0.31; 'writes:': 0.31; 'regular': 0.32; 'run': 0.32; 'running': 0.33; 'received:209.85': 0.35; 'tool': 0.35; 'received:209.85.160.46': 0.35; 'received:google.com': 0.35; 'received:209': 0.37; 'jason': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'read': 0.60; 'expression': 0.60; 'skip:2 20': 0.60; 'skip:n 30': 0.60; 'up,': 0.60; 'break': 0.61; 'challenge': 0.61; 'making': 0.63; 'skip:r 40': 0.68; 'smith': 0.68; 'skip:r 30': 0.69; 'article': 0.77; 'received:117': 0.78; 'message-id:@mx.google.com': 0.80; '(probably': 0.84; 'embrace': 0.84; 'noise': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:subject:in-reply-to:references:user-agent :date:message-id:mime-version:content-type; bh=ijYRuEkAvnw9t+3mWpkZR4VS/TVdyMZJbwpMFqwRKVw=; b=mdkov0zaaganMK3Ael4H5OfqAAySUAUoy5LzTM4mIDlXHs/fX4dbugfzlXNUrbf8IA 673tIWihVc4yLKKjx5SNQjnvGmd9cVtomZfO193q0WNw6EItbaBju+EvwxD44wbK64v/ uE9+OxXWegnN+GlkbQ5tdNSRI+iU56svurR2+RP2dcD9t5elLLCBaMrIV5WAgDGnj7A1 PiCctXU4YycnepkTkLU3fuhkYOyVywwjHRhZ5RFFYsClbQib7AQew58KRzNR1PQvw2ki XnvVK4FuB89lSKBGSSVPMNo3jav8s37nZBNc3rgTmOd8CkxZw7Ha+lfqZLhuRAtb7eVx IdhA== X-Received: by 10.66.6.42 with SMTP id x10mr10707234pax.218.1365096858955; Thu, 04 Apr 2013 10:34:18 -0700 (PDT) Sender: Kushal Kumaran From: Kushal Kumaran To: python-list@python.org Subject: Re: In defence of 80-char lines In-Reply-To: References: <515cd919$0$29966$c3e8da3$5496439d@news.astraweb.com> User-Agent: Notmuch/0.13.2 (http://notmuchmail.org) Emacs/24.1.1 (x86_64-pc-linux-gnu) Date: Thu, 04 Apr 2013 23:04:07 +0530 MIME-Version: 1.0 Content-Type: text/plain X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365096868 news.xs4all.nl 6945 [2001:888:2000:d::a6]:51292 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42770 Roy Smith writes: > In article , > Jason Swails wrote: > >> The only time I regularly break my rule is for regular expressions (at some >> point I may embrace re.X to allow me to break those up, too). > > re.X is a pretty cool tool for making huge regexes readable. But, it > turns out that python's auto-continuation and string literal > concatenation rules are enough to let you get much the same effect. > Here's a regex we use to parse haproxy log files. This would be utter > line noise all run together. This way, it's almost readable :-) > > pattern = re.compile(r'haproxy\[(?P\d+)]: ' > r'(?P(\d{1,3}\.){3}\d{1,3}):' > r'(?P\d{1,5}) ' > > r'\[(?P\d{2}/\w{3}/\d{4}(:\d{2}){3}\.\d{3})] ' > r'(?P\S+) ' > r'(?P\S+)/' > r'(?P\S+) ' > r'(?P(-1|\d+))/' > r'(?P(-1|\d+))/' > r'(?P(-1|\d+))/' > r'(?P(-1|\d+))/' > r'(?P\+?\d+) ' > r'(?P\d{3}) ' > r'(?P\d+) ' > r'(?P\S+) ' > r'(?P\S+) ' > r'(?P[\w-]{4}) ' > r'(?P\d+)/' > r'(?P\d+)/' > r'(?P\d+)/' > r'(?P\d+)/' > r'(?P\d+) ' > r'(?P\d+)/' > r'(?P\d+) ' > r'(\{(?P.*?)\} )?' > r'(\{(?P.*?)\} )?' > r'(\{(?P.*?)\} )?' > r'"(?P.+)"' > ) > > And, for those of you who go running in the other direction every time > regex is suggested as a solution, I challenge you to come up with easier > to read (or write) code for parsing a line like this (probably > hopelessly mangled by the time you read it): > > 2013-04-03T00:00:00+00:00 localhost haproxy[5199]: 10.159.19.244:57291 > [02/Apr/2013:23:59:59.811] app-nodes next-song-nodes/web8.songza.com > 0/0/3/214/219 200 593 sessionid=NWiX5KGOdvg6dSaA > sessionid=NWiX5KGOdvg6dSaA ---- 249/249/149/14/0 0/0 > {4C0ABFA9-515B6DEF-933229} "POST > /api/1/station/892337/song/16024201/notify-play HTTP/1.0" Is using csv.DictReader with delimiter=' ' not sufficient for this? I did not actually read the regular expression in its entirety. -- regards, kushal