Evaluate my first python script, please
jeanmichel at sequans.com
Fri Mar 5 15:28:49 CET 2010
Duncan Booth wrote:
> Jean-Michel Pichavant <jeanmichel at sequans.com> wrote:
>> You've already been given good advices.
>> Unlike some of those, I don't think using regexp an issue in any way.
>> Yes they are not readable, for sure, I 100% agree to that statement, but
>> you can make them readable very easily.
>> # not readable
>> match = re.search('\d+\.\d+\.\d+\.\d+\s+(\S+)', line)
>> # readable
>> match = re.search('\d+\.\d+\.\d+\.\d+\s+(\S+)', line) # match
>> '192.168.200.1 (foo123)'
>> I gladly understand ppl that are not willing to use regexp (or as last
>> resort), but for a perl guy, that should not be an issue.
> The problem with your comment is that just by existing it means people will
> be misled into thinking the regex is being used to match strings like
> '192.168.200.1 (foo123)'
> when in fact the OP is expecting to match strings like
> '192.168.200.1 foo123'
> i.e. you may mislead some people into thinking the parentheses are part of
> what is being matched when they're actually grouping within the regex.
> Another problem with either regular expression line is that it makes it
> less easy to see that it doesn't do what the OP wanted. /etc/hosts contains
> lines with an IP address followed by multiple hostnames. It may also
> contain comments either as complete lines or at the end of lines. The code
> given will miss hostnames after the first and will include hostnames in
> commented out lines.
And tell me how not using regexp will ensure the /etc/hosts processing
is correct ? The non regexp solutions provided in this thread did not
handled what you rightfully pointed out about host list and commented lines.
And FYI, the OP pattern does match '192.168.200.1 (foo123)'
Ok that's totally unfair :D You're right I made a mistake. Still the
comment is absolutely required (provided it's correct).
More information about the Python-list