Re: first of not None
On 10/9/08, Serge Matveenko [EMAIL PROTECTED] wrote: I need to put in the var property of the first object from the list that is not None. Somth like: foo = first_of([any, beny, riki,]).name Dont want to ugly if-cascade: foo = any.name if name is not None else beny.name if beny is not None \ else riki.name if riki is not None after some play with interpreter and Python logic i've got this: objs = [None, 'dfgh', None,] obj_l = [obj.__len__() for obj in objs if obj is not None][0] Now the question is this is lazy or not? And how could i make it lazy? -- Serge Matveenko mailto:[EMAIL PROTECTED] http://serge.matveenko.ru/ -- http://mail.python.org/mailman/listinfo/python-list
Re: first of not None
Serge Matveenko schrieb: On 10/9/08, Serge Matveenko [EMAIL PROTECTED] wrote: I need to put in the var property of the first object from the list that is not None. Somth like: foo = first_of([any, beny, riki,]).name Dont want to ugly if-cascade: foo = any.name if name is not None else beny.name if beny is not None \ else riki.name if riki is not None after some play with interpreter and Python logic i've got this: objs = [None, 'dfgh', None,] obj_l = [obj.__len__() for obj in objs if obj is not None][0] The usual way to compute the len is to use len(obj) Now the question is this is lazy or not? And how could i make it lazy? No, it's not. You could make it a generator expression: obj_l = (len(obj) for obj in objs if obj is not None).next() Diez -- http://mail.python.org/mailman/listinfo/python-list
first of not None
Hello, everybody! Could someone help me with coding this thing? I need to put in the var property of the first object from the list that is not None. Somth like: foo = first_of([any, beny, riki,]).name Dont want to ugly if-cascade: foo = any.name if name is not None else beny.name if beny is not None \ else riki.name if riki is not None -- Serge Matveenko mailto:[EMAIL PROTECTED] http://serge.matveenko.ru/ -- http://mail.python.org/mailman/listinfo/python-list
Re: first of not None
Serge Matveenko a écrit : Hello, everybody! Could someone help me with coding this thing? I need to put in the var property of the first object from the list that is not None. Somth like: foo = first_of([any, beny, riki,]).name Dont want to ugly if-cascade: foo = any.name if name is not None else beny.name if beny is not None \ else riki.name if riki is not None def not_none(obj): return obj is not None def first_of(seq, predicate=not_none, default=None): for obj in seq: if predicate(obj): return obj else: return default -- http://mail.python.org/mailman/listinfo/python-list
Re: first of not None
I need to put in the var property of the first object from the list that is not None. Somth like: foo = first_of([any, beny, riki,]).name Dont want to ugly if-cascade: foo = any.name if name is not None else beny.name if beny is not None \ else riki.name if riki is not None assuming you meant foo = any.name if ***any*** is not None else beny.name... If you have a fixed/hard-coded list of elements, you could something like: foo = (any or beny or riki).name If you have dynamic list of elements: class NoElementFound(Exception): pass def first(iterable): for element in iterable: if element: return element raise NoElementFound lst = [any, beny] if condition: lst.append(riki) print first(lst).name This first() is about the functionality of the SQL Coalesce() function. Hope this helps, -tim -- http://mail.python.org/mailman/listinfo/python-list