Re: disgrating a list

2006-09-01 Thread Hardcoded Software
Tim Chase wrote:
>  >>> def flatten(x):
> ... q = []
> ... for v in x:
> ... if hasattr(v, '__iter__'):
> ... q.extend(flatten(v))
> ... else:
> ... q.append(v)
> ... return q
> ...

Let's do some nitpicking on "pythonic" style. A more pythonic way to do
it would be:

try:
q.extend(v)
except TypeError:
q.append(v)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: disgrating a list

2006-09-02 Thread Hardcoded Software
Tal Einat wrote:
> Neil Cerutti wrote:
> > On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote:
> > > Tim Chase wrote:
> > >> I'm not sure if '__iter__' is the right thing to be looking
> > >> for, but it seems to work at least for lists, sets,
> > >> dictionarys (via their keys), etc.  I would use it because at
> > >> least then you know you can iterate over it
> > >
> > > AFAIK and as seen throughout posts on c.l.py, the best way to
> > > check if something is iterable is:
> > >
> > > try:
> > > iter(obj)
> > > except TypeError:
> > > 
> > > else:
> > > 
> >
> > That confounds me.  Of course, I'm coming from a C++, where you
> > never want to throw an exception in a common case, hence the name
> > 'exception'. The Python FAQ does say that raising and catching an
> > exception is an expensive operation. I see that type-checking is
> > good to avoid, but type-checking must be better than "abusing"
> > exceptions in this way. Is the above really a popular idiom?
> >
> > If so, I guess I'll get used to it.
> >
> Such use of exceptions is surely common enough in Python to be worth
> getting to know, if only in order to understand others' code.
>
> If you had wanted to check if an object was callable, which is a more
> basic functionality in Python, you would use Python's built-in function
> callable(). Unfortunately there is no such function as iterable().
> (perhaps there should be? ...)
>
> Raising an exception is a relatively "expensive" operation, but not so
> much that you would want to avoid it altogether... In most such cases,
> such as checking whether an object is iterable, speed isn't an issue.
>
> IMO this is far from "abuse". The iter() function's documenation
> explicitly notes "If it does not support either of those protocols,
> TypeError is raised." This can be viewed as one possible output of the
> function, in addition to the possibility of returning an iterator. In
> such a function, raising exceptions instead of returning None or other
> special values (often called "return codes") is much more modular, and
> IMO much more readable.
>
> - Tal

I agree. Code readability is much more important than code performance
(Well, in the Python realm.). A Python coder shouldn't care about
performance until he/she does a profiling and finds out a bottleneck.

Besides, I like my solution a little better :). When performing the
extend in the try, the extend will raise TypeError anyway. No need to
call iter(). It's what my Python in a nutshell book calls "It's easier
to ask forgiveness than permission" idiom, as opposed to the "Look
before you leap" idiom.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for number?

2006-09-04 Thread Hardcoded Software

Dr. Pastor wrote:
> In the following code I would like to ascertain
> that x has/is a number. What the simplest TEST should be?
> (Could not find good example yet.)
> ---
> x=raw_input('\nType a number from 1 to 20')
> if TEST :
>   Do_A
> else:
>   Do_B
> ---
> Thanks for any guidance.
>
> == Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet 
> News==
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ 
> Newsgroups
> = East and West-Coast Server Farms - Total Privacy via Encryption =

To test if it *is* a number, the solution is pretty simple (s.isdigit()
or an int(s) in a try..except ValueError), but if you want to test if
it is or *has* a number, I think that the simplest solution would be a
regexp:

import re
re_has_digit = re.compile(r'\d+')
try:
digits = re_has_digit.findall(input)[0]
number = int(digits)
print "%d is a number." % number
except IndexError:
print "'%s' has no number in it." % input

I didn't try it, but it should work.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: eval(repr(object)) hardly ever works

2006-09-13 Thread Hardcoded Software

Matthew Wilson wrote:
> I understand that idea of an object's __repr__ method is to return a
> string representation that can then be eval()'d back to life, but it
> seems to me that it doesn't always work.
>
> For example it doesn't work for instances of the object class:
>
> In [478]: eval(repr(object()))
> 
>File "", line 1
>  
>  ^
> SyntaxError: invalid syntax
>
> It seems to work for types like integers and dictionaries and lists,
> but not for much else.
>
> Any thoughts?
>
>
> --
> A better way of running series of SAS programs:
> http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles

I don't think that repr() is for eval(). repr() is for outputting a
string that represent the object and is not ambiguous. Example: print
'foo' == print u'foo' but print repr('foo') != print repr(u'foo')

-- 
http://mail.python.org/mailman/listinfo/python-list