C-like assignment expression?
flag
Messages 1 - 10 of 26 - Collapse all
/groups/adfetch?adid=5MwbQQ8AAACvmPoWdl6OT3FbUVT39cL3
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  boblat...@googlemail.com  
View profile  
 More options May 21 2008, 5:38 am
Newsgroups: comp.lang.python
From: boblat...@googlemail.com
Date: Wed, 21 May 2008 02:38:10 -0700 (PDT)
Local: Wed, May 21 2008 5:38 am
Subject: C-like assignment expression?
Hello,

I have an if-elif chain in which I'd like to match a string against
several regular expressions. Also I'd like to use the match groups
within the respective elif... block. The C-like idiom that I would
like to use is this:

if (match = my_re1.match(line):
  # use match
elsif (match = my_re2.match(line)):
  # use match
elsif (match = my_re3.match(line))
  # use match

...buy this is illegal in python. The other way is to open up an else:
block in each level, do the assignment and then the test. This
unneccessarily leads to deeper and deeper nesting levels which I find
ugly. Just as ugly as first testing against the RE in the elif: clause
and then, if it matches, to re-evaluate the RE to access the match
groups.

Thanks,
robert


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  Diez B. Roggisch  
View profile  
 More options May 21 2008, 5:46 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Wed, 21 May 2008 11:46:48 +0200
Local: Wed, May 21 2008 5:46 am
Subject: Re: C-like assignment expression?

This might help:

-----------
s = "foo"

class Tester(object):

    def __call__(self, pattern):
        self.m = re.match(pattern, s)
        return self.m is not None

    def __getattr__(self, name):
        return getattr(self.m, name)

test = Tester()

if test("bar"):
    print "wrong"
elif test("foo"):
    print "right"
-------------

Diez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  Ulrich Eckhardt  
View profile  
 More options May 21 2008, 6:14 am
Newsgroups: comp.lang.python
From: Ulrich Eckhardt <eckha...@satorlaser.com>
Date: Wed, 21 May 2008 12:14:22 +0200
Local: Wed, May 21 2008 6:14 am
Subject: Re: C-like assignment expression?

How about this (untested) code:

for re in (re1, re2, re3):
  match = re.match(line)
  if match:
    # use it

This requires that "use it" means the same for each regular expression
though...

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
4.  Bruno Desthuilliers  
View profile  
 More options May 21 2008, 7:14 am
Newsgroups: comp.lang.python
From: Bruno Desthuilliers <bruno.42.desthuilli...@websiteburo.invalid>
Date: Wed, 21 May 2008 13:14:38 +0200
Local: Wed, May 21 2008 7:14 am
Subject: Re: C-like assignment expression?
boblat...@googlemail.com a écrit :

> Hello,

> I have an if-elif chain in which I'd like to match a string against
> several regular expressions. Also I'd like to use the match groups
> within the respective elif... block. The C-like idiom that I would
> like to use is this:

> if (match = my_re1.match(line):
>   # use match
> elsif (match = my_re2.match(line)):
>   # use match
> elsif (match = my_re3.match(line))
>   # use match

<ot>
Isn't it the third or fourth time this very same question pops up here ?
  Starts to look like a FAQ.
</ot>

The canonical solution is to iterate over a list of expression,function
pairs, ie:

def use_match1(match):
    # code here

def use_match2(match):
    # code here

def use_match3(match):
    # code here

for exp, func in [
     (my_re1, use_match1),
     (my_re2, use_match2),
     (my_re3, use_match3)
     ]:
     match = exp.match(line)
     if match:
        func(match)
        break

The alternate solution is Diez's Test object.

HTH


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
5.  Hrvoje Niksic  
View profile  
 More options May 21 2008, 7:47 am
Newsgroups: comp.lang.python
From: Hrvoje Niksic <hnik...@xemacs.org>
Date: Wed, 21 May 2008 13:47:59 +0200
Local: Wed, May 21 2008 7:47 am
Subject: Re: C-like assignment expression?
Bruno Desthuilliers <bruno.42.desthuilli...@websiteburo.invalid>
writes:

> The canonical solution is to iterate over a list of
> expression,function pairs, ie:

Although that solution is pretty, it is not the canonical solution
because it doesn't cover the important case of "if" bodies needing to
access common variables in the enclosing scope.  (This will be easier
in Python 3 with 'nonlocal', though.)  The snippet posted by Diez is
IMHO closer to a canonical solution to this FAQ.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
6.  boblatest@googlemail.com  
View profile  
 More options May 21 2008, 9:12 am
Newsgroups: comp.lang.python
From: "boblat...@googlemail.com" <boblat...@googlemail.com>
Date: Wed, 21 May 2008 06:12:13 -0700 (PDT)
Local: Wed, May 21 2008 9:12 am
Subject: Re: C-like assignment expression?
On May 21, 1:47 pm, Hrvoje Niksic <hnik...@xemacs.org> wrote:

> Although that solution is pretty, it is not the canonical solution
> because it doesn't cover the important case of "if" bodies needing to
> access common variables in the enclosing scope.  (This will be easier
> in Python 3 with 'nonlocal', though.)  The snippet posted by Diez is
> IMHO closer to a canonical solution to this FAQ.

Hello everybody,

thanks for the various answers. I'm actually pretty puzzled because I
expected to see some obvious solution that I just hadn't found before.
In general I find Python more elegant and syntactically richer than C
(that's where I come from), so I didn't expect the solutions to be a
lot more verbose and/or ugly (no offense) than the original idea which
would have worked if Python's assignment statement would double as
expression, as in C.

Thanks again,
robert

PS: Since I'm testing only three REs, and I only need the match
results from one of them, I just re-evaluate that one.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7.  cokofree...@gmail.com  
View profile  
 More options May 21 2008, 9:28 am
Newsgroups: comp.lang.python
From: cokofree...@gmail.com
Date: Wed, 21 May 2008 06:28:35 -0700 (PDT)
Local: Wed, May 21 2008 9:28 am
Subject: Re: C-like assignment expression?
On May 21, 3:12 pm, "boblat...@googlemail.com"

Is it really a lot to change to have it

if my_re1.match(line):
  match = my_re1.match(line)
elseif my_re2.match(line):
  match = my_re2.match(line)
elseif my_re3.match(line):
  match = my_re3.match(line)

?

That reads clearly to me...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
8.  Diez B. Roggisch  
View profile  
 More options May 21 2008, 9:39 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Wed, 21 May 2008 15:39:10 +0200
Local: Wed, May 21 2008 9:39 am
Subject: Re: C-like assignment expression?

And wastes time. regular expressions can become expensive to match - doing
it twice might be hurtful.

Diez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
9.  cokofree...@gmail.com  
View profile  
 More options May 21 2008, 9:43 am
Newsgroups: comp.lang.python
From: cokofree...@gmail.com
Date: Wed, 21 May 2008 06:43:59 -0700 (PDT)
Local: Wed, May 21 2008 9:43 am
Subject: Re: C-like assignment expression?

> And wastes time. regular expressions can become expensive to match - doing
> it twice might be hurtful.

> Diez

match = (my_re1.match(line) or my_re2.match(line)) or
my_re3.match(line)

?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
10.  Diez B. Roggisch  
View profile  
 More options May 21 2008, 10:09 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Wed, 21 May 2008 16:09:47 +0200
Local: Wed, May 21 2008 10:09 am
Subject: Re: C-like assignment expression?

cokofree...@gmail.com wrote:

>> And wastes time. regular expressions can become expensive to match -
>> doing it twice might be hurtful.

>> Diez

> match = (my_re1.match(line) or my_re2.match(line)) or
> my_re3.match(line)

How do you know *which* of the three has matched then?

Diez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2013 Google