473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to convert a function into generator?

Hello,

I came up with this algorithm to generate all permutations
it's not the best one, but it's easy enough

# lst = list with objects
def permute3(lst):
tmp = []
lenlst = len(lst)
def permute(perm, level):
if level == 1:
tmp.append(perm)
return
for i in lst:
if i not in perm:
permute(perm + (i,), level - 1)
for item in lst:
permute((item,), lenlst)
return tuple(tmp)

now I want to make a generator from it
the idea is to get each time a new permutation
I don't really understand how to handle the case
when my function has an inner function which has
a yield statement ..
I would say that the inner function becomes an generator
and stops&yields the value on each yield .. but in reality
I want to propogate these values to the caller of the outer function
I hope you got the idea of what I mean
the code is the sketch of the idea

def permute3gen(lst):
lenlst = len(lst)
def permute(perm, level):
if level == 1:
yield perm
return # not sure return without a value is allowed,
theoretically it could be replaces with if/else block
for i in lst:
if i not in perm:
permute(perm + (i,), level - 1)
for item in lst:
yield permute((item,), lenlst) # makes generator from the outer
function too
this is what I get

In [67]: reload permute
-------reload(permute)
Out[67]: <module 'permute' from 'permute.pyc'>

In [68]: p = permute.permute3gen(["a","b","c","d"])

In [69]: p
Out[69]: <generator object at 0x2af3a44795f0>

In [70]: x = p.next()

In [71]: y = p.next()

In [72]: x
Out[72]: <generator object at 0x2af3a448e830>

In [73]: y
Out[73]: <generator object at 0x2af3a448e878>

In [74]: x.next()
---------------------------------------------------------------------------
<type 'exceptions.StopIteration' Traceback (most recent call last)

/pool/PROG/python/permute/<ipython consolein <module>()

<type 'exceptions.StopIteration'>:

I don't understand why the generator x raises StopIteration exception
I would expect that x.next() would call
permute(("a",), 4) and would stop at "abcd"

thanks in advance

regards, Daniel
Dec 7 '06 #1
1 1631
On Wed, 2006-12-06 at 17:33 +0100, Schüle Daniel wrote:
def permute3gen(lst):
lenlst = len(lst)
def permute(perm, level):
if level == 1:
yield perm
return # not sure return without a value is allowed,
theoretically it could be replaces with if/else block
for i in lst:
if i not in perm:
permute(perm + (i,), level - 1) ##1##
for item in lst:
yield permute((item,), lenlst) # makes generator from the outer
function too ##2##
Your only problem is in knowing what to do with recursively invoked
sub-generators. You have two such invocations, which I marked above with
##1## and ##2##, and neither one is handled correctly.

In ##1##, you construct a sub-generator and simply discard it. In ##2##,
you construct a sub-generator, and yield it (instead of yielding its
elements). In neither case do you actually consume any of the elements
that the sub-generators are prepared to produce.

To usefully invoke a sub-generator, you need to consume it (i.e. iterate
over it) and yield whatever it produces.

Hope this helps,

Carsten
Dec 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TheDustbustr | last post by:
<code> from __future__ import generators from time import time threads= def sleep(n): print "In Sleep" t=time() while (t+n>time()): yield None
2
by: chansky | last post by:
I read the following link about generator: http://www.python.org/peps/pep-0255.html but I am still not so clear on the use/purpose of a generator function other than the fact that a generator can...
8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
2
by: Fred Ma | last post by:
Hello, I have a random generator that takes a scaling factor as an argument. Since it takes one argument, it is not a generator in the sense defined by SGI's online STL documentation. I'd...
2
by: AngleWyrm | last post by:
Ok, I know that srand( time(NULL) ) isn't such a good idea, but I do it anyway. Why? The clock is an easy source of randomness. And I've got this sneaky suspicion that I'm not the first guy to...
2
by: name | last post by:
The piece of code is for a Web Form Page. Who can tell me why? Thanks a lot! ------------------------------------------------ VB.Net Code: Protected Overrides Sub AddParsedSubObject(ByVal obj As...
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
3
by: SpreadTooThin | last post by:
I am wondering if someone who knows the implemention of python's time could help converting this to c/c++.... nanoseconds = int(time.time() * 1e9) # 0x01b21dd213814000 is the number of 100-ns...
0
by: Terry Reedy | last post by:
Rui Jorge Rei wrote: Futile, I believe. I would also like to get all the arguments and keyword The arguments to a generator function are the arguments to the generator 's next method. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.