473,472 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cloning generator iterators

I have something like the following (using Python 2.5 syntax):

class adapt(object):
def __init__(self):
self.iterator = self.generator()
self.n = 0

def generator(self):
while True:
while True:
if (yield 1):
if (yield 1):
break
self.n = 2
while (yield self.n):
self.n += 1

def clone(self):
clone = self.__class__()
clone.n = self.n

# This assignment does not work!
clone.iterator.gi_frame.f_lasti = clone.iterator.gi_frame.f_lasti

a = adapt()
c = a.clone()

c should be an independent clone of a, *including the program counter*.

However, I have trouble adjusting the pc. The assignment above does not
work since f_lasti is a readonly attribute.

c1 = copy.copy(a)
c1.iterator = copy.copy(a.iterator)

also does not work, since a.iterator can not be copied.

Question: What is the best way to implement this clone operation?

Solutions I came up with:

1. Don't use generators, and program a state based approached directly,
2. Keep track of the values that where sent to the generator iterator,
and feed the same values to the clone.

I would like to keep the clone operation fairly independent from the
generator, so that I can try out different generators (the generator
above is probably not optimal).

2. is somewhat unsatisfactory, since with the clone operation become
more expensive with long histories.
Motivation:

Abstractly speaking, I have a list of elements, and the list as a
whole has property P. Now some of these elements can be deleted, and
the list still has property P. The goal of the program is to delete as
many elements of the list as possible without loosing property P.

Now assume the following additional two conditions:

1. Testing for property P is time consuming.

2. Elements which can be deleted often come in 'clusters', that is
elements at index i, i+1, i+2, i+3, ... can all be deleted without
loosing property p.

I am trying to address 1. and 2. by the above generator which I use to
determine how many elements to delete. After two successful deletions,
I delete 2, 3, 4, ... elements until the list does not show property P
anymore.

Now the generator helps, but it is still taking (too) long. On
multi-core machines (soon coming to a desktop near you) I can reduce
the elapsed time by testing three lists simultaneously: the second list
is constructed under the assumption that the first list has property
P, and the third list is constructed under the assumption that the
first list does *not* have property P.

My idea was to clone the class constructing the lists to test, which
includes a clone of the adapt class sketched above. One of the two
clones (for list 2 and 3) will be thrown away depending on the outcome
of the test of list 1.
Aug 19 '06 #1
2 2707
Oops. Got the indentation wrong. Here is the corrected version:

def generator(self):
while True:
while True:
if (yield 1):
if (yield 1):
break
self.n = 2
while (yield self.n):
self.n += 1
Aug 19 '06 #2
Bernhard Mulder wrote:
[ attempt to clone/fork a generator ]
You can do this, but you can't pickle the results. (If you want pickling,
use Stackless - I've not tried pickling generators in stackless because I
don't use stackless, but it looks pretty clear you can pickle them there)
Question: What is the best way to implement this clone operation?
However you should be able to do what you want using the small extension
"statesaver" which you can grab from
* http://www.gosubway.org/install/statesaver.c

The statesaver allows you to clone generators in the way that you want.

FWIW, I can see lots of situations where this would be useful - mainly in
the area of dealing with search spaces (After all, this effectively allows
you to fork the generator).
Michael.
--
http://kamaelia.sourceforge.net/Home - Concurrency, Networking, Simplicity

Aug 20 '06 #3

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

Similar topics

9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
2
by: Clark C. Evans | last post by:
There is an interesting difference between how exceptions are handled between iterators constructed from a class, and iterators constructed from a generator. The following are "mostly equivalent":...
24
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I...
8
by: Paul Chiusano | last post by:
I've been playing around with generators and have run into a difficulty. Suppose I've defined a Node class like so: class Node: def __init__(self, data=None, left=None, right=None):...
10
by: John Machin | last post by:
Please consider the timings below, where a generator expression starts out slower than the equivalent list comprehension, and gets worse: >python -m timeit -s "orig=range(100000)"...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
11
by: vbgunz | last post by:
I am afraid that this is the first time in which I would probably need something explained to me as if I were a little child. I am having a hard time getting this through my thick skull. What in...
4
by: Kenneth McDonald | last post by:
I'm trying to write a 'flatten' generator which, when give a generator/iterator that can yield iterators, generators, and other data types, will 'flatten' everything so that it in turns yields...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.