Speak and Shout

Tuesday, January 03, 2006

Jython experiments, pt 1

I had to parse a text file yesterday to count some array elements. I normally would fire up Python, but, for fun, I decided to give Jython a whirl.

I started out with baby steps.

def main():
   file = open("berlin.tsp")
   for x in file:
      print x

I started up Jython 2.1 at the command prompt and ran the script, but I got an AttributeError: __getitem__ on line 3. What the heck? I do this all the time in Python.

I studied the error again and played around with the code for a moment. Suddenly, I remembered that iterating over lines in a file was a Python 2.4 innovation. I had to think for a second ... how did I do it in Python 2.2? Oh yes, readlines().

OK, onto the next part, scanning for a particular substring in the file. Again, I like baby steps.

def main():
   file = open("berlin.tsp")
   for x in file:
      if 'NODE_COORD_SECTION' in x:
         print x
         break

Nothing fancy here, just seeing if things work. I switched back to the Jython prompt and ran it again. What?! Another error.
TypeError: string member test needs char left operand.

I had to consult Jython Essentials to confirm my suspicion: You can't test for substrings in a string with this syntax, only single characters. But I had done this in Python! Oh right ... in 2.4. Bloodied but unbowed, I replaced my existing syntax with a .startswith() method. OK, it worked. Press on.

I realized that once I hit the break statement in the code, I still needed to scan the file from that point. It was time to modify the code slightly.

def main():
   file = open("berlin.tsp")
   lines = file.readlines()
   for i,v in enumerate(lines):
      if v.startswith('NODE_COORD_SECTION'):
         print v
         break
      else:
         print "Error"
         return
   for j in range(i, len(lines)):
         print lines[j]

Oh, forget it. Even I know enumerate() is new for 2.4. Let's try again.
def main():
   file = open("berlin.tsp")
   lines = file.readlines()
   for i in range(len(lines)):
      if lines[i].startswith('NODE_COORD_SECTION'):
         print lines[i]
         break
      else:
         print "Error"
         return
   total = 0
   for j in range(i, len(lines)):
      lst = lines[j].strip().split() # parse the numbers
      total += sum(lst) # sum them up

I also added code at the bottom to sum the array elements. And what do you know ... sum() didn't work either.

At this point, I switched over to Python and finished the program as I was originally coding it.

I learned a couple things:
  1. I've assimilated new Python features more than I realized, and I've come to rely on them. I disagree with those that say that new versions of Python have been incorporating useless features.
  2. Jython hasn't caught up with the latest Python innovations and is suffering for it.

Still, I was keeping an open mind ... check out Jython experiments, part 2.

0 Comments:

Post a Comment



<$I18N$LinksToThisPost>:

Create a Link

<< Home