1
myDict = {'itemkey1':'itemvalue1', 'itemkey2':'itemkey2','itemkey3':'itemvalue3','itemkey4':'itemvalue4'};

event = portTest.event(myDict);

I am getting the following error

Aborted run: Jython exception: TypeError:arg can't be coerced to java.util.List

myDict is the array of key values.

i hope i am passing the correct syntax in myDict for keyvalue[] datalist as it just got key value pair.

public Response event(KeyValue[] dataList)

I am calling the event function in java from jython script using grinder tool.

Azodious
  • 13,752
  • 1
  • 36
  • 71
srp
  • 521
  • 11
  • 22

2 Answers2

1

You'll need to turn your dictionary into a series of keys and values:

def chainDict(mapping):
    items = []
    for item in mapping.iteritems():
        items.extend(item)
    return items

event = portTest.event(chainDict(myDict))

This'll pass a list of [keyFoo, valueFoo keyBar, valueBar] to the event method, where keys and values are paired but in arbitrary order.

If you don't have an itertools module available, chain can be defined as:

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • net.grinder.scriptengine.jython.JythonScriptExecutionException: ImportError: no module named itertools – srp Sep 11 '12 at 09:29
  • Thank you for your reply but doesn't it import work in jython ? – srp Sep 11 '12 at 09:30
  • `from net.grinder.script.Grinder import grinder from net.grinder.script import Test from java.lang import System import time import random def chain(*iterables): for it in iterables: for element in it: yield element def userjourney(): time.sleep(8) myDict = {'itemkey1':'itemvalue1', 'itemkey2':'itemkey2','itemkey3':'itemvalue3','itemkey4':'itemvalue4'} grinder.logger.info("myDict '%s'" %myDict) event = portTest.Event(list(chain(*MyDict.iteritems()))) class TestRunner: def __call__(self): userjourney()` – srp Sep 11 '12 at 09:46
  • @Shashank: Sure, but does it work? If not, what is your error? – Martijn Pieters Sep 11 '12 at 09:48
  • net.grinder.scriptengine.jython.JythonScriptExecutionException: SyntaxError: ('invalid syntax') line no: where iam starting the function def chain(*iterables): and yield element (no code object) at line 0 this is the error iam getting – srp Sep 11 '12 at 09:52
  • @Shashank: Right, there is more syntax missing than I thought. Updated answer to fit. – Martijn Pieters Sep 11 '12 at 09:55
  • thank you, that has fixed that problem but i got a new error which says: ERROR SHASHANK-PC-0 thread-1 [ run-0 ]: Aborted run: Jython exception: NameError: MyDict [calling TestRunner] net.grinder.scriptengine.jython.JythonScriptExecutionException: NameError: MyDict File "C:\event.py", line 56, in userjourney File "C:\event.py", line 99, in __call__ – srp Sep 11 '12 at 10:03
  • @Shashank: Small typo on my part (`myDict` vs. `MyDict`), corrected. – Martijn Pieters Sep 11 '12 at 10:04
  • @Martijin, I have got a small doubt , does this still pass the list in arbitrary order because my values will be something like: key1 = 1 which corresponds to value1 = item1. if it is in arbitrary it would be like key 1 corresponds to value 2 which is another value for the that key. Could you please clarify me this. Thank you – srp Sep 11 '12 at 10:23
  • @Shashank: The key and value will be grouped, the order of the *groups* is arbitrary. So it could be `[key1, value1, key2, value2]` *or* it could be `[key2, value2, key1, value1]`. The key and value stay together. – Martijn Pieters Sep 11 '12 at 10:25
0

You need to explicitly pass myDict.items() instead of myDict for KeyValue pairs, myDict.keys() for the keys, or myDict.values() for err, the values...

Jon Clements
  • 138,671
  • 33
  • 247
  • 280