Blog of Chat

March 22, 2008

Using IronPython for Dynamic Expressions.

Also see: The Exception Model

We recently had this question posted to our forums over at LVS :

Dear Forum Experts:

I am looking for very specialized solution:

I have various Items which I store into a table in a Relational DB.
I would like to do a custom calculation, specific for each item at it’s instance. Because the calculation is specific for the item, and items are soo many I wold like to store the calculation formula into a relational DB. The problem is to convert the string of formula into a real programming command and to actually perform the calculation. I do not want to use Excel or additional software in order to gain calculation speed e.g.

ItemID = 5001, ItemSize = “a - b”
ItemID = 5002, ItemSize = “a - 2*b”
ItemID = 5003, ItemSize = “a + b”

So, ItemSize is actually the formula expression that would calculate various instances of a and b variables… I have tryed this:

int a = 10;
int b = 5;

string formula = “a + b” // This comes from ItemSIze of DB,SQL, etc.

int Result = a + b; // This is a second line for test only - hard coded…

int CalcResult = int.Parse(formula); //I wish this was working…

MessageBox.Show(Result.ToString()); // This works…
MessageBox.Show(CalcResult.ToString()); // Never got that far.

The result will be stored in different DB with the instances of a and b.
Could you please post any information on how should I approach this problem.

Thanks a lot.

Several options immediately came to mind: code up a simple expression interpreter, evaluate the expression with dynamic SQL (yuck), use lightweight code gen. Then I remembered this little thing I saw at last years PDC called IronPython. Solving this problem with IronPython was “like butta”.

using System; using System.Collections.Generic; using System.Text; using IronPython.Hosting; namespace PythonDemo { class Program { delegate int MyExpressionDelegate(int a, int b); static void Main(string[] args) { PythonEngine pe = new PythonEngine(); MyExpressionDelegate expression = pe.CreateLambda<MyExpressionDelegate>("a + b"); int a = 10; int b = 5; int c = expression(a,b); Console.WriteLine(c); } } }
Live Person: Live Chat Solution for Online Customer Service on Website.

Also see: Parsing things you don’t understand

Also see: Java Concurrency, another series on its issues

Also see: Introducing Microsoft Tagspace

Also see: LearnExpression.com is live.

That’s all there was to it! The API for the PythonEngine was very intuitive. I could immediately see where and how I could integrate this with any number of applications that I’ve worked on in the past. Tip of the hat to the IronPython guys!

Now I haven’t tested this against a simple interpreter but I would imagine as long as you are smart and keep a cache of the expressions and don’t re-parse them every time that it would perform just as well as any interpreted solution if not better. Just follow the make it work, make it work right and make it work fast model and you’ll be ok.

I wonder if this would also be possible by referenceing the PowerShell runtime. I’ll have to take a look at that next and see how it compares.

P.S. Microsoft, if you’re listening, please include IronPython in the Orcas/NETFX3.5 release! :) I’d love to see IDE support for python scripts and such.


http://weblogs.asp.net/dfindley/archive/2006/11/02/Using-IronPython-for-Dynamic-Expressions_2E00_.aspx

Comments »

The URI to TrackBack this entry is: http://spacechat.blogsome.com/2008/03/22/using-ironpython-for-dynamic-expressions-2/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com