I think a function similar to the one below should be added to the
builtin module:
def boolselect(condition, trueresult, falseresult):
if condition:
return trueresult
else:
return falseresult
--- WITH
scalewidth = boolselect(self.__vertical, _scalew, _scaleh)
a1 = 0
a4 = boolselect(self.__vertical, self.__width, self.__height)
a2 = (a4 - scalewidth) // 2
a3 = a2 + scalewidth
self.__meterwidth = self.__meterrect[0].Width()
self.__meterheight = self.__meterrect[0].Height()
self.__meterlength = boolselect(self.__vertical, self.__meterheight, self.__meterwidth)
t = boolselect(self.__range >= 18, -18, -12)
redzone = self.__dBToOffset(-6)
yellowzone = self.__dBToOffset(t)
--- WITHOUT
if self.__vertical:
scalewidth = _scalew
else:
scalewidth = _scaleh
a1 = 0
if self.__vertical:
a4 = self.__width
else:
a4 = self.__height
a2 = (a4 - scalewidth) // 2
a3 = a2 + scalewidth
self.__meterwidth = self.__meterrect[0].Width()
self.__meterheight = self.__meterrect[0].Height()
if self.__vertical:
self.__meterlength = self.__meterheight
else:
self.__meterlength = self.__meterwidth
if self.__range >= 18:
t = -18
else:
t = -12
redzone = self.__dBToOffset(-6)
yellowzone = self.__dBToOffset(t)
---
What do you think?
PS: maybe a better name than boolselect could be found. 15 1446
Adal Chiriliuc <me@spammers.com>
(news:13***********************@smtp.myrealbox.com ) wrote: I think a function similar to the one below should be added to the builtin module:
def boolselect(condition, trueresult, falseresult): if condition: return trueresult else: return falseresult
--- WITH
scalewidth = boolselect(self.__vertical, _scalew, _scaleh)
--- WITHOUT
if self.__vertical: scalewidth = _scalew else: scalewidth = _scaleh
What do you think?
Here's a nifty workaround I figured out myself but am sure others use it as
well:
scalewidth=(_scaleh,_scalew)[__self.vertical]
HTH
It's not as readable as the "if cond ? true_expr : false_expr", but I think
introducing syntax like this is far from needed and pretty complicated.
In article <13***********************@smtp.myrealbox.com>,
Adal Chiriliuc <me@spammers.com> wrote: I think a function similar to the one below should be added to the builtin module:
def boolselect(condition, trueresult, falseresult): if condition: return trueresult else: return falseresult
[ ... ] What do you think?
PS: maybe a better name than boolselect could be found.
Some poeple might be disturbed by what it would do with
this_line = boolselect (dummy_flag, "Dummy line\n", infile.readline())
Side-effects are the main reason that a simple function
can't generally replace a full conditional ternary operator.
See google for the ternary operator debate, and see the other
discussion in clp right now.
Regards. Mel.
Adal Chiriliuc <me@spammers.com> writes: I think a function similar to the one below should be added to the builtin module:
def boolselect(condition, trueresult, falseresult): if condition: return trueresult else: return falseresult
That doesn't work because both results get evaluated either way. E.g.
boolselect(x==0, f(x), g(x))
calls both f and g. You need something like
(lambda: g(x), lambda: f(x))[bool(condition)]()
Adal Chiriliuc wrote: I think a function similar to the one below should be added to the builtin module:
def boolselect(condition, trueresult, falseresult): if condition: return trueresult else: return falseresult
This doesn't simulate the conditional operator; it doesn't do
short-circuiting. See PEP 308; this kind of construct was explicitly
rejected (but then, so is the whole PEP).
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Forever we / Infinitely
-- Sandra St. Victor
Paul Rubin <http://ph****@NOSPAM.invalid> writes: (lambda: g(x), lambda: f(x))[bool(condition)]()
Since you call the object afterwards, you don't need the lambdas
(which add a layer of indirection), just the function references:
(g, f)[bool(condition)]()
Tor Iver Wilhelmsen wrote: Paul Rubin <http://ph****@NOSPAM.invalid> writes:
(lambda: g(x), lambda: f(x))[bool(condition)]()
Since you call the object afterwards, you don't need the lambdas (which add a layer of indirection), just the function references:
(g, f)[bool(condition)]()
Well, you meant:
(g, f)[bool(condition)](x)
but that was taking his example too literally; he was just using f(x)
and g(x) to expressions that you wanted laziy evaluated.
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Forever we / Infinitely
-- Sandra St. Victor
Tor Iver Wilhelmsen <to*****************@broadpark.no> writes: (lambda: g(x), lambda: f(x))[bool(condition)]()
Since you call the object afterwards, you don't need the lambdas (which add a layer of indirection), just the function references:
(g, f)[bool(condition)]()
Good catch, for that particular example. The lambdas are needed for
more general cases, e.g. (x > 0 ? (f(x)+g(y)) : (h(x)-z(y)))
There's been various proposals floated for macros in Python. If
something like that becomes real, that can solve this whole problem.
I didn't know this was discussed so much in the past. I should have
searched before.
Most of you objected that this is a bad solution because it evaluates
both variants. That's true if you want a real ternary operator. I now
think I gave a bad title to this thread. It shouldn't have mentioned
?:
I've searched through the C++ sources I'm now porting to Python
(450 KB) and found 44 uses of ?: and from these only 4 needed short
circuit evaluation (to avoid dereferencing NULL pointers or zero
division).
I now suggest that this function be added without implying that it's
the Python equivalent of ?: and with the docs clearly explaining that
it's not ?: and how it differs. It will be like Python private vars,
almost but not quite (of course, Python private vars are a lot more
close to the ideal than this is).
Anyway, I suggested this because the function I have is actually named
Util.BoolSelect, and this is kind of long :)
Adal Chiriliuc wrote: I didn't know this was discussed so much in the past. I should have searched before.
And if you read all of it, you probably wouldn't be making
the following suggestion, either. :-)
I now suggest that this function be added without implying that it's the Python equivalent of ?: and with the docs clearly explaining that it's not ?: and how it differs.
It won't happen because (a) it's a trivial function to write
on-demand, and (b) there are already several different ways of
spelling it with current Python syntax if you don't want a
function, and (c) I suspect that the requirement for short-circuit
evaluation would actually be *more* common in Python than it
appears it is in C++ based on your very limited sample population.
-Peter
"Mitja" <nu*@example.com> wrote in message news:<LA******************@news.siol.net>... Adal Chiriliuc <me@spammers.com> (news:13***********************@smtp.myrealbox.com ) wrote: I think a function similar to the one below should be added to the builtin module:
def boolselect(condition, trueresult, falseresult): if condition: return trueresult else: return falseresult
--- WITH
scalewidth = boolselect(self.__vertical, _scalew, _scaleh)
--- WITHOUT
if self.__vertical: scalewidth = _scalew else: scalewidth = _scaleh
What do you think?
Here's a nifty workaround I figured out myself but am sure others use it as well: scalewidth=(_scaleh,_scalew)[__self.vertical]
If you're going to use the "(F, T)[C]" syntax, it would be better to
write it as "(F, T)[bool(C)]" to ensure that the array index is 0 or
1.
However, a better alternative is "(C and [T] or [F])[0]", which
short-circuits and also has the advantage of having the same order as
"if".
If you're absolutely certain that T will never be false, you can
simplify this to "C and T or F".
Dan Bishop wrote: However, a better alternative is "(C and [T] or [F])[0]", which short-circuits and also has the advantage of having the same order as "if".
It's got the same order, but it's completely opaque. It's hardly better
if you're interested in readability.
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Performing in front of a live audience is like a feeling of shock.
-- Sade Adu
Peter Hansen wrote: Adal Chiriliuc wrote:
I didn't know this was discussed so much in the past. I should have searched before. And if you read all of it, you probably wouldn't be making the following suggestion, either. :-)
I now suggest that this function be added without implying that it's the Python equivalent of ?: and with the docs clearly explaining that it's not ?: and how it differs.
It won't happen because (a) it's a trivial function to write on-demand,
How do you write this function without lambda in the call?
and (b) there are already several different ways of spelling it with current Python syntax if you don't want a function
One uglier than the other.
and (c) I suspect that the requirement for short-circuit evaluation would actually be *more* common in Python than it appears it is in C++ based on your very limited sample population.
ACK.
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Reinhold Birkenfeld wrote: Peter Hansen wrote:It won't happen because (a) it's a trivial function to write on-demand,
How do you write this function without lambda in the call?
Basically, you use if/then like you are "supposed" to. :-)
But the OP wasn't asking about a short-circuit version, just
the trivial fully evaluated function approach. and (b) there are already several different ways of spelling it with current Python syntax if you don't want a function
One uglier than the other.
Agreed. Use if/then and save the sanity of your maintenance
programmers. and (c) I suspect that the requirement for short-circuit evaluation would actually be *more* common in Python than it appears it is in C++ based on your very limited sample population.
ACK.
Ironic that Bill the Cat changed the meaning of that from
positive to negative. :-) (Or did he always use mixed-case,
so that all-caps ACK is still uniquely ASCII?)
-Peter
Peter Hansen wrote: Reinhold Birkenfeld wrote:
Peter Hansen wrote:It won't happen because (a) it's a trivial function to write on-demand,
How do you write this function without lambda in the call?
Basically, you use if/then like you are "supposed" to. :-) But the OP wasn't asking about a short-circuit version, just the trivial fully evaluated function approach.
Granted. This or (x and y or z) without y being false... and (b) there are already several different ways of spelling it with current Python syntax if you don't want a function
One uglier than the other.
Agreed. Use if/then and save the sanity of your maintenance programmers.
and (c) I suspect that the requirement for short-circuit evaluation would actually be *more* common in Python than it appears it is in C++ based on your very limited sample population.
ACK.
Ironic that Bill the Cat changed the meaning of that from positive to negative. :-) (Or did he always use mixed-case, so that all-caps ACK is still uniquely ASCII?)
Well, I don't know Bill the Cat, should I?
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Reinhold Birkenfeld wrote: Peter Hansen wrote:Reinhold Birkenfeld wrote:ACK.
Ironic that Bill the Cat changed the meaning of that from positive to negative. :-) (Or did he always use mixed-case, so that all-caps ACK is still uniquely ASCII?)
Well, I don't know Bill the Cat, should I?
Probably not. :-) It was a comic strip called "Bloom County"
which featured among many other things a cat named Bill who
was, uh, just a tad incoherent, and given to mutterings like
"Ack! Pthft!"
See http://encyclopedia.thefreedictionar...ll%20the%20Cat
for background, and the following for a nice pic: http://www.premier.net/~cspedale/opus/images/bill2.jpg
-Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Guido van Rossum |
last post by:
Robert and Python-dev,
I've read the J2 proposal up and down several times, pondered all the
issues, and slept on it for a night, and I still don't like it enough
to accept it. The only reason...
|
by: Brian Sabbey |
last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The
mechanism described here is intended to act as a complement to thunks.
Please let me know what you think.
Suite-Based...
|
by: seberino |
last post by:
I've heard 2 people complain that word 'global' is confusing.
Perhaps 'modulescope' or 'module' would be better?
Am I the first peope to have thought of this and suggested it?
Is this a...
|
by: wkaras |
last post by:
I would like to propose the following changes to the C++ Standard,
the goal of which are to provide an improved ability to specify
the constraints on type parameters to templates. Let me say from...
|
by: Steve R. Hastings |
last post by:
I have been studying Python recently, and I read a comment on one
web page that said something like "the people using Python for heavy math
really wish they could define their own operators". The...
|
by: Kaz Kylheku |
last post by:
I've been reading the recent cross-posted flamewar, and read Guido's
article where he posits that embedding multi-line lambdas in
expressions is an unsolvable puzzle.
So for the last 15 minutes...
|
by: glomde |
last post by:
i I would like to extend python so that you could create hiercical
tree structures (XML, HTML etc) easier and that resulting code would be
more readable than how you write today with packages like...
|
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= |
last post by:
Hello everybody,
this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:
--- begin of proposal ---
Proposal to add...
|
by: terminator |
last post by:
first:
I find the 'r/l-value reference' terminology rather confusing and I
find the following names for this new refrence type more suitable:
temporary reference:
Since it is generally intended...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |