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

iterator wrapper

alf
Hi,

I have a following task: let's say I do have an iterator returning the
some objects:
>>i=<iterator>
i.next()
1
>>i.next()
'abgfdgdfg'
>>i.next()
<some object>
For some reason I need to wrap thos objects with a list. I thought I
could have a smart lamda or simple function class yielding following result:

>>i=<iterator>
i=list_wrapper(i)
i.next()
[1]
>>i.next()
['abgfdgdfg']
>>i.next()
[<some object>]
What would thesolution?
Thx,

A.
Aug 11 '06 #1
9 2035

alf wrote:
Hi,

I have a following task: let's say I do have an iterator returning the
some objects:
>>i=<iterator>
>>i.next()
1
>>i.next()
'abgfdgdfg'
>>i.next()
<some object>
For some reason I need to wrap thos objects with a list. I thought I
could have a smart lamda or simple function class yielding following result:

>>i=<iterator>
>>i=list_wrapper(i)
>>i.next()
[1]
>>i.next()
['abgfdgdfg']
>>i.next()
[<some object>]
What would thesolution?
If I understand you correctly, something like this:
>>stuff = ['a', 'b', 'c', 'd']
i1 = iter(stuff)
i1.next()
'a'
>>i1.next()
'b'
>>class LW(object): # ListWrapper
.... def __init__(self, i):
.... self.theiter = i
.... def next(self):
.... return [self.theiter.next()]
....
>>i2 = iter(stuff)
x = LW(i2)
x.next()
['a']
>>x.next()
['b']
>>x.next()
['c']
>>x.next()
['d']
>>x.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in next
StopIteration

Cheers,
John

Aug 11 '06 #2
John Machin wrote:
alf wrote:
Hi,

I have a following task: let's say I do have an iterator returning the
some objects:
>>i=<iterator>
>>i.next()
1
>>i.next()
'abgfdgdfg'
>>i.next()
<some object>
For some reason I need to wrap thos objects with a list. I thought I
could have a smart lamda or simple function class yielding following result:

>>i=<iterator>
>>i=list_wrapper(i)
>>i.next()
[1]
>>i.next()
['abgfdgdfg']
>>i.next()
[<some object>]
What would thesolution?

If I understand you correctly, something like this:
>stuff = ['a', 'b', 'c', 'd']
i1 = iter(stuff)
i1.next()
'a'
>i1.next()
'b'
>class LW(object): # ListWrapper
... def __init__(self, i):
... self.theiter = i
... def next(self):
... return [self.theiter.next()]
...
>i2 = iter(stuff)
x = LW(i2)
x.next()
['a']
>x.next()
['b']
>x.next()
['c']
>x.next()
['d']
>x.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in next
StopIteration

Cheers,
John
You could also use a generator comprehension:

([n] for n in i)

|>i = iter(xrange(3)) # iter() just for kicks..
|>I = ([n] for n in i)
|>I.next()
[0]
|>I.next()
[1]
|>I.next()
[2]
|>I.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

Peace,
~Simon

Aug 11 '06 #3
alf
Simon Forman wrote:
>>>>>class LW(object): # ListWrapper
... def __init__(self, i):
... self.theiter = i
... def next(self):
... return [self.theiter.next()]

I hoped one lamda would take care of it but looks like it is a simplest
choice.
|>I = ([n] for n in i)
This is nice but I am iterating thru hude objects (like MBs) so you know ...
Thx for responding ...
A.
Aug 12 '06 #4
alf <ask@mewrites:
|>I = ([n] for n in i)

This is nice but I am iterating thru hude objects (like MBs) so you know ...
I don't understand the objection-- the above is entirely correct and
produces the same iterator you'd get from

def wrap(i):
for x in i:
yield [x]
I = wrap(i)

You could also use

import itertools
I = itertools.imap(lambda x: [x], i)

which again does the same thing.
Aug 12 '06 #5
alf wrote:
Simon Forman wrote:
>>>>class LW(object): # ListWrapper
... def __init__(self, i):
... self.theiter = i
... def next(self):
... return [self.theiter.next()]


I hoped one lamda would take care of it but looks like it is a simplest
choice.
|>I = ([n] for n in i)

This is nice but I am iterating thru hude objects (like MBs) so you know ...
Thx for responding ...
A.
No, I don't know... :-)

My friend, I think you've misunderstood. Observe:

|>L = [n for n in range(3)]
|>G = (n for n in range(3))
|>L
[0, 1, 2]
|>G
<generator object at 0xb7d982ec>
|>L.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'next'
|>G.next()
0

List comprehensions [] create lists, generator comprehensions () create
generators.

Generator comprehensions work "just-in-time", pulling items from
whatever they're iterating over as they themselves are iterated over,
as I hope this example makes clear:

|>i = iter(xrange(3))
|>G = ([n] for n in i)
|>G.next()
[0]
|>i.next()
1
|>G.next()
[2]
|>G.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

