Passig a dictionary from python to php

Passig a dictionary from python to php

Postby fbartolom » Tue Jan 28, 2014 9:32 am

I need to use python to query a data repository to later export the result to php in order to make it available on the web. To do it I use:

Code: Select all
print json.dumps(res)

where res is the output array. Unfortunately when I execute it I receive error:

Code: Select all
python palina.py 70032
Traceback (most recent call last):
File "palina.py", line 17, in <module>
print json.dumps(res)
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 263, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 177, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <DateTime '20140128T10:00:00' at 7f82ee9a5f80> is not JSON serializable

What other way of exporting the array exists without the need to serialize it? Or else, does exist some better serialization tool not producing this error?

Thanks,
Last edited by stranac on Tue Jan 28, 2014 12:46 pm, edited 1 time in total.
Reason: Added code tags. Locked.
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Passig a dictionary from python to php

Postby stranac » Tue Jan 28, 2014 12:58 pm

What is DateTime?
If you want to serialize stuff not supported by the default encoder, you will have to create a subclass of json.JSONEncoder, and use that to do the encoding.

However, there is no json standard for serializing a datetime object, so you will have to learn what php expects to get.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1114
Joined: Thu Feb 07, 2013 3:42 pm

Re: Passig a dictionary from python to php

Postby fbartolom » Tue Jan 28, 2014 1:49 pm

The point is that I have no idea about what comes from the remote server I interview with the python command, but I should export it as a dictionary to analyze it in a php script I am more familiar with. What is the most robust way to do it, given serialize has those strong limitations?
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Passig a dictionary from python to php

Postby stranac » Tue Jan 28, 2014 2:36 pm

There is no way to pass "anything at all" from a python script to a php script.
You have to know what kind of data you're dealing with, and then send that data in a way that the php script can understand.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1114
Joined: Thu Feb 07, 2013 3:42 pm

Re: Passig a dictionary from python to php

Postby fbartolom » Tue Jan 28, 2014 4:13 pm

What I feared, so I need to learn how to use array and dictionaries even in Python, and I think it makes 10...!
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Passig a dictionary from python to php

Postby ochichinyezaboombwa » Tue Jan 28, 2014 10:32 pm

if DateTime is your only problem (btw: are you SURE you need DateTime and not datetime?), you can convert it say to (y,m,d, hh, mm, ss) tuple, and on the php side, re-construct it, or just use it, or whatever.
What do you mean by "makes 10"?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Passig a dictionary from python to php

Postby fbartolom » Wed Jan 29, 2014 12:45 pm

As a matter of fact I am trying to access the RPC server by php, what would solve a couple of problems. Only I do not know how to translate the python code in phpRPC: the format of the latter is in fact of the kind:
Code: Select all
 $s1 = new rpc_Client("muovi.roma.it", "/ws/xml/autenticazione/1", $port);
 $rpc_encoder = new rpc_Encoder("?");
 $rpc_encoder->add_param("token", $DEV);
 $message = $rpc_encoder->return_call();
 $result = $s1->send($message);


while the python one is much simpler:

Code: Select all
from xmlrpclib import Server
from pprint import pprint
import sys, json
palina=sys.argv[1]
DEV_KEY = 'UuC378Q3l3Y4vtF8q2Hj3i5Up6OmZKb2'
s1 = Server('http://muovi.roma.it/ws/xml/autenticazione/1')
s2 = Server('http://muovi.roma.it/ws/xml/paline/7')
token = s1.autenticazione.Accedi(DEV_KEY, '')
res = s2.paline.Previsioni(token, palina, 'it')

In particular I have no cue about to express those "autenticazione.Accedi" and "paline.Previsioni" and conversely what to put as parameter for rpc_Encoder().

Any cue about how to do it?

Thanks, Fabrizio
Last edited by stranac on Wed Jan 29, 2014 2:01 pm, edited 1 time in total.
Reason: Added code tags.
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am

Re: Passig a dictionary from python to php

Postby stranac » Wed Jan 29, 2014 2:02 pm

Sounds like a question to ask in a php place...
Also, please read this: viewtopic.php?t=145
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1114
Joined: Thu Feb 07, 2013 3:42 pm

Re: Passig a dictionary from python to php

Postby fbartolom » Wed Jan 29, 2014 6:13 pm

Thanks. It is actually halfway as I try to replicate a python functionality on php...
fbartolom
 
Posts: 15
Joined: Tue Jan 28, 2014 9:30 am


Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest