Special-purpose extension of Python -- new kinds of objects

Jeff Epler jepler at unpythonic.net
Sun Dec 28 18:09:32 EST 2003


There have been lots of discussions on how to add domain-specific
extensions to Python.  You would do well to read these threads somewhere
like google groups, they're relevant to the topic at hand.

In your case, you could re-cast the "decision network" in terms of class
definitions, and get pretty similar behavior if those classes have the
right behavior in metaclasses:
    class my_example(DN):
        class X(Chance):
            cpd = gaussian(mean=25, std_dev=75)
        class Y(Chance):
            cpd = gaussian(mean=12, std_dev=19)
        Z = X + Y
using the class statement, you can create a namespace with some values
in it, and then do some processing of those values (for instance, to
precompute the .nodes values).  If you're very fond of your syntax, then
consider using strings as the easiest way.  Docstring abuse has been
seen before, and would look something like this:
    class my_example(DN): """
     chance X:
       cpd: gaussian [mean: 25, std_dev: 75]
     chance Y:
       cpd: gaussian [mean: 12, std_dev: 19]
     chance Z = X + Y
    """
either using docstrings or an external file, you'll need a parser.
There is a languishing PEP about exposing the Python parser generator to
Python code, so that you could write your own grammar with the same
level of power as Python's grammar.  There are lots of other package,
such as yapps and spark which are also suitable for writing parsers.

Once you have a parser and use external files, it's possible to extend
Python's import system to load files of your type automatically with
the import statement.  This is an advanced topic, though.

Jeff





More information about the Python-list mailing list