[IronPython] mapping C# iterators to Python iterator

amit regmi.amit at gmail.com
Tue Dec 4 01:02:17 CET 2007


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
>
>   




More information about the Ironpython-users mailing list