Has anyone succeeded in using the "yield" keyword inside jython scripts for Grinder 3?

I have this function:

def ngrams(word):
    for i in range(1, len(word) + 1):
        yield word[:i]


.. and I got an error

2012-03-09 19:37:31,928 ERROR worker-bootstrap: Error running worker process
net.grinder.scriptengine.jython.JythonScriptExecutionException: SyntaxError: ('invalid syntax', ('C:\\grinder-3.7.1\\lib
\\.\\nb-romg-file-store\\current\\grinder_test.py', 72, 15, '        yield word[:i]  '))
        (no code object) at line 0


Is there a way to get the job done yield

? I've tried the same function in the console jython

- if it works fine.


source to share

1 answer

In older versions of Jython, generators (functions using keyword yield

) are not available by default. You can try to enable the function by adding

from __future__ import generators


to the beginning of the source file. If that doesn't work, you are probably out of luck and generators are simply not available in this version of Jython. In this case, you can try to model the behavior using lists:

def ngrams(word):
    result = []
    for i in range(1, len(word) + 1):
    return result


It's a dead ugliness, but it should work even in the most ancient Python implementations.



All Articles