pyDATR

SourceForge.net Logo

1. What is it?

pyDATR is an implementation of the DATR language, written in Python. More about DATR can be found out at the DATR homepage. More about Python can be found at the Python homepage.

pyDATR is mainly supposed to be used as a Python library although a crude console interface is included for test purposes. The core language works and the standard library is mostly implemented. Also the implementation sports a transparent persistence mechanism.

2. How to get it?

The source code files can be downloaded from the project's SVN repository at Sourceforge. A distutils package will be supplied soon.

3. How to install it?

See the instructions in the README.TXT file.

4. How to use it?

Mainly pyDATR is supposed to be used as a library for Python programs. You can find some code samples in the test/testDATR.py and DATRinteract.py files. 

Basically you feed a DATR theory to the DATR parser like this:


from DATR import DATRparser
s = "Nodename: <def1> == atom.\n" p = DATRparser.get_parser("theoryname", s)
t = p.parse(s)

t now refers to a DATRcore.Theory object that allows you to query the theory:

t.query("Nodename", ("def1",))

The result of this is a tuple containing the elements of the query result. In this case this would be ("atom",). The query method takes two parameters: the name of the node where the evaluation of the query is supposed to start and the path to evaluate. The path is given as a tuple containing one string element for every element of the query path. So the above is equivalent to the DATR query:

Nodename:<def1>

And the result is equivalent to this DATR extensional statement:

Nodename:<def1> = atom.

If you have downloaded a .dtr file from the DATR homepage or written one of your own, you need to concatenate all lines into one long string as the PLY parser used for pyDATR needs it's input as a single string.

If you have configured the persistence mechanism, you can call the save() method of your theory object. This will write a pickled version of the theory object itself into the database. For the nodes separate records get created.

In order to load the theory from the database, call the load() classmethod of the DATRcore.Theory class. This will give you a Theory object with transparent access to the nodes in the database. Only nodes that actually are requested will get loaded into memory.

4.1 Using functions from the standard library

If you want to use a function from the standard library follow the format described on the DATR homepage.  Mainly you declare your use of such a function with the "uses" directive in your DATR program. Then you use it like a node of it's own.

If you wish to add more native Python functions to the standard library you can derive a new class from DATRStdlib.Stdlib:

from DATR import DATRStdlib

class Extlib(DATRStdlib.Stdlib):
    def __init__(self):
        super(Extlib, self).__init__()

Your new function will be called with the DATRcore.Query object to evaluate. It is expected to return a (possibly empty) tuple of DATRcore.Atom objects:

    def Myfunc(query):
        pathtpl = query.current_path
        # Do whatever will produce the desired result
        return result
 

To use the function in your theory you would write something like this:

# uses Myfunc

Node:
    <demo> == Myfunc:<>

Copyright (c) 2005, 2006 by Henrik Weber

Valid HTML 4.01 Transitional