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

John Krukoff jkrukoff at ltgc.com
Tue Apr 15 17:16:07 EDT 2008


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 at it these days, less than 1 typo per function!).
> It leads me to the following questions
> 
> 1. Is this functionality already built in and im just missing it
> 2. Is there some well known, good technique for these that I missed?
> 3. Insert question I need to ask here (with a response)
> 
> These are the funtions w/ explaination:
> 
> def nsplit(s,p,n):
>     n -= 1
>     l = s.split(p, n)
>     if len(l) < n:
>         l.extend([''] * (n - len(l)))
>     return l
> 
> 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)
> 
> 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 is
> iterable. I would like to see this in builtins, to mirror callable.
> The optional count_str adds flexibility for string handling, since
> sometimes I need to iterate over a string, but usually not. I
> frequently use it to simplify my case handling in this type of
> costruct:
> 
> def foo(bar):
>     bar = bar if iterable(bar) else [bar]
>     for x in bar:
>         ....
> 
> Thanks for feeback,
> Erich

As far as I know there is no built in function that does exactly what
you want. You can certainly simplify your nsplit function a bit, but as
mentioned, it's probably best just to create your own package and keep
your utility functions there.

It's worth noting that you almost certainly want to be doing
isinstance( item, basestring ) in your iterable function instead of
isinstance( item, str ), or things will get very surprising for you as
soon as you have to deal with a unicode string.

If you don't want the hassle of creating a separate package, and you're
only interested in having these functions be handy on your local python
install, you could also add them into your sitecustomize file as
described here:
http://docs.python.org/lib/module-site.html

On linux, that's as easy as creating a file
named /usr/lib/python2.5/sitecustomize.py that inserts whatever you want
into the __builtin__ module, and it'll be automatically imported
whenever you run python.

I'd doubt there's a case for getting this functionality added to the
language, as your use case seems pretty specific, and it's just not that
hard to write the function that does what you want to do.

-- 
John Krukoff <jkrukoff at ltgc.com>
Land Title Guarantee Company




More information about the Python-list mailing list