java <sTring, address> map to python dictionary conversion using py4j

351 views
Skip to first unread message

pchari...@gmail.com

unread,
Jan 10, 2019, 11:12:42 PM1/10/19
to Py4J Support and Comments
we have a requirement to convert a map in java to dictionary in python using py4j. Map is of <String,Address> were Address is a object with houseNo, city, country are properties within it.
I was able to fetch the map and print them, but Address is coming as java object, need to know how shall i parse the above java object and get the values from it in python.

for key, value in address.items():
print (getattr(value, "houseNo"))

returns
<py4j.java_gateway.JavaMember object at 0x000001CBB904C320>
<py4j.java_gateway.JavaMember object at 0x000001CBB904C3C8>

But i need the actual object parameter "houseNo" : 41


is it possible with py4j?

Jonah Graham

unread,
Jan 11, 2019, 3:56:43 AM1/11/19
to pchari...@gmail.com, Py4J Support and Comments
Yes this is possible, have a look at https://www.py4j.org/faq.html#how-to-access-a-field and come back with any further questions if that is not working for you.

Jonah

~~~
Jonah Graham
Kichwa Coders Ltd.
www.kichwacoders.com


--
You received this message because you are subscribed to the Google Groups "Py4J Support and Comments" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4j+uns...@py4j.org.
To post to this group, send email to py...@py4j.org.
To view this discussion on the web visit https://groups.google.com/a/py4j.org/d/msgid/py4j/6b95cdb2-d2d3-4ed4-b4d2-92839ac1c779%40py4j.org.

pchari...@gmail.com

unread,
Jan 11, 2019, 4:28:06 AM1/11/19
to Py4J Support and Comments

I am using the client server example that is being shared in the py4j site(i am new to this)

tried the below code to invoke gateway along with client server configuration:


from py4j.clientserver import ClientServer, JavaParameters, PythonParameters
from py4j.java_gateway import JavaGateway, GatewayParameters


class EclipseAction(object):
    """Simple implementation of an EclipseRunnable that opens
    a MessageDialog in the current thread. It must thus be
    called from a UI thread.
    """

    def __init__(self, clientserver):
        self.clientserver = clientserver

    def run(self, viewer):
       
        workspace_root = self.clientserver.jvm.org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot()
        project_names = [project.getName() for project in workspace_root.getProjects()]
        #print(project_names[0])
       
        addressDictionary = self.clientserver.jvm.xxx.accessAddressDetails(project_names[0])
        gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_field=True))
        for key, value in addressDictionary.items():
            print(value.city)
            print(value.toString())
        #self.clientserver.jvm.org.eclipse.jface.dialogs.MessageDialog.openInformation(viewer.getControl().getShell(), "Sample View", "HELLO FROM PYTHON")


    class Java:
        implements = ["org.py4j.plugin_example.EclipseRunnable"]


action = EclipseAction(None)

clientServer = ClientServer(JavaParameters(), PythonParameters(), action)

# Small hack so that action can call Java
action.clientserver = clientServer


But the value.city didnt return the actual field value. it returns the <py4j.java_gateway.JavaMember object at 0x0000022DA8DF95F8>

Also, have a query,

With the example plugin, Shall i access the code in static method of a java code using python on the initial run without invoking the Sample View Action button? Could you please elaborate on steps.

pchari...@gmail.com

unread,
Jan 13, 2019, 11:52:46 PM1/13/19
to Py4J Support and Comments

could anyone please assist on this.

Barthelemy Dagenais

unread,
Jan 19, 2019, 5:59:01 AM1/19/19
to pchari...@gmail.com, Py4J Support and Comments
Hi,

> But the value.city didnt return the actual field value. it returns the <py4j.java_gateway.JavaMember object at 0x0000022DA8DF95F8>

You are currently mixing a ClientServer and a JavaGateway. You should use only one (probably ClientServer in your case). You thus need:

clientServer = ClientServer(JavaParameters(auto_field=True), PythonParameters(), action)

> With the example plugin, Shall i access the code in static method of a java code using python on the initial run without invoking the Sample View Action button? Could you please elaborate on steps.

I don't understand this question. Can you ellaborate (your usage scenario, what you want to accomplish, which example plugin are you referring to?).

Bart

Reply all
Reply to author
Forward
0 new messages