Re: Cascading ifs

2007-04-09 Thread Ernesto García García
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cascading ifs

2007-04-05 Thread Sick Monkey

If it is just the indentation that is bothering you, you could do this:

match = test
if match == 1:
   print 1
elif match == 2:
   print 2
elif match == test:
   print test

On 4/2/07, Ernesto García García [EMAIL PROTECTED] 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
--
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list

Cascading ifs

2007-04-02 Thread Ernesto García García
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cascading ifs

2007-04-02 Thread Wojciech Muła
Ernesto García García 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cascading ifs

2007-04-02 Thread Duncan Booth
Ernesto García García [EMAIL PROTECTED] 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).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cascading ifs

2007-04-02 Thread irstas
On Apr 2, 4:20 pm, Ernesto García García
[EMAIL PROTECTED] 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/replaceNestedConditionalWithGuardClauses.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.

-- 
http://mail.python.org/mailman/listinfo/python-list