Next:, Previous:C API, Up:APIs


12.2 Python API

The directory src/python_api contains a definition for a Pedro client class in the file pedroclient.py. This directory also contains a program pedro_gui.py that uses this API and provides an interface to subscriptions and notifications.

Below is a simple example using this class withing the python interpreter.

>>> from pedroclient import *
>>> me = PedroClient()
>>> me.subscribe('f(X)')
1
>>> me.notify('f(a)')
1
>>> me.get_notification()
('f(a)', 0)
>>> me.notify('f(g(12, "hi"))')
1
>>> print me.get_term()[0]
f(g(12, "hi"))
>>> 
PedroClient(machine='localhost', port=4550, async = True)
Create a Pedro client object connected to the Pedro server on hostname using the port port. If async is true a thread is created to process Pedro messages. If not, a call to notification_ready will process any Pedro messages. If the main program has its own event loop (e.g. in pygame) then the program runs faster setting async to False and adding notification_ready to the loop.
disconnect()
A method that disconnects the client.
connect()
A method that (re)connects the client.
notify(term)
A method that sends term as a notification to the Pedro server.
subscribe(term, goal = 'true', rock = 0)
A method that subscribes with the supplied term, goal and rock. This method, if successful, returns the ID of the subscription.
unsubscribe(ID)
A method that unsubscribes to the subscription with the given ID.
register(name)
A method that registers the supplied name with the Pedro server.
deregister()
A method that deregisters its name.
p2p(toaddr, term)
A method that sends term to the address toaddr using the peer-to-peer support. The machine part of the address can be elided if it is to a process on the same machine. Do not use localhost as the name of the machine.
get_notification()
A method that returns the next notification received by the client as a pair consiting of the notification string together with the rock supplied in the corresponding subscription. This method blocks until a message is available if async == True and returns None if async == false .
get_term()
This has the same behaviour as get_notification except that the notification is parsed into a Prolog term.
parse_string(string)
Parse string into a Prolog term.
notification_ready()
A method that tests if a notification is ready to get.
PedroParser()
Create a Prolog parser object. The only method, parse(string), parses string to a Prolog term. If a parse error occurs, a ParserError exception is thrown that contains the position of the error.