[IronPython] mapping C# iterators to Python iterator

Dino Viehland dinov at exchange.microsoft.com
Tue Dec 4 01:19:39 CET 2007


I think there's a bug here in that we're not defining __iter__ on Stack (it's enumerable but it doesn't support indexing it's items).  There's not a whole lot of objects that are like that :)  I've opened a bug to track this issue: http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=14196

My best advice to work around this is to call .GetEnumerator explicitly on the object and then use the resulting enumerator to access the values.  Note you still won't get a __getitem__ method to call (and self.iterObj[temp] would be more Pythonic anyway) so you'll need to use a list comprehension or an old-fashioned loop to collect the objects if you want indexing.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of amit
Sent: Monday, December 03, 2007 4:02 PM
To: Discussion of IronPython
Subject: Re: [IronPython] mapping C# iterators to Python iterator

thanks tonnes Dino.
:)

I did as you said and got it working for ArrayList class.
Now I am struggling with another issue
the problem is in the below code wherein I could return ArrayList values
with __getitem__
For other classes say 'Stack' though it wont work because it wont have
__getitem__

I think I am missing something here. May be some sort of method
invocation to GetEnumerator(), MoveNext(), Current() could help.

class Dummy(object):
def __init__(self, iterObj):
self.iterObj = iterObj
self.index = 0

def next(self):
temp = self.index
if self.index == self.iterObj.Count:
raise StopIteration
self.index = self.index + 1
return self.iterObj.__getitem__(temp)


class CliClassWrapper(object): # this is the class wrapper for
on-the-fly class generation
__slots__ = ('__cliobj__',)

def __iter__(self):
return Dummy(self) # this is where I pass the object


- Regmee


Dino Viehland wrote:
> To map these to .NET's IEnumerator/IEnumerable you just need to define __iter__ or __getitem__.  If you define __iter__ you should return something that is enumerable (implements IEnumerable or has a next method which we'll call until it throws to stop enumeration).  If you define __getitem__ we'll try and index from 0 to infinity and stop at an index out of range or stop iteration exception.
>
> You explicitly mention "say for ArrayList object" and I'm not sure what that means - we won't convert an arbitrary enumerable into another type such as ArrayList.  But we will convert it one of the enumeration interfaces.  When we pass your object out it'll be wrapped in one of our PythonEnumerable/PythonEnumerator/ItemEnumerable/ItemEnumerator classes.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of amit
> Sent: Saturday, December 01, 2007 4:15 PM
> To: users at lists.ironpython.com
> Subject: [IronPython] mapping C# iterators to Python iterator
>
> Hi,
>
> I want to know how the "Iterators" in C# can be mapped to those in Python.
> What I tried was:
>
> say for ArrayList object
>
>     def __iter__(self):
>         self.index = self.Count
>         return self
>
>     def next(self):
>         if self.index == 0:
>             raise StopIteration
>         self.index = self.index - 1
>         return ??
>
> If I am creating python classes dynamically how would I map
> GetEnumerator() , MoveNext() , Current() , Reset()
> to python Class so as to make it iterable inside python.
>
> --Regmee
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>

_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list