Proposal: function which simulates C ?: operator

Adal Chiriliuc me at spammers.com
Sun Jul 11 14:33:54 CEST 2004


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.




More information about the Python-list mailing list