473,378 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

Hello,

I have an idea, thus far half-baked, to create a language called
Ephemeral, for "flexible modeling & reasoning language".
I'd like some advice about how to go about implementing it.

Ephemeral would be Python plus additional constructs to support
decision analysis -- what's needed are essentially specifications
of probability and utility functions. Let's not worry about whether
such a language would be usefult to anyone.

The goal here is to merge the description of the probability and
utility stuff with Python in general. I would like for objects
of these new types to appear in ordinary Python functions so that
they can be manipulated. Here's an example:

# "dn" == "decision network", an Ephemeral construct;
# this part is parsed separately from the python code
dn my_example:
chance X:
cpd: gaussian [mean: 25, std_dev: 75]
chance Y:
cpd: gaussian [mean: 12, std_dev: 19]
chance Z = X + Y

# this part is ordinary python code; nodes, cpd, parents, and
# and posterior are constructed automatically from above description
def __main__:
print my_example.nodes # "[X, Y, Z]"
print my_example.X.cpd # "gaussian [mean: 25, std_dev: 75]"
print my_example.Z.parents # "[X, Y]"
print my_example.Z.posterior # "gaussian [mean: 37, std_dev: 77.4]"
One way to look at this is that it is ordinary Python code with
new kinds of objects embedded in it. Another approach would be to
embed Python functions in a decision network. I think that it will
generally be more powerful and flexible to embed the new stuff in
Python rather than vice versa. How can I start going about this?

Are there other projects that have a similar spirit --
extending Python with special purpose language constructs?

Thanks for any comments,
Robert Dodier
--
If I have not seen as far as others, it is because
giants were standing on my shoulders. -- Hal Abelson
Jul 18 '05 #1
7 1459
ro***********@yahoo.com (Robert Dodier) writes:
# "dn" == "decision network", an Ephemeral construct;
# this part is parsed separately from the python code
dn my_example:
chance X:
cpd: gaussian [mean: 25, std_dev: 75]
chance Y:
cpd: gaussian [mean: 12, std_dev: 19]
chance Z = X + Y


I don't see what good this does. You can do the same thing with
ordinary Python classes.
Jul 18 '05 #2
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

Jul 18 '05 #3
Jeff Epler <je****@unpythonic.net> writes:
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


When you say Z = X + Y, you've added two classes together. Can you
actually do that with metaclasses?
Jul 18 '05 #4
On Sun, 28 Dec 2003 16:40:57 -0800, Paul Rubin wrote:
Jeff Epler <je****@unpythonic.net> writes:
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


When you say Z = X + Y, you've added two classes together. Can you
actually do that with metaclasses?


Yeah, i did this with the comparison operators on classes:

http://groups.google.com.au/groups?h....au%26rnum%3D1

funkalitious.

Simon.

Jul 18 '05 #5
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
ro***********@yahoo.com (Robert Dodier) writes:
# "dn" == "decision network", an Ephemeral construct;
# this part is parsed separately from the python code
dn my_example:
chance X:
cpd: gaussian [mean: 25, std_dev: 75]
chance Y:
cpd: gaussian [mean: 12, std_dev: 19]
chance Z = X + Y


I don't see what good this does. You can do the same thing with
ordinary Python classes.


Thanks for your interest. I want to make it possible to experiment
with notations that don't correspond to Python syntax. For example,
to write "X ~ gaussian(mu,sigma2)", or to write "Y <- (X1, X2, X3)",
say.

The larger goal is to make the decision model description as
natural as possible for people that work on decision problems.
Development effort should focus on the decision model, which,
in interesting problems, may be much larger and more complex
than the ordinary Python glue code around it.

Languages such as BUGS, S, dot, and Netica express the sorts
of ideas that I want in Ephemeral.

Robert Dodier
--
``Strange women, lying in ponds, distributing swords,
is no basis for a system of government.'' -- Dennis (MP&THG)
Jul 18 '05 #6
Robert Dodier wrote:
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:

ro***********@yahoo.com (Robert Dodier) writes:
# "dn" == "decision network", an Ephemeral construct;
# this part is parsed separately from the python code
dn my_example:
chance X:
cpd: gaussian [mean: 25, std_dev: 75]
chance Y:
cpd: gaussian [mean: 12, std_dev: 19]
chance Z = X + Y


I don't see what good this does. You can do the same thing with
ordinary Python classes.

Thanks for your interest. I want to make it possible to experiment
with notations that don't correspond to Python syntax. For example,
to write "X ~ gaussian(mu,sigma2)", or to write "Y <- (X1, X2, X3)",
say.

The larger goal is to make the decision model description as
natural as possible for people that work on decision problems.
Development effort should focus on the decision model, which,
in interesting problems, may be much larger and more complex
than the ordinary Python glue code around it.


Personally, I would first write all of the code for performing
calculations in pure Python, using the usual OO notation. Then you can
always add a parser which translates the mathematical syntax into OO.

David

Jul 18 '05 #7
Jeff Epler <je****@unpythonic.net> wrote:
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: [...]
I'd rather not try to shoehorn the decision model description into
a description in terms of Python classes. I'd like to try to keep
the description as natural as possible.
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: [...]
I've implemented similar systems by loading files in one format
or another -- XML was the latest attempt; I won't be trying that
again -- and, to explore just how far the integration with Python
can be taken, I'm wondering what it would take to merge the model
description with Python. I'd like to have Python in the model
(to return probability & utility values) as well as model in the
Python.
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.
Yes, I've seen the PEP in question (PEP 269), I'll take a closer
look. I'll look at yapps and spark too.
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.


Well, that's not out of the question. Can you give a reference
to a starting point for hacking on the import system?

Thanks for your interest. I appreciate your help.

Robert Dodier
--
``Supreme executive power derives from a mandate from the masses,
not from some farcical aquatic ceremony.'' -- Dennis (MP&THG)
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Barry Olly | last post by:
Hi, I'm working on a mini content management system and need help with dealing with special characters. The input are taken from html form which are then stored into a varchar column in...
14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
1
by: quickcur | last post by:
I am rewrite some of my excel sheets to xml document. In my excel, there is a lot of special characters like "/", " " (space), "#", "+'. I am using a java program based on JDom to to create xml....
3
by: ATH0 | last post by:
How to search for special character { } and how to count them.. I got field called text ( undefined length ) and in this field you must define "{" as start and "}" as end of some text line. If...
4
by: dutch disCo | last post by:
Hi, I'm trying to use GET method to pass variables through my script files. Unfortunately, when a special char (&, ?, +) is being posted the variable gets trimmed in a very strange way. I'm...
7
by: Mindy Geac | last post by:
Is it possible to delete special caracters from a string and multiple spaces? When the input is : "a&*bbb cc/c d!d" I want the result : "abbb ccc dd" thnx. MJ <?php
2
by: Ozer | last post by:
Hi friends, I wanna ask something. I use javascript menu on my user interface. And the js files contains special(turkish) character. I need to use codepage=1254 attribute in my @Page tag. If i...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
17
by: Carl Mercier | last post by:
Hi, Is it possible to use special characters like \n or \t in a VB.NET string, just like in C#? My guess is NO, but maybe there's something I don't know. If it's not possible, does anybody...
3
by: infinity | last post by:
Hi all, Is constructor a special member function? But I don't think it is either a member function or even a special member function although it has the syntax of a function. I think it confused...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.