[IronPython] Why won't these snippets run?

Martin Maly Martin.Maly at microsoft.com
Sun Sep 18 19:54:48 PDT 2005


Hi Ray, 

> Ray Djajadinata Wrote:
> 
> fileBeforeSort = file("READ_ME_AND_SORT_ME.txt", "r") 
> afterSort = sorted(int(line.rstrip('\n')) for line in fileBeforeSort)
> fileBeforeSort.close()
> 
> IronPython tripped on the 2nd line, it said:
> 
> SyntaxError: unexpected token for at
> ReadAndSortAndWriteInPython.py:2
> 

This code uses generator expressions which IronPython doesn't support
yet.
It is Python 2.4 feature and we are just starting to add 2.4 features
Into IronPython. I am hoping to add the support for generator
expressions
soon.

> Also, when I tried to use Python to write what is basically a 
> C# program:
> 
> <snippet>
> from System import Array, Convert
> from System.IO import File
> 
> reader = File.OpenText("READ_ME_AND_SORT_ME.txt")
> beforeSort = reader.ReadToEnd().Split('\n')
> reader.Close()
> 		
> beforeSortInt = [Convert.ToInt32(number) for number in beforeSort]
> 
> Array.Sort(beforeSortInt)
> </snippet>

I don't see reason for this to not work, what error are you getting?

> Also, if I use parentheses during conversion like this:
> 
> beforeSortInt = (Convert.ToInt32(number) for number in beforeSort)

Again, this doesn't work because of the missing enerator expressions.

> I managed to make that disappear by turning the parentheses 
> into square brackets, but then I got this:
> 
> D:\Development\Book>IronPythonConsole
> ReadAndSortAndWriteInPython2.py
> <type 'list'>
> Traceback (most recent call last):
>    at __main__.Initialize() in
> D:\Development\Book\ReadAndSortAndWriteInPython2.
> py:line 12
> ValueError: Bad args for the method <method# Sort on System.Array>
> 
> OK, so beforeSortInt is a Python list, whereas Array.Sort 
> expects a System.Array, but... shouldn't this translate 
> transparently? Or am I missing something real silly here?

That's exactly the reason why it doesn't work. Python lists don't
Translate to .NET arrays automatically. I hope this is something
we'll fix soon.


Martin


More information about the users-ironpython.com mailing list