Florida Housing Market

May 29, 2008

Utilising IronPython for Active Expressions.

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

Earnest Forum Experts:

I am searching very specialised solution:

I have various Items which I store into a table in a Relational DB.
I would care 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 care to put in the calculation formula into a relational DB. The problem is to change the string of formula into a existent programming command and to really execute the calculation. I do not desire to utilise Excel or extra software in order to gather calculation speed e.g.

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

And then, ItemSize is in reality the formula expression that would work out assorted instances of a and b variables… I have examined this:

int a = 10;
int b = 5;

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

int Result = a + b; // This is a second line for test merely - firmly ridden…

int CalcResult = int.Parse(formula); //I care this was puzzling out…

MessageBox.Show(Result.ToString()); // This figures out…
MessageBox.Show(CalcResult.ToString()); // Ne’er conveyed that far.

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

Thanks a lot.

Several options instantly amounted to bear in mind: code up a elementary expression interpreter, pass judgment the expression with active SQL (yuck), utilise lightweight code gen. And then I remembered this little thing I saw at last years PDC bade IronPython. Figuring out this problem with IronPython was “like butta”.

using System; using System.Collections.Generic; using System.Text; using IronPython.Hosting; namespace PythonDemo { class Program { designate int MyExpressionDelegate(int a, int b); static void Independent(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); } } }

That’s all there was to it! The API for the PythonEngine was very visceral. I could straightaway envision where and how I could desegregate this with any number of applications that I’ve influenced on in the past. Tip of the hat to the IronPython guys!

At present 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. Simply trace the work it make, work it make correct and work it make quick model and you’ll be ok.

I question if this would too be potential by citing the PowerShell runtime. I’ll have to read a look at that next and run across how it equates.

P.S. Microsoft, if you’re taking heed, delight let in IronPython in the Orcas/NETFX3.5 release! :) I’five hundred love to run across IDE support for python scripts and such.

Relating Posts:
How Techie are CIOs?
Introduction To Mortgage Broker License
Brad Abrams’ pixel8 Interview Podcast posted
Residential Veridical Estate Appraiser Secrets - FHA Today Taking Line Applications For Appraisers
DevWeek 2008 Silverlight Precon Demos
A VS.NET Macro to Bring forth Machine Keys.

Comments

The URI to TrackBack this entry is: http://kerryzan.blogsome.com/2008/05/29/utilising-ironpython-for-active-expressions/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Sorry, the comment form is closed at this time.

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