Hello Everyone

While writing python code, I frequently come across the need to do
certain tasks based on combined conditions.

Much of the task for all the sub-conditions are common but some are
specific to one or more of these sub-conditions.

A simplified example:

########################## Code ##########################
if (color == BLUE and count == 20) or (color == RED and count % 5 == 0):
    rotate_the_wheel() # Common to the two sub-conditions
    if(color == BLUE and count == 20): # First sub-condition
        set_signal()
    if(color == RED and count % 5 == 0): # Second sub-condition
        clear_signal()
    proc_post_rotate() # Common to the two sub-conditions

I am not sure if there is a better way to do this. If not, maybe there
can be an extension to the language, which would allow marking a
sub-condition just like we mark a sub-expression in a regular
expression.

Tentative syntax for this could be ({} marks the sub-condition and
\number refers back to it):

########################## Code ##########################
if {(color == BLUE and count == 20)} or {(color == RED and count % 5 == 0)}:
    rotate_the_wheel()
    if(\1): # First marked condition
        set_signal()
    if(\2): # Second marked condition
        unset_signal()
    proc_post_rotate()

And like sub-expressions, the nesting of marked sub-condions should
also be possible:

########################## Code ##########################
if {{(color == BLUE and count == 20)} and {value == 20}} or {(color ==
RED and count % 5 == 0)}:
    if(\1):# Refers to the entire subcondition {{(color == BLUE and
count == 20)} and {value = 20}}
        proc1()
    if(\2):# Refers to sub-subcondition {value == 20}

This will not only avoid the repetition of sub-conditions, but make
code readable since something like \1 will give an immediate
indication of a sub-condition that's defined earlier.

Please let me know something similar is already implemented.
Even otherwise, all your thoughts, inputs and criticism are welcome.

Thanks and best regards
Shrinivas Kulkarni
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to