Python Forum

Full Version: shortening an elif construct
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i have a big long elif construct like
    if condition-0:
        ...
    elif condition-1:
        ...
    elif condition-2:
        ...
    elif condition-3:
        ...
    elif condition-4:
        ...
    elif condition-5:
        ...
    elif condition-6:
        ...
    elif condition-7:
        ...
    elif condition-8:
        ...
    else:
        ...
    ...# a lot of ending code
but there are a few times i would like to shorten it and only do part of it like
    if condition-0:
        ...
    elif condition-1:
        ...
    elif condition-2:
        ...
    elif condition-3:
        ...
    elif condition-4:
        ...
    elif condition-5:
        ...
    else:
        ...
    ...# a lot of ending code
i am wanting to avoid replicating code so that the execution clauses, which are rather complex code, do not have to be duplicated by having both elif constructs. but i cannot, as far as i know, just wrap part of this in another if construct, such as the following invalid code
    if condition-0:
        ...
    elif condition-1:
        ...
    elif condition-2:
        ...
    elif condition-3:
        ...
    elif condition-4:
        ...
    elif condition-5:
        ...
    if do-3-more-tests:
        elif condition-6:
            ...
        elif condition-7:
            ...
        elif condition-8:
            ...
    else:
        ...
    ...# a lot of ending code
the code in the else block is the same in both situations, whether do-3-more-tests is true or false. anyone have an idea how to do this (and keep the code from enlarging very much and avoid any duplications)?

this is the kind of thing i seriously want to avoid
    if do-3-more-tests:
        if condition-0:
            ...
        elif condition-1:
            ...
        elif condition-2:
            ...
        elif condition-3:
            ...
        elif condition-4:
            ...
        elif condition-5:
            ...
        elif condition-6:
            ...
        elif condition-7:
            ...
        elif condition-8:
            ...
        else:
            ...
    else:
        if condition-0:
            ...# same as on line 3 above
        elif condition-1:
            ...# same as on line 5 above
        elif condition-2:
            ...# same as on line 7 above
        elif condition-3:
            ...# same as on line 9 above
        elif condition-4:
            ...# same as on line 11 above
        elif condition-5:
            ...# same as on line 13 above
        else:
            ...# same as on line 21 above
    ...# a lot of ending code
see the problem? i can see a way to do this by using a stack of if constructs that end with continue, by having it all in a loop. but there is code to be run at the end that would have to be duplicated 7 times. anyone have an idea how to do this (and keep the code from enlarging very much and avoid any duplications)?
I have fuzzy understanding of the subject, nevertheless I post link to this article: Use Python to do Switch Case

Maybe it's total miss or maybe it's spot-on.
What about a dictionary replacement for the if/elif/else structure?

structure = {condition_1: function_a, condition_2: function_b, ...}
...
structure[current_state](common_data)
Not know what your conditions are, it's not clear if you can convert them to dictionary keys. But if you can, it could solve your problems. One, it allows code reuse by assigning the same function to different conditions. Two, you can do your last code example while only repeating one line of code.
no, i can't convert them to dictionary keys. here is a small sample (my current case is a bit more complex) ...
    if x < 16:
        ...
    elif x < 256:
        ...
    elif x < 65536:
        ...
    elif x < 4294967296:
        ...
    elif x < 18446744073709551616:
        ...
    else:
        raise ValueError()

(Jul-23-2018, 12:25 PM)perfringo Wrote: [ -> ]I have fuzzy understanding of the subject, nevertheless I post link to this article: Use Python to do Switch Case

Maybe it's total miss or maybe it's spot-on.
what i started to do is much like what it talks about. but, i am expanding on it. imagine having a switch case (in a language that does that) with 8 possible cases. then you want to have a 2nd switch case that has the same exact set of cases except 2 of the are not there. you could nest each switch case in an if else. but then you have all those duplicate cases. now, figure out a way to code it with no duplicates. then convert that switch case to how Python does it (is described in your link).

BTW, Python's way is more flexible. see what i coded in the sample code in the first post #4 above. try doing that with an ordinary switch case.

how i did this ...

it really is a sequence of numeric range tests like shown above, but with different numbers. what i did was substitute with variables and under the alternate condition, made those variables be 0 (if x < 0). i coded all the tests with variables to make the code look more consistent. i will be releasing this code later on.
(Jul-24-2018, 02:44 AM)Skaperen Wrote: [ -> ]no, i can't convert them to dictionary keys. here is a small sample (my current case is a bit more complex)
in this example the question is what is in the body of each condition

my_swithch = ((4, func1), (8, func2), (16, func3), (32, func4), (64, func5))
for power, func in my_switch:
    if x < 2 ** power:
        func()
        break
else:
    raise ValueError()
in the example I use tuple of tuples, but you can use ordered dict if prefer
You could convert that to a data structure.
(Jul-24-2018, 04:36 AM)ichabod801 Wrote: [ -> ]You could convert that to a data structure.
is this at me or @Skaperen? I'm not sure what you mean
Sorry, that was @Skaperen.
yes, a data structure could very well do the job. but i do want my code to be at least half-way semi-readable.
(Jul-24-2018, 04:50 AM)Skaperen Wrote: [ -> ]but i do want my code to be at least half-way semi-readable.
monstrous if-else and magic numbers like 18446744073709551616 are way from readable... anyway, do what you want...
Pages: 1 2