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 This directory also contains a program 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)')
>>> me.notify('f(a)')
>>> me.get_notification()
('f(a)', 0)
>>> me.notify('f(g(12, "hi"))')
>>> 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.
A method that disconnects the client.
A method that (re)connects the client.
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.
A method that unsubscribes to the subscription with the given ID.
A method that registers the supplied name with the Pedro server.
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.
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 .
This has the same behaviour as get_notification except that the notification is parsed into a Prolog term.
Parse string into a Prolog term.
A method that tests if a notification is ready to get.
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.