473,480 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

copying generatrors

Does anyone have code to copy a generator?

Here is what I'd like to do:

def foo():
yield 1
yield 2
yield 3

f = foo()
g = copy(foo)

print f.next()
1
print f.next()
2

print g.next()
1

Thanks,

Horace
Jun 5 '07 #1
4 1036
Why not just do this:
>>def foo():
.... yield 1
.... yield 2
.... yield 3
....
>>f = foo()
g = foo()
f.next()
1
>>f.next()
2
>>f.next()
3
>>g.next()
1
>>g.next()
2
>>g.next()
3

Jun 5 '07 #2
My example wasn't very good. Here's another try:

def foo():
yield 1
yield 2
yield 3

f = foo()
f.next()
1

g=copy(f) # copy the generator after an iteration

f.next()
2
f.next()
3

g.next()
2

I want to copy the generator's state after one or more iterations.

In article <11**********************@o11g2000prd.googlegroups .com>,
Matimus <mc******@gmail.comwrote:
Why not just do this:
>def foo():
... yield 1
... yield 2
... yield 3
...
>f = foo()
g = foo()
f.next()
1
>f.next()
2
>f.next()
3
>g.next()
1
>g.next()
2
>g.next()
3
Jun 6 '07 #3
Horace Enea wrote:
My example wasn't very good. Here's another try:

def foo():
yield 1
yield 2
yield 3

f = foo()
f.next()
1

g=copy(f) # copy the generator after an iteration

f.next()
2
f.next()
3

g.next()
2

I want to copy the generator's state after one or more iterations.
You could use itertools.tee():
>>def foo():
.... yield 1
.... yield 2
.... yield 3
....
>>import itertools
f = foo()
f.next()
1
>>f, g = itertools.tee(f)
f.next()
2
>>f.next()
3
>>g.next()
2
>>g.next()
3

But note that if your iterators get really out of sync, you could have a
lot of elements stored in memory.

STeVe
Jun 6 '07 #4
Steve,

Hey, thanks. I'll try that.

Horace

In article <-J******************************@comcast.com>,
Steven Bethard <st************@gmail.comwrote:
Horace Enea wrote:
My example wasn't very good. Here's another try:

def foo():
yield 1
yield 2
yield 3

f = foo()
f.next()
1

g=copy(f) # copy the generator after an iteration

f.next()
2
f.next()
3

g.next()
2

I want to copy the generator's state after one or more iterations.

You could use itertools.tee():
>>def foo():
... yield 1
... yield 2
... yield 3
...
>>import itertools
>>f = foo()
>>f.next()
1
>>f, g = itertools.tee(f)
>>f.next()
2
>>f.next()
3
>>g.next()
2
>>g.next()
3

But note that if your iterators get really out of sync, you could have a
lot of elements stored in memory.

STeVe
Jun 6 '07 #5

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

Similar topics

5
9602
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
3
3616
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
4
2938
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
13
17476
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
22
6927
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable...
0
1519
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and...
0
1559
by: surya | last post by:
Hi, I need to enable shadow copying network share folder assemblies. Currently if the path is given as the network share folder path, shadow copying is not enabled. Should something special be...
1
8197
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
1
1519
by: emilysmith10 | last post by:
I am creating a new database by copying a template .mdb file. This template has assigned permissions for each of it's objects. I know that just by copying the file, I lose these permissions. Is...
1
2781
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. ...
0
7046
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
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
6956
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
5342
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
4783
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
4485
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
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.