Thread: Jython Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    13
    Rep Power
    0

    Jython Help


    I am getting an error: SyntaxError: invalid syntax
    when I try to run this code with Jython. I can run the function in another .py file (no Jython) and it works great.
    I am running it from a command prompt: jython myFile.py

    I am still pretty new to Python/Jython and I am not sure why this happening, maybe a conflict of Java and Python APIs.

    Any ideas?

    Code:
    import time
    from time import strftime
    
    global minutesList
    minutesList = []
    
    def todaysMinutes():
        year, month, day = time.localtime()[0:3]
        for hour in xrange(0, 24):
            for minute in xrange(0, 60):
                t = (year, month, day, hour, minute, 0, 0, 0, 0)                
                yield time.strftime("%a %b %d %H:%M", t)
            
    minutesList = list(todaysMinutes())
    Thanks!
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Well, theres nothing that really jumps out as an error at first glance. It might be helpful if you could post the full error message (including the line number).

    I can't really test this with Jython right now since I don't have it set up, but, if we can't figure it out I'll install it and give it a go.

    Oh, you might want to choose either import time or from time import *, since there's not much point in importing the time module and then importing the strftime() function after - and then not using it . Also, you don't need to declare minutesList as global. So, after a few changes you'll end up with something like:

    Code:
    from time import localtime, strftime
    
    def todaysMinutes():
        year, month, day = localtime()[:3]
        for hour in xrange(24):
            for minute in xrange(60):
                time = (year, month, day, hour, minute, 0, 0, 0, 0)                
                yield strftime("%a %b %d %H:%M", time)
    
    minutesList = [stringTime for stringTime in todaysMinutes()]   
    
    print minutesList
    Note: I used a list comprehension here simply out of preferance, but you can still use list() if need's be in it's place.

    I hope this helps ,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    13
    Rep Power
    0
    Thanks for taking the time to review my problem. After some research and talking with a few people, it turns out the "yield"
    is not valid in Jython 2.1, so you get this error:

    Code:
    Traceback (innermost last):
      (no code object) at line 0
      File "a.py", line 8
                        yield strftime("%a %b %d %H:%M", time)
                              ^
    SyntaxError: invalid syntax
    Can anyone else think of a simple way to get all the minutes of the day in list that doesnt use yield? I will need to make this work with Jython, but even something basic in Python would be great.

    Thanks!
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ah, I'd totaly forgetten that Jython was that far behind Python right now. Anyway, this should do what you want:

    Code:
    from time import localtime, strftime
    
    def todaysMinutes():
        times = []
        year, month, day = localtime()[:3]
        for hour in xrange(24):
            for minute in xrange(60):
                time = (year, month, day, hour, minute, 0, 0, 0, 0)                
                times.append(strftime("%a %b %d %H:%M", time))
        return times
    
    minutesList = todaysMinutes()  
    
    print minutesList
    Enjoy ,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo