473,785 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iterator clone

Whats is the way to clone "independen t" iterator? I can't use tee(),
because I don't know how many "independen t" iterators I need. copy and
deepcopy doesn't work...

--pavel
Jul 13 '08 #1
13 6365
Yosifov Pavel wrote:
Whats is the way to clone "independen t" iterator? I can't use tee(),
because I don't know how many "independen t" iterators I need. copy and
deepcopy doesn't work...
There is no general way. For "short" sequences you can store the items in a
list which is also the worst-case behaviour of tee().

What are you trying to do?

Peter

Jul 13 '08 #2
On 13 ÉÀÌ, 14:12, Peter Otten <__pete...@web. dewrote:
Yosifov Pavel wrote:
Whats is the way to clone "independen t" iterator? I can't use tee(),
because I don't know how many "independen t" iterators I need. copy and
deepcopy doesn't work...

There is no general way. For "short" sequences you can store the items ina
list which is also the worst-case behaviour of tee().

What are you trying to do?

Peter
I try to generate iterators (iterator of iterators). Peter, you are
right! Thank you. For example, it's possible to use something like
this:

def cloneiter( it ):
"""return (clonable,clone )"""
return tee(it)

and usage:

clonable,seq1 = cloneiter(seq)

...iter over seq1...
then clone again:

clonable,seq2 = cloneiter(clona ble)

...iter over seq2...

Or in class:

class ReIter:
def __init__( self, it ):
self._it = it
def __iter__( self ):
self._it,ret = tee(self._it)
return ret

and usage:

ri = ReIter(seq)
...iter over ri...
...again iter over ri...
...and again...

But I think (I'm sure!) it's deficiency of Python iterators! They are
not very good...

--Pavel
Jul 13 '08 #3
Yosifov Pavel wrote:
On 13 июл, 14:12, Peter Otten <__pete...@web. dewrote:
>Yosifov Pavel wrote:
Whats is the way to clone "independen t" iterator? I can't use tee(),
because I don't know how many "independen t" iterators I need. copy and
deepcopy doesn't work...

There is no general way. For "short" sequences you can store the items in
a list which is also the worst-case behaviour of tee().

What are you trying to do?

Peter

I try to generate iterators (iterator of iterators). Peter, you are
right! Thank you. For example, it's possible to use something like
this:

def cloneiter( it ):
"""return (clonable,clone )"""
return tee(it)
[snip]

That is too abstract, sorry. What concrete problem are you trying to solve
with your cloned iterators? There might be a way to rearrange your setup in
a way that doesn't need them.
But I think (I'm sure!) it's deficiency of Python iterators! They are
not very good...
Well, I think Python's iterators, especially the generators, are beautiful.
More importantly, I think there is no general way to make iterators
copyable, regardless of the programming language. The problem is that most
of the useful ones depend on external state.

Peter
Jul 13 '08 #4
Well, I think Python's iterators, especially the generators, are beautiful.
More importantly, I think there is no general way to make iterators
copyable, regardless of the programming language. The problem is that most
of the useful ones depend on external state.

Peter
Hmm, but tee() de facto do it (clone iterator) and ignore side-effects
of iterator ("external" state). And tee() create independent
**internal** state of iterator (current position). But **external**
state - is headache of programmer. So, iterator/generator have to be
method for copy itself (the tee() implementation) or be "re-
startable". Why not?

Concrete problem was to generate iterators (iterator of slices). It
was solved with ReIter.

--Best regards,
--pavel
Jul 14 '08 #5
On Sun, 13 Jul 2008 18:51:19 -0700, Yosifov Pavel wrote:
>Well, I think Python's iterators, especially the generators, are beautiful.
More importantly, I think there is no general way to make iterators
copyable, regardless of the programming language. The problem is that most
of the useful ones depend on external state.

Hmm, but tee() de facto do it (clone iterator) and ignore side-effects
of iterator ("external" state). And tee() create independent
**internal** state of iterator (current position).
`tee()` doesn't copy the iterator or its internal state but just caches
it's results, so you can iterate over them again. That makes only sense
if you expect to use the two iterators in a way they don't get much out of
sync. If your usage pattern is "consume iterator 1 fully, and then
re-iterate with iterator 2" `tee()` has no advantage over building a list
of all results of the original iterator and iterate over that twice.
`tee()` would be building this list anyway.
But **external** state - is headache of programmer. So,
iterator/generator have to be method for copy itself (the tee()
implementation) or be "re- startable". Why not?
Because it's often not possible without generating a list with all
results, and the advantage of a low memory footprint is lost.

Ciao,
Marc 'BlackJack' Rintsch
Jul 14 '08 #6
`tee()` doesn't copy the iterator or its internal state but just caches
it's results, so you can iterate over them again. That makes only sense
if you expect to use the two iterators in a way they don't get much out of
sync. If your usage pattern is "consume iterator 1 fully, and then
re-iterate with iterator 2" `tee()` has no advantage over building a list
of all results of the original iterator and iterate over that twice.
`tee()` would be building this list anyway.
It's interesting and a concrete answer. Thanks a lot.
Because it's often not possible without generating a list with all
results, and the advantage of a low memory footprint is lost.

Ciao,
Marc 'BlackJack' Rintsch
Seems like "monada". But I think is possible to determine when there
is a bounded external state (side-effects) or not, may be is needed
some new class-protocol for it... or something else. Or another way:
iterators may be re-iterable always, but if programmer need to point
to the extra- (external) state, he has to raise some a special
exception in __iter)) method... OK, it's only fantasies about language
design :-)

