Re: the PHP ternary operator equivalent on Python

2005-11-24 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote >> dictionary accesses don't involve passes over the dictionary. > > I was actually thinking of the effects of hardware > memory cache... yeah, sure. -- http://mail.python.org/mailman/listinfo/python-list

Re: the PHP ternary operator equivalent on Python

2005-11-24 Thread rurpy
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > > > I don't find your code any more readable than the OP's > > equivalent code: > > the OP's question was > > How you do this in a practic way without > the use of one-line code ? I know. But you compared the readability of code with one-

Re: the PHP ternary operator equivalent on Python

2005-11-24 Thread rurpy
Steven D'Aprano wrote: > [EMAIL PROTECTED] wrote: > > > Fredrik Lundh wrote: > > > >>def convert(old): > >> > >>new = dict( > >>CODE=old['CODEDATA'], > >>DATE=old['DATE'] > >>) > >> > >>if old['CONTACTTYPE'] == 2: > >>new['CONTACT'] = old['FIRSTCONTACT'] > >

Re: the PHP ternary operator equivalent on Python

2005-11-24 Thread Steven D'Aprano
[EMAIL PROTECTED] wrote: > Fredrik Lundh wrote: > >>def convert(old): >> >>new = dict( >>CODE=old['CODEDATA'], >>DATE=old['DATE'] >>) >> >>if old['CONTACTTYPE'] == 2: >>new['CONTACT'] = old['FIRSTCONTACT'] >>else: >>new['CONTACT'] = old['SECONDC

Re: the PHP ternary operator equivalent on Python

2005-11-24 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I don't find your code any more readable than the OP's > equivalent code: the OP's question was How you do this in a practic way without the use of one-line code ? > The OPs code make one pass through the dict, your's makes > two. I do not know what effect (i

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Fredrik Lundh
Paul Rubin wrote: > It gets messy for a more complicated structure: > >d = {'x': x1() if x_is_old else x2(), > 'y': y1() if y_is_old else y2(), > 'z': z1() if z_is_old else z2() } > etc. somewhere between the '}' and the '.', a data-driven approach will be more readable, easie

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > dNewDataFields['CODE'] = dOldDataFields['CODEDATA'] > dNewDataFields['DATE'] = dOldDataFields['DATE'] > if dOldDataFields['CONTACTTYPE'] == 2: > dNewDataFields['CONTACT'] = dOldDataFields['FIRSTCONTACT'] > else: > dNewDataFields['CONTACT'] = dOl

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Steven D'Aprano
On Wed, 23 Nov 2005 07:01:58 -0800, Daniel Crespo wrote: >> WHY WHY WHY the obsession with one-liners? What is wrong with the good old >> fashioned way? > > >> if cond: >>x = true_value >> else: >>x = false_value > > Let me tell you something: I'm not a one-liner coder, but sometimes It

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread rurpy
Fredrik Lundh wrote: > Daniel Crespo wrote: > > > Let me tell you something: I'm not a one-liner coder, but sometimes It > > is necesary. > > For example: > > I need to translate data from a DataField to Another. > > > > def Evaluate(condition,truepart,falsepart): > >if condition: > >r

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Fredrik Lundh
Daniel Crespo wrote: > Let me tell you something: I'm not a one-liner coder, but sometimes It > is necesary. > For example: > I need to translate data from a DataField to Another. > > def Evaluate(condition,truepart,falsepart): >if condition: >return truepart >else: >return

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread [EMAIL PROTECTED]
Luis M. Gonzalez wrote: > This could be done easier this way: > > L = [('even','odd')[n%2] for n in range(8)] That is even/odd, his(created to demonstrate the uglies of ternary) has 4 states, zero, -/+ then even/odd. -- http://mail.python.org/mailman/listinfo/python-list

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Luis M. Gonzalez
This could be done easier this way: L = [('even','odd')[n%2] for n in range(8)] -- http://mail.python.org/mailman/listinfo/python-list

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Luis M. Gonzalez
This could be done easier this way: L = [('odd','even')[n%2] for i in range(8)] -- http://mail.python.org/mailman/listinfo/python-list

Re: the PHP ternary operator equivalent on Python

2005-11-23 Thread Daniel Crespo
> WHY WHY WHY the obsession with one-liners? What is wrong with the good old > fashioned way? > if cond: >x = true_value > else: >x = false_value Let me tell you something: I'm not a one-liner coder, but sometimes It is necesary. For example: I need to translate data from a DataField to

Re: the PHP ternary operator equivalent on Python

2005-11-19 Thread [EMAIL PROTECTED]
Steven D'Aprano wrote: > L = ["zero" if n == 0 else \ > "negative " + ("odd" if n % 2 else "even") if n < 0 else \ > "odd" if n % 2 else "even" for n in range(8)] > BTW, the continuation is not necessary I believe. [ x==0 and "zero" or ["","-"][x < 0] + ("even", "odd")[x%2] for x in range

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread [EMAIL PROTECTED]
Steven D'Aprano wrote: > Why do you assume that everything you need for your list comprehension has > to go into a single line? Chances are your list comp already calls > functions, so just create one more for it to use. > > > py> def describe(cond): > ... if cond: > ... return "odd" >

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Steven D'Aprano
On Fri, 18 Nov 2005 21:05:50 -0800, [EMAIL PROTECTED] wrote: > Steven D'Aprano wrote: >> WHY WHY WHY the obsession with one-liners? What is wrong with the good old >> fashioned way? >> >> if cond: >> x = true_value >> else: >> x = false_value >> >> It is easy to read, easy to understand,

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread [EMAIL PROTECTED]
Roy Smith wrote: > I think the list comprehensions are going to be the death of readable > python programs. Could be, but seems that someone in charge of the language wants readable python programs to die then as if list comprehension is not enough, there comes generator expression and now the for

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Roy Smith
In article <[EMAIL PROTECTED]>, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Steven D'Aprano wrote: > > WHY WHY WHY the obsession with one-liners? What is wrong with the good old > > fashioned way? > > > > if cond: > > x = true_value > > else: > > x = false_value > > > > It is easy to

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread [EMAIL PROTECTED]
Steven D'Aprano wrote: > WHY WHY WHY the obsession with one-liners? What is wrong with the good old > fashioned way? > > if cond: > x = true_value > else: > x = false_value > > It is easy to read, easy to understand, only one of true_value and > false_value is evaluated. It isn't a one-lin

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Steven D'Aprano
On Fri, 18 Nov 2005 11:17:17 -0800, David Wahler wrote: > Daniel Crespo wrote: >> I would like to know how can I do the PHP ternary operator/statement >> (... ? ... : ...) in Python... >> >> I want to something like: >> >> a = {'Huge': (quantity>90) ? True : False} > > Well, in your example the

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread [EMAIL PROTECTED]
The cleanest(IMO) is this : a = (predicate and [if_true_expr] or [if_false_expr])[0] This would give you the necessary "short circuit" behaviour no matter what. a = predicate and if_true_expr or if_false_expr works most of the time but should if_true_expr turns out to be 0 or something like tha

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Steve M
Another way to simulate the ternary operator is this: a = (quantity > 90 and "It is very huge") or "The value is correct" You have to be careful of semantics of 'and' and 'or'. But in this case I wonder why you don't just test whether quantity is greater than 90 and assign the corresponding value

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Daniel Crespo
Hi Peter, Expand your mind. a = {'whatever': TernaryOperation(quantity>90,"It is very huge","The value is correct")} ;-) thanks for your advice anyway Daniel -- http://mail.python.org/mailman/listinfo/python-list

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Grant Edwards
On 2005-11-18, Daniel Crespo <[EMAIL PROTECTED]> wrote: > I would like to know how can I do the PHP ternary operator/statement > (... ? ... : ...) in Python... The _PHP_ ternary operator (x?y:z)! Kids these days! -- Grant Edwards grante Yow! It's so OBVIOUS!!

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Peter Otten
Daniel Crespo wrote: > Oh... Well, thanks for that information. > > I'll do this then: > > def TernaryOperation(condition,true_part,false_part): > if condition: > return True-part > else: > return False-part > > a = {'Huge': TernaryOperation(quantity>90,True,False)} By

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Daniel Crespo
Oh... Well, thanks for that information. I'll do this then: def TernaryOperation(condition,true_part,false_part): if condition: return True-part else: return False-part a = {'Huge': TernaryOperation(quantity>90,True,False)} Thank you -- http://mail.python.org/mailman/l

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread gene tani
Mike Meyer wrote: > "Daniel Crespo" <[EMAIL PROTECTED]> writes: > > > Hi! > > > > I would like to know how can I do the PHP ternary operator/statement > > (... ? ... : ...) in Python... > > > > I want to something like: > > > > a = {'Huge': (quantity>90) ? True : False} > > > > Any suggestions? >

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Mike Meyer
"Daniel Crespo" <[EMAIL PROTECTED]> writes: > Hi! > > I would like to know how can I do the PHP ternary operator/statement > (... ? ... : ...) in Python... > > I want to something like: > > a = {'Huge': (quantity>90) ? True : False} > > Any suggestions? Lots of ways, depending on your exact needs

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread David Wahler
Daniel Crespo wrote: > I would like to know how can I do the PHP ternary operator/statement > (... ? ... : ...) in Python... > > I want to something like: > > a = {'Huge': (quantity>90) ? True : False} Well, in your example the '>' operator already returns a boolean value so you can just use it di

Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread Simon Brunning
On 18 Nov 2005 10:53:04 -0800, Daniel Crespo <[EMAIL PROTECTED]> wrote: > I would like to know how can I do the PHP ternary operator/statement > (... ? ... : ...) in Python... Wait for Python 2.5 - . -- Cheers, Simon B, [EMAIL PROTECTED], http://www.brunn

the PHP ternary operator equivalent on Python

2005-11-18 Thread Daniel Crespo
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any suggestions? Thanks Daniel -- http://mail.python.org/mailman/listinfo/python-list