Python Forum
shortening an elif construct
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shortening an elif construct
#1
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)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
You could convert that to a data structure.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Sorry, that was @Skaperen.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
yes, a data structure could very well do the job. but i do want my code to be at least half-way semi-readable.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the if construct not calculating correctly? egemynet 21 3,855 Mar-21-2022, 01:20 PM
Last Post: deanhystad
  Building command in a looping construct DennisT 3 1,874 Sep-08-2020, 06:32 PM
Last Post: DennisT
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,237 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Best construct? Array, class, other? PappaBear 1 2,967 May-10-2017, 06:02 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020