[IronPython] Why won't these snippets run?

Ray Djajadinata rayfd_2000 at yahoo.com
Sun Sep 18 18:56:02 CEST 2005


Hello,

I'm using Iron Python 0.9.1. I wonder why won't these
run?

<snippet>
fileBeforeSort = file("READ_ME_AND_SORT_ME.txt", "r")
afterSort = sorted(int(line.rstrip('\n')) for line in
fileBeforeSort)
fileBeforeSort.close()
</snippet>

IronPython tripped on the 2nd line, it said:

SyntaxError: unexpected token for at
ReadAndSortAndWriteInPython.py:2

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>

Also, if I use parentheses during conversion like
this:

beforeSortInt = (Convert.ToInt32(number) for number in
beforeSort)

IronPython will choke:
Traceback (most recent call last):
SyntaxError: unexpected token for at
ReadAndSortAndWriteInPython2.py:8

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?

Thanks a bunch,
Ray

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Ironpython-users mailing list