473,246 Members | 1,776 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,246 software developers and data experts.

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 1033
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
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
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
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
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
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
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
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
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
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
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. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.