Re: More efficient/elegant branching

2019-12-24 Thread Marco Sulla via Python-list
I agree with Chris Angelico, branch1 is "the way to go". Maybe you have to add a default at start, maybe None, and maybe raise an exception if res is None. Anyway, despite I'm a pain in the... arse and I usually activate ALL the possible warnings in the world, I always disable cyclomatic complexity

Re: More efficient/elegant branching

2019-12-22 Thread DL Neil via Python-list
On 11/12/19 1:07 AM, Daniel Haude wrote: Hello Neil, thanks for the detailed answer. Question: are there other people/factors who/which should be regarded as more important than the linter's opinion? Yes. Mine. Um, see below... (unless humor) I was just puzzled at the linter's output (took

Re: More efficient/elegant branching

2019-12-17 Thread musbur
On Fri, 13 Dec 2019 11:34:24 +0100 Antoon Pardon wrote: > Well if you really want to go this route, you may consider the > following: > > def branch4(a, b, z): > decision = [ > ((lambda: a > 4 and b == 0), "first"), > ((lambda: len(z) < 2), "second"), > ((lambda: b +

Re: More efficient/elegant branching

2019-12-13 Thread Antoon Pardon
On 9/12/19 12:27, Musbur wrote: > Hello, > > I have a function with a long if/elif chain that sets a couple of > variables according to a bunch of test expressions, similar to > function branch1() below. I never liked that approach much because it > is clumsy and repetetive, and pylint thinks so as

Re: More efficient/elegant branching

2019-12-10 Thread Musbur
Hello Neil, thanks for the detailed answer. Question: are there other people/factors who/which should be regarded as more important than the linter's opinion? Yes. Mine. I was just puzzled at the linter's output (took me a while to figure out what it actually meant), and that got me started

Re: More efficient/elegant branching

2019-12-10 Thread Chris Angelico
On Wed, Dec 11, 2019 at 12:26 AM Daniel Haude wrote: > The whole thing is rather academic. Also my efficiency argument doesn't > hold water because this routine is executed just a few times per hour. I > like the "condition table" approach for its lower line count but I'll > stick with if/elif bec

Re: More efficient/elegant branching

2019-12-10 Thread Daniel Haude
Hello Neil, thanks for the detailed answer. Question: are there other people/factors who/which should be regarded as more important than the linter's opinion? Yes. Mine. I was just puzzled at the linter's output (took me a while to figure out what it actually meant), and that got me started

Re: More efficient/elegant branching

2019-12-10 Thread Python
Musbur wrote: I like it! I think it's a cute exercise but it doesn't really solve any problem. The if/elif chain can accomplish the same thing (and much more) in the same line count for the price of being clunkier. **D Yep! This is why I wrote half a dozen of switch implementation but never ac

Re: More efficient/elegant branching

2019-12-10 Thread Musbur
I like it! I think it's a cute exercise but it doesn't really solve any problem. The if/elif chain can accomplish the same thing (and much more) in the same line count for the price of being clunkier. **D -- https://mail.python.org/mailman/listinfo/python-list

Re: More efficient/elegant branching

2019-12-10 Thread Python
Musbur wrote: Hello, I have a function with a long if/elif chain that sets a couple of variables according to a bunch of test expressions, similar to function branch1() below. I never liked that approach much because it is clumsy and repetetive, and pylint thinks so as well. I've come up with

Re: More efficient/elegant branching

2019-12-09 Thread DL Neil via Python-list
I agree with you, so I'm going to ignore def branch2(a, b, z): and def branch3(a, b, z) because they appear too contrived - and I guess that's fair, in that you've contrived to satisfy pylint. Question: are there other people/factors who/which should be regarded as more important than the lint

Re: More efficient/elegant branching

2019-12-09 Thread Tim Chase
On 2019-12-09 12:27, Musbur wrote: > def branch1(a, b, z): > """Inelegant, unwieldy, and pylint complains > about too many branches""" > if a > 4 and b == 0: > result = "first" > elif len(z) < 2: > result = "second" > elif b + a == 10: > result =

Re: More efficient/elegant branching

2019-12-09 Thread Peter Otten
Musbur wrote: > Hello, > > I have a function with a long if/elif chain that sets a couple of > variables according to a bunch of test expressions, similar to function > branch1() below. I never liked that approach much because it is clumsy > and repetetive, and pylint thinks so as well. I've come

Re: More efficient/elegant branching

2019-12-09 Thread Chris Angelico
On Mon, Dec 9, 2019 at 10:36 PM Musbur wrote: > > Hello, > > I have a function with a long if/elif chain that sets a couple of > variables according to a bunch of test expressions, similar to function > branch1() below. I never liked that approach much because it is clumsy > and repetetive, and py

More efficient/elegant branching

2019-12-09 Thread Musbur
Hello, I have a function with a long if/elif chain that sets a couple of variables according to a bunch of test expressions, similar to function branch1() below. I never liked that approach much because it is clumsy and repetetive, and pylint thinks so as well. I've come up with two alternati