Testing for callable or iterable (was: Recurring patterns: Am I missing it, or can we get these added to the language?)

2008-04-15 Thread Ben Finney
Erich <[EMAIL PROTECTED]> writes: > def iterable(item, count_str=False): > if not count_str and isinstance(item, str): > return False > try: > iter(item) > except: > return False > return True > This is just simple boolean test for whether or not an object

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread John Krukoff
On Tue, 2008-04-15 at 13:48 -0700, Jeffrey Froman wrote: > Tim Chase wrote: > >def nsplit(s, delim=None, maxsplit=None): > > if maxsplit: > >results = s.split(delim, maxsplit) > >result_len = len(results) > >if result_len < maxsplit: > > results.extend([''

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread John Krukoff
On Tue, 2008-04-15 at 11:51 -0700, Erich wrote: > Hello all, > > Today I found myself once again defining two functions that I use all > the time: nsplit and iterable. These little helper functions of mine > get used all the time when I work. Im sick of having to define them > (but am very good

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Jeffrey Froman
Erich wrote: > def iterable(item, count_str=False): > if not count_str and isinstance(item, str): > return False > try: > iter(item) > except: > return False > return True Beware the "except" clause here, as it catches *all* errors. Thus, if you happen to ha

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Jeffrey Froman
Tim Chase wrote: >def nsplit(s, delim=None, maxsplit=None): > if maxsplit: >results = s.split(delim, maxsplit) >result_len = len(results) >if result_len < maxsplit: > results.extend([''] * (maxsplit - result_len) >return results > else: >

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Mike Driscoll
On Apr 15, 3:15 pm, Tim Chase <[EMAIL PROTECTED]> wrote: > > My suggestion would just be to create your own utils.py module > that holds your commonly used tools and re-uses them > > -tkc Well, I almost said that, but I was trying to find some "battery" included that he could use since the OP s

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Tim Chase
>> def nsplit(s,p,n): >> n -= 1 >> l = s.split(p, n) >> if len(l) < n: >> l.extend([''] * (n - len(l))) >> return l > > The split() method has a maxsplit parameter that I think does the same > thing. For example: > temp = 'foo,bar,baz' temp.split(',', 1) > ['foo'

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread David
> Today I found myself once again defining two functions that I use all > the time: nsplit and iterable. These little helper functions of mine > get used all the time when I work. Im sick of having to define them > (but am very good at it these days, less than 1 typo per function!). > It lead

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Robin Stocker
Erich schrieb: > This is like split() but returns a list of exactly lenght n. This is > very useful when using unpacking, e.g.: > x, y = nsplit('foo,bar,baz', ',', 2) You could use the second argument of split: x, y = 'foo,bar,baz'.split(',', 1) Note that the number has the meaning "only spli

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Mike Driscoll
On Apr 15, 1:51 pm, Erich <[EMAIL PROTECTED]> wrote: > Hello all, > > Today I found myself once again defining two functions that I use all > the time: nsplit and iterable. These little helper functions of mine > get used all the time when I work. Im sick of having to define them > (but am very go

Re: Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Mike Driscoll
On Apr 15, 1:51 pm, Erich <[EMAIL PROTECTED]> wrote: > Hello all, > > Today I found myself once again defining two functions that I use all > the time: nsplit and iterable. These little helper functions of mine > get used all the time when I work. Im sick of having to define them > (but am very go

Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Erich
Hello all, Today I found myself once again defining two functions that I use all the time: nsplit and iterable. These little helper functions of mine get used all the time when I work. Im sick of having to define them (but am very good at it these days, less than 1 typo per function!). It leads m