In defence of 80-char lines
Jason Swails
jason.swails at gmail.com
Thu Apr 4 09:23:37 EDT 2013
On Thu, Apr 4, 2013 at 8:39 AM, Roy Smith <roy at panix.com> wrote:
> In article <mailman.96.1365077619.3114.python-list at python.org>,
> Jason Swails <jason.swails at gmail.com> 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<pid>\d+)]: '
> r'(?P<client_ip>(\d{1,3}\.){3}\d{1,3}):'
> r'(?P<client_port>\d{1,5}) '
>
For some reason that never occurred to me. I use this technique every
other time I want to break up a long string, but never for regexes...
Now I will. I was wary of using re.X since I sometimes use meaningful
whitespace in my regexes, and I didn't want to have to figure out how to
prevent them from being ignored... This is a much better solution.
Thanks,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130404/e1fc5dbf/attachment.html>
More information about the Python-list
mailing list