--pavel
Jul 14 '08 #7
On 13 juil, 12:05, Yosifov Pavel <b...@ngs.ruwro te:
(snip)
def cloneiter( it ):
"""return (clonable,clone )"""
return tee(it)
This might as well be written as

cloneiter = tee

Or yet better, just remove the above code and s/cloneiter/tee/g in the
remaining...

Jul 14 '08 #8
On 13 Jul., 08:53, Yosifov Pavel <b...@ngs.ruwro te:
Whats is the way to clone "independen t" iterator? I can't use tee(),
because I don't know how many "independen t" iterators I need. copy and
deepcopy doesn't work...

--pavel
You can try generator_tools

http://pypi.python.org/pypi/generator_tools/0.3.3

Jul 15 '08 #9
On 14 ÉÀÌ, 23:36, "bruno.desthuil li...@gmail.com "
<bruno.desthuil li...@gmail.com wrote:
On 13 juil, 12:05, Yosifov Pavel <b...@ngs.ruwro te:
(snip)
defcloneiter( it ):
"""return (clonable,clone )"""
return tee(it)

This might as well be written as

cloneiter = tee

Or yet better, just remove the above code and s/cloneiter/tee/g in the
remaining...
Yes, sure. It was only for illustration. BUT: Marc Rintsch is right:
cloning of iterators in this manner is bad, more good is to use one,
single list(my_iter) instead of (see
http://aquagnu.blogspot.com/2008/07/...n-python.html).

Thanks to all!

--pavel
Jul 15 '08 #10

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

Similar topics

0
2669
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some not so obvious errors I have made.. Cheers
38
3689
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
6
2151
by: Steve | last post by:
I can't find a straight answer on what to use? I need a deep copy, so I implemented IConeable and the Clone() method. However, I'm not sure I did it correct. Is it suposed to be an allocation of a new object, then the assignment of each member? Is that all or is there something else I need to do? Here is my code <code> public Object Clone() {
4
4322
by: Brian Keating | last post by:
Hi there, Consider this from MSDN *Notes to Inheritors When you derive from DataGridViewCheckBoxCell and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell. * How should I do this? a)
1
3822
by: Alex D. | last post by:
hi guys. I need to clone multiple times an object and I am succesfully cloning using the regular serialization process, using a MemoryStream. My problem is that after cloning the object more that 7 or 10 times then my computer's memory gets flooded and every time I call the Clone() method the processor resources gets consumed 100% for like 30 seconds. And this problem increases as the amount of clones are created increases. I read some...
2
11469
by: Steven | last post by:
Hi, I have created my own node (class MyNode : TreeNode) for a TreeView. To populate the treeview, i use something like MyNode newNode = new MyNode("Bla bla bla","0","1") for example. But, to move the nodes in the treeview, i use the clone() method... : MyNode theCopy = (MyNode)theTreeView.SelectedNode.Clone();
16
2518
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some objects (like Font, Style, String,
14
8640
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event handlers are set like the old one. Is there a way to do this job? For example, is there a way to use EventInfo object to get all event handlers of the old control in runtime and set my new cloned control events to the event handlers of the old...
7
2845
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
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 we have to send another system
3
2877
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.