pyext - google

T

tim

Hi,

I'm playing around with the python external for PureData. (pyext)
I want to enable a patch to lookup something on the net.
I modified the "search.py" example from Dive Into Python and kind of
pasted it into the simple.py example from the pyext docs to be loaded in
a pd patch.
Loading the script in the patch works, and it receives messages and
sends messages back, so I think on the pd side I've got it set up right.
If I call my "srch" function (see script) I get this error:

Traceback (most recent call last):
File "c:/pd/scripts\\searchggl.py", line 86, in srch_1
res = searchgoogle(q)
File "c:/pd/scripts\\searchggl.py", line 101, in searchgoogle
results = _server.doGoogleSearch(
File "C:\\Python24\\lib\\site-packages\\SOAPpy\\Client.py", line 470,
in __call__
return self.__r_call(*args, **kw)
File "C:\\Python24\\lib\\site-packages\\SOAPpy\\Client.py", line 492,
in __r_call
self.__hd, self.__ma)
File "C:\\Python24\\lib\\site-packages\\SOAPpy\\Client.py", line 406,
in __call
raise p
SOAPpy.Types.faultType: <Fault SOAP-ENV:Client: No Deserializer found to
deserialize a 'http://www.w3.org/1999/XMLSchema:Symbol' using encoding
style 'http://schemas.xmlsoap.org/soap/encoding/'.>

attached are the original search.py script, and my modified pd-pyext
version.

Any clues appreciated!
Tim

"""Search Google from the command line

This program is part of "Dive Into Python", a free Python book for
experienced programmers. Visit http://diveintopython.org/ for the
latest version.
"""

__author__ = "Mark Pilgrim ([email protected])"
__version__ = "$Revision: 1.2 $"
__date__ = "$Date: 2004/05/20 18:53:59 $"
__copyright__ = "Copyright (c) 2004 Mark Pilgrim"
__license__ = "Python"

from SOAPpy import WSDL

# you'll need to configure these two values;
# see http://www.google.com/apis/
WSDLFILE = '/path/to/copy/of/GoogleSearch.wsdl'
APIKEY = 'YOUR_GOOGLE_API_KEY'

_server = WSDL.Proxy(WSDLFILE)
def search(q):
"""Search Google and return list of {title, link, description}"""
results = _server.doGoogleSearch(
APIKEY, q, 0, 10, False, "", False, "", "utf-8", "utf-8")
return [{"title": r.title.encode("utf-8"),
"link": r.URL.encode("utf-8"),
"description": r.snippet.encode("utf-8")}
for r in results.resultElements]

if __name__ == '__main__':
import sys
for r in search(sys.argv[1])[:5]:
print r['title']
print r['link']
print r['description']
print

# py/pyext - python script objects for PD and MaxMSP
#
# Copyright (c) 2002-2003 Thomas Grill ([email protected])
# For information on usage and redistribution, and for a DISCLAIMER OF ALL
# WARRANTIES, see the file, "license.txt," in this distribution.
#

"""This is an example script for the py/pyext object's basic functionality.

pyext Usage:
- Import pyext

- Inherit your class from pyext._class

- Specfiy the number of inlets and outlets:
Use the class members (variables) _inlets and _outlets
If not given they default to 1
You can also use class methods with the same names to return the respective number

- Constructors/Destructors
You can specify an __init__ constructor and/or an __del__ destructor.
The constructor will be called with the object's arguments

e.g. if your PD or MaxMSP object looks like
[pyext script class arg1 arg2 arg3]

then the __init__(self,args) function will be called with a tuple argument
args = (arg1,arg2,arg3)
With this syntax, you will have to give at least one argument.
By defining the constructor as __init__(self,*args) you can also initialize
the class without arguments.

- Methods called by pyext
The general format is 'tag_inlet(self,args)' resp. 'tag_inlet(self,*args)':
tag is the PD or MaxMSP message header.. either bang, float, list etc.
inlet is the inlet (starting from 1) from which messages are received.
args is a tuple which corresponds to the content of the message. args can be omitted.

The inlet index can be omitted. The method name then has the format 'tag_(self,inlet,args)'.
Here, the inlet index is a additional parameter to the method

You can also set up methods which react on any message. These have the special forms
_anything_inlet(self,args)
or
_anything_(self,inlet,args)

Please see below for examples.

Any return values are ignored - use _outlet (see below).

Generally, you should avoid method_, method_xx forms for your non-pyext class methods.
Identifiers (variables and functions) with leading underscores are reserved for pyext.

- Send messages to outlets:
Use the inherited _outlet method.
You can either use the form
self._outlet(outlet,arg1,arg2,arg3,arg4) ... where all args are atoms (no sequence types!)
or
self._outlet(outlet,arg) ... where arg is a sequence containing only atoms

- Use pyext functions and methods:
See the __doc__ strings of the pyext module and the pyext._class base class.

"""

try:
import pyext
except:
print "ERROR: This script must be loaded by the PD/Max pyext external"

#################################################################

class srchg(pyext._class):
"""Example of a simple class which receives messages and prints to the console"""

# number of inlets and outlets
_inlets=3
_outlets=1


# methods for first inlet
def srch_1(self,q):
#return ('found')
self._outlet(1,'found')
res = searchgoogle(q)
self._outlet(1,res)

#################################################################

def searchgoogle(q):
from SOAPpy import WSDL

# you'll need to configure these two values;
# see http://www.google.com/apis/
WSDLFILE = 'c:\\Python24\\GoogleSearch.wsdl'
APIKEY = 'your_key_here'

_server = WSDL.Proxy(WSDLFILE)
"""Search Google and return list of {title, link, description}"""
results = _server.doGoogleSearch(
APIKEY, q, 0, 10, False, "", False, "", "utf-8", "utf-8")
return [{"title": r.title.encode("utf-8"),
"link": r.URL.encode("utf-8"),
"description": r.snippet.encode("utf-8")}
for r in results.resultElements]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top