[IronPython] Why won't these snippets run?
Martin.Maly at microsoft.com
Sun Sep 18 19:54:48 PDT 2005
> Ray Djajadinata Wrote:
> fileBeforeSort = file("READ_ME_AND_SORT_ME.txt", "r")
> afterSort = sorted(int(line.rstrip('\n')) for line in fileBeforeSort)
> IronPython tripped on the 2nd line, it said:
> SyntaxError: unexpected token for at
This code uses generator expressions which IronPython doesn't support
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
> Also, when I tried to use Python to write what is basically a
> C# program:
> from System import Array, Convert
> from System.IO import File
> reader = File.OpenText("READ_ME_AND_SORT_ME.txt")
> beforeSort = reader.ReadToEnd().Split('\n')
> beforeSortInt = [Convert.ToInt32(number) for number in beforeSort]
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:
> <type 'list'>
> Traceback (most recent call last):
> at __main__.Initialize() in
> 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.
More information about the users-ironpython.com