function while true - list function? 
Author Message
 function while true - list function?

Hi there,

is there a function in python which will populate a list with the results
of a function while the function returns something other than 'None'?  I've
looked for something but can't find much, but it's something I'm doing so
often (especially while processing SQL databases) that I'm wondering if, if
it doesn't already exist, it could be a good idea for future versions.

eg:

def GrabSomeData:
        somecursor.execute("SELECT x,y FROM z")
        return unroll(somecursor.fetchone)   *

* I figured "unroll" would be a good name for it :-)



Sat, 10 Jan 2004 22:00:19 GMT  
 function while true - list function?


Quote:
> Hi there,

> is there a function in python which will populate a list with the results
> of a function while the function returns something other than 'None'?
I've
> looked for something but can't find much, but it's something I'm doing so
> often (especially while processing SQL databases) that I'm wondering if,
if
> it doesn't already exist, it could be a good idea for future versions.

> eg:

> def GrabSomeData:
>         somecursor.execute("SELECT x,y FROM z")
>         return unroll(somecursor.fetchone)   *

> * I figured "unroll" would be a good name for it :-)

Here's a version of unroll which can take extra args:

def unroll(fn,*args,**kwds):
    results = []
    x = fn(*args,**kwds)
    while x != None:
        results.append(x)
        x = fn(*args,**kwds)
    return results

for record in unroll(cursor.fetchone):
    process(record)

This will create a list of all results of your function,
up to, but not including the first 'None' result.

...
Here's another way to do it, using generators:
(in python 2.2)

from __future__ import generators
def gen_unroll(fn, *args, **kwds):
    x = fn(*args,**kwds)
    while x != None:
        yield x
        x = fn(*args,**kwds)

for record in gen_unroll(cursor.fetchone):
    process(record)
    if somecondition(): break

The difference, with the generator version, is that
results are not all computed and put in a list, as with the
first version of unroll; they are generated only as they are needed.

With the generator version, if you decide to break out of processing records
before all are done, you will have saved the time needed to fetch all
records in advance.



Sat, 10 Jan 2004 23:48:52 GMT  
 function while true - list function?

Quote:
> Here's another way to do it, using generators:
> (in python 2.2)

And here is another (Python 2.2):

def gen_unroll(fn, *args, **kwds):
    return iter(lambda: fn(*args, **kwds), None)

--

 \__/
  ^^                      SYGNATURA ZASTPCZA
QRCZAK



Sun, 11 Jan 2004 00:22:44 GMT  
 function while true - list function?

An alternative to the generator class method:

Also in 2.2, the 'iter' function has two forms:

  iter( collection )  &
  iter( callable, sentinel )

The second form takes a callable object of no args and a sentinal
value, and returns an iterator that calls the function until it
returns the sentinel value.

For example:

Quote:
>>> i = iter( [1,2,3,None,4,5,6] )
>>> j = iter( i.next, None )
>>> for x in j: print x

...
1
2
3

So, if you can curry your function into one that takes no args
( with lambda or by turning it into a class method that keeps
 state ) or if that's what you've already got, you can just
use 'iter( function, None )' .

If you need to force eager evaluation of the iterator you
can use:      [ x for x in iter( function, None ) ]

-- Steve Majewski



Sun, 11 Jan 2004 00:28:28 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. compare function in sort function of lists

2. TIP #15: Functions to List and Detail Math Functions

3. VC++ calling fortran function and fortran function calling a c++ function

4. Calling functions from functions from functions ...

5. recursive function that returns true/false (newbie stuff)

6. converting C++ Function to Clarion 5 Function

7. Replace standard MESSAGE function in C4 with own message function

8. Clarion function prototypes for SPGP pgp wrapper functions

9. What might be equivalent clarion function for vb function IIF()

10. counter function : How works the Set attribute function

11. VI Logger functions dont appear on Labview Function Palettes

12. Passing variables to user functions of Clipper functions

 

 
Powered by phpBB® Forum Software