Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Cascading ifs

Reply
Thread Tools

Cascading ifs

 
 
=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=
Guest
Posts: n/a
 
      04-02-2007
Hi experts,

How would you do this without the more and more indenting cascade of ifs?:

match = my_regex.search(line)
if match:
doSomething(line)
else:
match = my_regex2.search(line)
if match:
doSomething2(line)
else:
match = my_regex3.search(line)
if match:
doSomething3(line)

etc.

Thanks in advance and regards,
Ernesto
 
Reply With Quote
 
 
 
 
=?ISO-8859-2?Q?Wojciech_Mu=B3a?=
Guest
Posts: n/a
 
      04-02-2007
Ernesto Garca Garca wrote:
> Hi experts,
>
> How would you do this without the more and more indenting cascade of ifs?:
>
> match = my_regex.search(line)
> if match:
> doSomething(line)
> else:
> match = my_regex2.search(line)
> if match:
> doSomething2(line)
> else:
> match = my_regex3.search(line)
> if match:
> doSomething3(line)
>
> etc.


tbl = [(my_regex, doSomething), (my_regex2, doSomething2), (my_regex3,
doSomething3)]
for regex, fun in tbl:
match = regexp.match(line)
if match:
fun(line)
break

w.
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      04-02-2007
Ernesto Garca Garca <(E-Mail Removed)> wrote:

> Hi experts,
>
> How would you do this without the more and more indenting cascade of
> ifs?:
>
> match = my_regex.search(line)
> if match:
> doSomething(line)
> else:
> match = my_regex2.search(line)
> if match:
> doSomething2(line)
> else:
> match = my_regex3.search(line)
> if match:
> doSomething3(line)
>
> etc.
>
> Thanks in advance and regards,
> Ernesto
>


PATTERNS = [
(my_regex, doSomething),
(my_regex2, doSomething2),
(my_regex3, doSomething3),
]
....

for regex, action in PATTERNS:
match = regex.search(line)
if match:
action(line)
break


Also be aware that repeatedly calling the search method with different
regular expressions is horribly inefficient. You would be much better to
combine the regular expressions into one and check which groups match
(although admittedly that behaves differently since it would find the
regex which matches earliest in the string instead of finding the first
regex which matches anywhere).
 
Reply With Quote
 
irstas@gmail.com
Guest
Posts: n/a
 
      04-02-2007
On Apr 2, 4:20*pm, Ernesto Garc*a Garc*a
<(E-Mail Removed)> wrote:
> Hi experts,
>
> How would you do this without the more and more indenting cascade of ifs?:
>
> match = my_regex.search(line)
> if match:
> * *doSomething(line)
> else:
> * *match = my_regex2.search(line)
> * *if match:
> * * *doSomething2(line)
> * *else:
> * * *match = my_regex3.search(line)
> * * *if match:
> * * * *doSomething3(line)
>
> etc.
>
> Thanks in advance and regards,
> Ernesto



You might be able to use "guard clauses":
http://www.refactoring.com/catalog/r...dClauses..html


match = my_regex.search(line)
if match:
* *doSomething(line)
return

match = my_regex2.search(line)
if match:
* *doSomething2(line)
return


But if all of the blocks contain the same code like they do in your
example, you're better off using the solutions provided by Wojciech
Muła and Duncan Booth, because they abstract away the repetition.

 
Reply With Quote
 
=?ISO-8859-2?Q?Ernesto_Garc=EDa_Garc=EDa?=
Guest
Posts: n/a
 
      04-09-2007
> tbl = [(my_regex, doSomething), (my_regex2, doSomething2), (my_regex3,
> doSomething3)]
> for regex, fun in tbl:
> match = regexp.match(line)
> if match:
> fun(line)
> break


Thank you for the idea. This is a bit more difficult when functions need
to work with a common context, but in that case I could store the
context in an object and use the object's methods.

Thanks,
Ernesto
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cascading ifs--style question William Pursell C Programming 13 11-25-2007 10:37 PM
Nested ifs, why does one work but not the other? randomdude@gmail.com VHDL 2 09-30-2005 01:25 PM
IFS to NTFS without loss? Knoppix reformats drives? psion Computer Support 1 09-02-2004 08:07 AM
Drop Down Ifs Continental Translations HTML 4 06-10-2004 11:34 AM
Heterogeneous Container: avoid ifs and casts? Markus Dehmann C++ 8 06-02-2004 09:35 PM



Advertisments