Using IronPython for Dynamic Expressions.
Also see: LINQ - The Uber FindControl
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”.
Also see: Eriskay: a Programming Language Based on Game Semantics
Also see: C# 3.0 Lambdas and Type Inference
Also see: Quaker votes
Also see: REST2SQL in a Jiffy, with Tagspace for Spice
Also see: Single source code base for Silverlight and WPF solutions
Also see: Updated Finalization and Hosting
Also see: On the Perils of Wikipedia
Also see: Snippet Compiler update
Also see: Generics and .NET
Also see: Quaker votes
Also see: Quaker votes
Also see: Tagspace: Social Bookmarking for the Whole Web…from Microsoft
Also see: Exception Handling in Running a Business
Also see: Snippet Compiler update
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); } } }
Also see: Tagspace, Meet Claimspace
Also see: Email Responsiveness
Also see: Alexbarn Leaves Microsoft…ARGH!
Also see: DevWeek 2008 Silverlight Precon Demos
Also see: Cool Silverlight Momentum Video Posted
Also see: C# 3.0 Lambdas and Type Inference
Also see: Programmers At Work
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