So, ([n] for n in i) is indeed sufficient to your needs, as I
understand them.

BTW, the ()'s of a function call serve to create generator
comprehensions:

|>sum(n * 2 for n in range(3))
6

Neat, eh?

HTH,
~Simon

"Some say it is better to give than to receive. We say it is better to
take than to receive. Note the subtle difference."

Aug 12 '06 #6
alf
Simon Forman wrote:
>>
>>>|>I = ([n] for n in i)

This is nice but I am iterating thru hude objects (like MBs) so you know ...

No, I don't know... :-)
potentially my source lists are huge - so wanted to avoid unnecessary
memory allocation

My friend, I think you've misunderstood. Observe:

|>L = [n for n in range(3)]
|>G = (n for n in range(3))
|>L
[0, 1, 2]
|>G
<generator object at 0xb7d982ec>
well, I am in the python 2.3 word where generator comprehensions seem
not to work. So I just took it a preallocated list comprehention. still
wonder if there would be a difference between:

G = (n for n in range(300000000)) - this creates the huge list there
G = (n for n in xrange(300000000)) - this (at least to my understanding)
does not

>
List comprehensions [] create lists, generator comprehensions () create
generators.
Generator comprehensions work "just-in-time", pulling items from
whatever they're iterating over as they themselves are iterated over,
as I hope this example makes clear:

[...]
got it now ... thx or the lesson....
A.
Aug 12 '06 #7
alf
Paul Rubin wrote:
alf <ask@mewrites:
>>>|>I = ([n] for n in i)

This is nice but I am iterating thru hude objects (like MBs) so you know ...


I don't understand the objection-- the above is entirely correct and
produces the same iterator you'd get from

def wrap(i):
for x in i:
yield [x]
I = wrap(i)

You could also use

import itertools
I = itertools.imap(lambda x: [x], i)

which again does the same thing.

I did see [] instead of () :-). this was a source of confusion.
okay, we have 3 functionally equal solution - which is most pythonic :-)
..... seroiusly ...

--
alf
Aug 12 '06 #8
alf wrote:
Simon Forman wrote:
>
|>I = ([n] for n in i)

This is nice but I am iterating thru hude objects (like MBs) so you know ...
No, I don't know... :-)

potentially my source lists are huge - so wanted to avoid unnecessary
memory allocation

My friend, I think you've misunderstood. Observe:

|>L = [n for n in range(3)]
|>G = (n for n in range(3))
|>L
[0, 1, 2]
|>G
<generator object at 0xb7d982ec>

well, I am in the python 2.3 word where generator comprehensions seem
not to work. So I just took it a preallocated list comprehention. still
wonder if there would be a difference between:

G = (n for n in range(300000000)) - this creates the huge list there
G = (n for n in xrange(300000000)) - this (at least to my understanding)
does not
Yes, you've got it, the xrange() version will not allocate a huge list.

It's not part of your main question, and I understand that there may be
reasons why you can't, but consider upgrading to 2.4 (or very soon now
2.5...)

Meanwhile, in 2.3 both John Machin's and Paul Rubin's solutions will do
the trick (as you already know ;-) )

List comprehensions [] create lists, generator comprehensions () create
generators.

Generator comprehensions work "just-in-time", pulling items from
whatever they're iterating over as they themselves are iterated over,
as I hope this example makes clear:

>[...]

got it now ... thx or the lesson....
A.
No problem. These are things I learned over time, it's fun to share
the knowledge. :)

Peace,
~Simon

Aug 12 '06 #9
alf
Simon Forman wrote:
Yes, you've got it, the xrange() version will not allocate a huge list.

It's not part of your main question, and I understand that there may be
reasons why you can't, but consider upgrading to 2.4 (or very soon now
2.5...)
upgrade to 2.4 is on the roadmap and will take place soon .... the app
is just too big and must be stable so since all works in 2.3 well we
stick to it.

there are some parts (related to wx where I noted that 2.4 behaves
differently so it needs some attention) ...
other reason is there is a number of users using customized python (on
top of standard install there is wx, twistedmatrix, matplot and a lot of
small libs/modules == 200MB) so it is kind of pin to migrate that from
the deployment point of view. Tried to have Python on network drive but
it is painfully slow. wonder what strategy should I choose while the
priority is the stable app.

--
alf
Aug 12 '06 #10

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

Similar topics

4
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help...
13
by: Grahamo | last post by:
Hi, I'm implementing a custom iterator (random access type ) so I can use stl algorithms such as sort on my legacy containers. I'm having problems compiling however. when implementing my...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
4
by: james t kirk | last post by:
I'm writing a wrapper class to handle the line merging and filtering for a log file analysis app The problem I'm running into is that the StopIteration exception raised when the wrapped file...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
3
by: raan | last post by:
Is iterators singleton? I am accessing a map through wrapper functions to it; that allow me to access the contents of the map as CUnit &unit = hash.getFirstunit(); while(hash.hasMoreunits())...
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.