Re: first non-null element in a list, otherwise None

2010-09-02 Thread wheres pythonmonks
Peter wrote: >> But this can be expensive memory wise.  Is there a way to concatenate >> generator expressions? > > itertools.chain() > Aha! import itertools >>> x = itertools.chain( (x for x in [None,None] if x is not None), [ None ] >>> ).next() >>> print x None >>> x = itertools.chain( (x fo

first non-null element in a list, otherwise None

2010-09-02 Thread wheres pythonmonks
This should be trivial: I am looking to extract the first non-None element in a list, and "None" otherwise. Here's one implementation: >>> x = reduce(lambda x,y: x or y, [None,None,1,None,2,None], None) >>> print x 1 I thought maybe a generator expression would be better, to prevent iterating

Re: inline exception handling in python

2010-08-12 Thread wheres pythonmonks
On Thu, Aug 12, 2010 at 2:57 PM, Thomas Jollans wrote: > On Thursday 12 August 2010, it occurred to wheres pythonmonks to exclaim: >> [I just hate function call overhead for this.] > > I think you've got your priorities wrong. If you want to avoid unnecessary > overhead

Re: inline exception handling in python

2010-08-12 Thread wheres pythonmonks
On Thu, Aug 12, 2010 at 2:42 PM, MRAB wrote: > wheres pythonmonks wrote: >> >> Hi! >> >> I have on a few occasions now wanted to have inline-exception >> handling, like the inline if/else operator. >> >> For example, >> >> The following m

Re: inline exception handling in python

2010-08-12 Thread wheres pythonmonks
On Thu, Aug 12, 2010 at 2:19 PM, wheres pythonmonks wrote: > On Thu, Aug 12, 2010 at 2:08 PM, Thomas Jollans wrote: >> On Thursday 12 August 2010, it occurred to wheres pythonmonks to exclaim: >>> try: >>>    f = n / d >>> except: >>>    f = float(&q

Re: inline exception handling in python

2010-08-12 Thread wheres pythonmonks
On Thu, Aug 12, 2010 at 2:08 PM, Thomas Jollans wrote: > On Thursday 12 August 2010, it occurred to wheres pythonmonks to exclaim: >> try: >>    f = n / d >> except: >>    f = float("nan") > > A catch-all except clause. Never a good idea. It's

inline exception handling in python

2010-08-12 Thread wheres pythonmonks
Hi! I have on a few occasions now wanted to have inline-exception handling, like the inline if/else operator. For example, The following might raise ZeroDivisionError: f = n / d So, I can look before I leap (which is okay): f = float("nan") if d == 0 else n/d; But, what I'd like to be able t

Re: easy question on parsing python: "is not None"

2010-08-05 Thread wheres pythonmonks
, Aug 5, 2010 at 11:56 AM, Roald de Vries wrote: > On Aug 5, 2010, at 5:42 PM, wheres pythonmonks wrote: >> >> How does "x is not None" make any sense?  "not x is None" does make sense. >> >> I can only surmise that in this context (preceding is) "not

easy question on parsing python: "is not None"

2010-08-05 Thread wheres pythonmonks
How does "x is not None" make any sense? "not x is None" does make sense. I can only surmise that in this context (preceding is) "not" is not a unary right-associative operator, therefore: x is not None === IS_NOTEQ(X, None) Beside "not in" which seems to work similarly, is there other syntacti

None is negative?

2010-08-03 Thread wheres pythonmonks
I did the google search... I must be blind as I don't see any hits... None is negative in Python? (v2.6) http://www.google.com/search?ie=UTF-8&q=%22none+is+negative%22+python >>> if None < -999.99: print "hi" hi >>> >>> if -999 > None: print "hi" hi >>> Is there a way to have the co

Re: subclassing versus object redefinition

2010-08-03 Thread wheres pythonmonks
stead of having code littered with objects strewn across the namespace. Maybe there is a Python ABC tutorial out there that can enlighten me? W On Tue, Aug 3, 2010 at 10:06 AM, Roald de Vries wrote: > On Aug 3, 2010, at 2:46 PM, wheres pythonmonks wrote: >> >> Hi! >> >

subclassing versus object redefinition

2010-08-03 Thread wheres pythonmonks
Hi! I have a class (supposed to be an abstract base class): In python (as opposed to static languages like C++) I don't seed to subclass the base class, but instead I can simply override the behavior of stub methods and values. Is there a preference between between subclassing (C++ approach) and o

Re: default behavior

2010-07-31 Thread wheres pythonmonks
ional-programming approaches. On Sat, Jul 31, 2010 at 5:55 AM, Steven D'Aprano wrote: > On Sat, 31 Jul 2010 01:02:47 -0400, wheres pythonmonks wrote: > > >>> Hint -- what does [].append(1) return? >>> >>> >> Again, apologies from a Python beginn

Re: default behavior

2010-07-30 Thread wheres pythonmonks
ith the child?) Thanks again from a Perl-to-Python convert! W On Fri, Jul 30, 2010 at 11:47 PM, Steven D'Aprano wrote: > On Fri, 30 Jul 2010 08:34:52 -0400, wheres pythonmonks wrote: > >> Sorry, doesn't the following make a copy? >> >>>>>> from collec

pylint scores

2010-07-30 Thread wheres pythonmonks
I am starting to use pylint to look at my code and I see that it gives a rating. What values do experienced python programmers get on code not targeting the benchmark? I wrote some code, tried to keep it under 80 characters per line, reasonable variable names, and I got: 0.12 / 10. Is this a goo

Re: default behavior

2010-07-30 Thread wheres pythonmonks
on the difference between that and: m['key'] = m.get('key',[]).append(1) Except that the latter works for immutable values as well as containers. On Fri, Jul 30, 2010 at 8:19 AM, Steven D'Aprano wrote: > On Fri, 30 Jul 2010 07:59:52 -0400, wheres pythonmonks wrote

Re: default behavior

2010-07-30 Thread wheres pythonmonks
Instead of defaultdict for hash of lists, I have seen something like: m={}; m.setdefault('key', []).append(1) Would this be preferred in some circumstances? Also, is there a way to upcast a defaultdict into a dict? I have also heard some people use exceptions on dictionaries to catch key existe

Re: default behavior

2010-07-29 Thread wheres pythonmonks
Thanks. I presume this will work for my nested example as well. Thanks again. On Thu, Jul 29, 2010 at 2:18 PM, Paul Rubin wrote: > wheres pythonmonks writes: >> How do I build an "int1" type that has a default value of 1? >> [Hopefully no speed penalty.] >> I

default behavior

2010-07-29 Thread wheres pythonmonks
Why is the default value of an int zero? >>> x = int >>> print x >>> x() 0 >>> How do I build an "int1" type that has a default value of 1? [Hopefully no speed penalty.] I am thinking about applications with collections.defaultdict. What if I want to make a defaultdict of defaultdicts of lists?

Re: Nice way to cast a homogeneous tuple

2010-07-28 Thread wheres pythonmonks
Thanks ... I thought int was a type-cast (like in C++) so I assumed I couldn't reference it. On Wed, Jul 28, 2010 at 9:31 AM, Nick Raptis wrote: > Ep, that missing line should be: > > On 07/28/2010 04:27 PM, Nick Raptis wrote: >> >> On 07/28/2010 04:15 PM, wheres py

Nice way to cast a homogeneous tuple

2010-07-28 Thread wheres pythonmonks
A new python convert is now looking for a replacement for another perl idiom. In particular, since Perl is weakly typed, I used to be able to use unpack to unpack sequences from a string, that I could then use immediately as integers. In python, I find that when I use struct.unpack I tend to get

Re: Easy questions from a python beginner

2010-07-23 Thread wheres pythonmonks
#x27;,'from __main__ import >>> A3,d',number=10); 9.4334241349350947 So: in-line lambda possible speed improvement. in-line tuple is slow, passed-in callback, slowest yet? Is this possibly right? Hopefully someone can spot the bug? W On Fri, Jul 23, 2010 at 4:10 AM, Steven D

Re: Easy questions from a python beginner

2010-07-22 Thread wheres pythonmonks
ess function call dispatches] However, it might be more efficient to avoid the function call overhead completely and pass-in a string which is substituted into a string code block, compiled, and executed. W On Thu, Jul 22, 2010 at 8:12 PM, Carl Banks wrote: > On Jul 22, 3:34 pm, wheres

Re: Easy questions from a python beginner

2010-07-22 Thread wheres pythonmonks
overkill. 3. Is there a reference on all the special variables, like __foo__? 4. Is there any work on deparsing (like Perl's deparse) lambda functions to inline algebra and get a performance gain? Thanks again for your input, W ( from Perl-hacker to Python Programmer ) On Sun, Jul 11, 20

Re: Easy questions from a python beginner

2010-07-11 Thread wheres pythonmonks
ons/938429/scope-of-python-lambda-functions-and-their-parameters Will try to avoid namespace mangling until next week. Thanks again, W On Sun, Jul 11, 2010 at 2:17 PM, Duncan Booth wrote: > wheres pythonmonks wrote: > >> I'm an old Perl-hacker, and am trying to Dive in Py

Easy questions from a python beginner

2010-07-11 Thread wheres pythonmonks
I'm an old Perl-hacker, and am trying to Dive in Python. I have some easy issues (Python 2.6) which probably can be answered in two seconds: 1.  Why is it that I cannot use print in booleans??  e.g.: >>> True and print "It is true!" I found a nice work-around using eval(compile(.,"","exec"))