Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109235

Re: re.search - Pattern matching review

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Matt Wheeler <m@funkyhat.org>
Newsgroups comp.lang.python
Subject Re: re.search - Pattern matching review
Date Sun, 29 May 2016 18:02:46 +0100
Lines 79
Message-ID <mailman.23.1464541914.1839.python-list@python.org> (permalink)
References <CACT3xuVzgYxA_Yb4LngimMpp1zhBAz5qwY1o0qcoYZ+WMtUdAQ@mail.gmail.com> <CAG93HwHRh2F9dcMoZarZkradj659hyccaoMcCF9gWHc2zioaAQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de FoyPll3pWKvckq6f3Zl00Q7Jk/XhC4am6kZOz/cFvOHQ==
Return-Path <m@funkyhat.org>
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; 'ideally': 0.04; 'none,': 0.05; 'none:': 0.05; 'retrieved': 0.05; 'false,': 0.07; '"%s"': 0.09; 'cmd': 0.09; 'matched': 0.09; 'stdout': 0.09; 'tuple': 0.09; 'python': 0.10; '2.7': 0.13; 'exception': 0.13; 'output': 0.13; 'def': 0.13; 'suggest': 0.15; 'explicitly': 0.15; '(pdb)': 0.16; '2016': 0.16; 'buggy': 0.16; 'e))': 0.16; 'ioerror,': 0.16; 'magic': 0.16; 'reached,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'try/except': 0.16; 'wrote:': 0.16; 'skip:` 10': 0.18; 'try:': 0.18; 'to:name:python-list@python.org': 0.20; 'trying': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; '(which': 0.26; 'linux': 0.26; 'message- id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'looks': 0.29; 'attempting': 0.29; 'implicitly': 0.29; 'raise': 0.29; 'code': 0.30; 'probably': 0.31; 'extract': 0.33; "skip:' 20": 0.34; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'false': 0.35; 'friends,': 0.35; 'i.e.': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'too': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'seem': 0.37; 'doing': 0.38; 'skip:- 20': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; "you'll": 0.61; 'address': 0.61; 'reach': 0.61; 'matter': 0.63; 'statement,': 0.66; 'here': 0.66; 'dear': 0.67; 'bulk': 0.76; 'construct': 0.84; 'from:addr:m': 0.84; 'subject:review': 0.84
X-Virus-Scanned Debian amavisd-new at membrane.funkyhat.net
X-Gm-Message-State ALyK8tKTJjnc35PoQBy7hAOdW3/HLwwUBqxI6MRP/0uhZ7jfSWinCoLK5Ia2TLMB5eSfTLd4ENzqqOS8knrtBA==
X-Received by 10.194.176.167 with SMTP id cj7mr20060070wjc.36.1464541386251; Sun, 29 May 2016 10:03:06 -0700 (PDT)
In-Reply-To <CACT3xuVzgYxA_Yb4LngimMpp1zhBAz5qwY1o0qcoYZ+WMtUdAQ@mail.gmail.com>
X-Gmail-Original-Message-ID <CAG93HwHRh2F9dcMoZarZkradj659hyccaoMcCF9gWHc2zioaAQ@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAG93HwHRh2F9dcMoZarZkradj659hyccaoMcCF9gWHc2zioaAQ@mail.gmail.com>
X-Mailman-Original-References <CACT3xuVzgYxA_Yb4LngimMpp1zhBAz5qwY1o0qcoYZ+WMtUdAQ@mail.gmail.com>
Xref csiph.com comp.lang.python:109235

Show key headers only | View raw


On 28 May 2016 at 19:12, Ganesh Pal <ganesh1pal@gmail.com> wrote:
> Dear Python friends,
>
> I am  on Python 2.7 and Linux . I am trying to extract the address
> "1,5,147456:8192" from the below stdout using re.search
>
> (Pdb) stdout
> 'linux-host-machine-1: Block Address for 1,5,27320320:8192 (block
> 1,5,147456:8192) --\nlinux-host-machine-1: magic
> 0xdeaff2fe         mark_cookie 0x300000000000000a\n'
> (Pdb) type(stdout)
> <type 'str'>
>
> Here is the code I have come up with, this looks buggy please review
> the same and suggest any better ways  to code the same
>
> the same may be splitlines() , re.complie() etc , my intention is to
> just match 1,5,147456:8192 and return the same.

This doesn't seem to exactly match your code below, i.e. your code is
attempting to construct a tuple from groups 1 through 4. To meet this
specification I could just `return re.search('(?<=\(block
)[^(]*(?=\))', stdout).group()`

> #Sample code
>
> import re
> import subprocess_run
>
> def get_block():

First point: your try block is too big. It should ideally contain just
the line that might raise the exception you're catching.

>     try:
>         cmd = "get_block_info -l"
>         # stdout is the output retrieved by subprocess.Popen()
>         stdout, stderr, exitcode = subprocess_run(cmd)
>         search_pat = 'Block Address.* \(block (\d+),(\d+),(\d+):(\d+)'
>         matched = re.search(search_pat, stdout)
>         block = (int(matched.group(1)),
>                    int(matched.group(2)),
>                    int(matched.group(3)),
>                    int(matched.group(4)),
>                   )

You could reduce repetition here by doing something like `tuple(int(n)
for n in matched.groups())`

>     except IOError, e:
>         logging.warning('Error reading lines from "%s" (%s).'
>                 % (cmd, e))
>
>     if block is None:
>        logging.error("block not found")

But this will never be reached, because when you try to get the groups
from `matched` above it will be None, so you'll get an AttributeError.
Consider revising (after moving the bulk of your code out of your
try/except block) to something like:

(run and re.search)
if matched:
    block = ....
    return block
else:
    logging.info("block not found")

>        return False
>     logging.info("block not found")
>     return block

And you probably don't need to explicitly return false, python
functions implicitly return None (which is falsey) if they don't reach
a return statement, but that's perhaps a matter of taste.

-- 
Matt Wheeler
http://funkyh.at

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: re.search - Pattern matching review Matt Wheeler <m@funkyhat.org> - 2016-05-29 18:02 +0100

csiph-web