使用 IronRuby 或 IronPython 修改 C# 对象列表

发布于 2024-09-13 14:07:35 字数 444 浏览 11 评论 0原文

如果我有一个对象列表,例如。 List 其中 Foo 有几个属性,然后我可以创建一个或多个为每一行运行的ironruby 或ironpython 脚本吗?

这是一些伪代码:

var items = new List<Foo>();
foreach(var item in items) {
   var pythonfunc = getPythonFunc("edititem.py");
   item = pythonfunc(item);
}

我需要一种动态方式来修改代码存储在数据库或文件中的列表。

如果您认为有更好的方法来做到这一点,或者有替代方案,以便可以为客户端编写自定义例程,该例程可以从数据库中提取特定于客户端的数据(导出),请发表评论或留下建议。

谢谢

If I had a list of objects eg. List<Foo> where Foo has a couple of properties, could I then create one or more ironruby or ironpython scripts that run for each row.

Here is some pseudo code:

var items = new List<Foo>();
foreach(var item in items) {
   var pythonfunc = getPythonFunc("edititem.py");
   item = pythonfunc(item);
}

I need a dynamic way to modify the list where the code is stored in a db or file.

If you think there is a better way to do this, or have an alternative so that a custom routine can be written for a client that can extract the client-specific data out of a database (an export), please comment or leave a suggestion.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云巢 2024-09-20 14:07:35

我以前使用过这种方法,将 IronPython 脚本保存在数据库和文件中。我喜欢的模式是用约定的名称来存储 Python 函数。换句话说,如果您正在处理 Foo 类型的对象,则 .py 文件或表中可能有一个名为“foo_filter”的 Python 函数。最终,您可以执行 Python 文件并将函数解析为函数引用字典。

一个快速示例应用程序...

您的 foo 类:

public class Foo {
    public string Bar { get; set; }
}

设置 Foo 并调用 getPythonFunc(i);

var items = new List<Foo>() {
    new Foo() { Bar = "connecticut" },
    new Foo() { Bar = "new york" },
    new Foo() { Bar = "new jersey" }                    
};

items.ForEach((i) => { getPythonFunc(i); Console.WriteLine(i.Bar); });

一个快速而肮脏的 getPythonFun 实现... ScriptXXX 对象图显然应该被缓存,由 GetVariable() 检索的变量也应该被缓存。

static void getPythonFunc(Foo foo) {

    ScriptRuntimeSetup setup = ScriptRuntimeSetup.ReadConfiguration();
    ScriptRuntime runtime = new ScriptRuntime(setup);
    runtime.LoadAssembly(Assembly.GetExecutingAssembly());
    ScriptEngine engine = runtime.GetEngine("IronPython");
    ScriptScope scope = engine.CreateScope();

    engine.ExecuteFile("filter.py", scope);

    var filterFunc = scope.GetVariable("filter_item");
    scope.Engine.Operations.Invoke(filterFunc, foo);
}

filter.py 的内容:

def filter_item(item):
    item.Bar = item.Bar.title()

基于属性应用规则的简单方法(不是在 Foo 上添加 Size 属性):

var items = new List<Foo>() {
    new Foo() { Bar = "connecticut", Size = "Small" },
    new Foo() { Bar = "new york", Size = "Large" },
    new Foo() { Bar = "new jersey", Size = "Medium" }
};

更改 getPythonFun() 中调用 ScriptScope 的 GetVariable() 的行:

var filterFunc = scope.GetVariable("filter_" + foo.Size.ToLower());

以及 filter 的新内容。 py

def filter_small(item):
    item.Bar = item.Bar.lower()

def filter_medium(item):
    item.Bar = item.Bar.title()

def filter_large(item):
    item.Bar = item.Bar.upper()

我在 http://www.codevoyeur.com 上提供了一堆更完整的示例/Articles/Tags/ironpython.aspx

I've used this approach before, both saving IronPython scripts in a database and in files. The pattern I like is to store Python functions with names known by convention. In other words, if you are processing an object of type Foo, you might have a Python function named "foo_filter" in your .py file or table. Ultimately, you can execute a Python file and parse out the functions into a dictionary of function references.

A quick sample app...

Your foo class:

public class Foo {
    public string Bar { get; set; }
}

Setting up Foo and calling getPythonFunc(i);

var items = new List<Foo>() {
    new Foo() { Bar = "connecticut" },
    new Foo() { Bar = "new york" },
    new Foo() { Bar = "new jersey" }                    
};

items.ForEach((i) => { getPythonFunc(i); Console.WriteLine(i.Bar); });

A quick and dirty getPythonFun implementation... The ScriptXXX object graph should obviously be cached, as should the variables retrieved by GetVariable().

static void getPythonFunc(Foo foo) {

    ScriptRuntimeSetup setup = ScriptRuntimeSetup.ReadConfiguration();
    ScriptRuntime runtime = new ScriptRuntime(setup);
    runtime.LoadAssembly(Assembly.GetExecutingAssembly());
    ScriptEngine engine = runtime.GetEngine("IronPython");
    ScriptScope scope = engine.CreateScope();

    engine.ExecuteFile("filter.py", scope);

    var filterFunc = scope.GetVariable("filter_item");
    scope.Engine.Operations.Invoke(filterFunc, foo);
}

The contents of filter.py:

def filter_item(item):
    item.Bar = item.Bar.title()

A simple way to apply rules based on a property (not the addition of the Size property on Foo):

var items = new List<Foo>() {
    new Foo() { Bar = "connecticut", Size = "Small" },
    new Foo() { Bar = "new york", Size = "Large" },
    new Foo() { Bar = "new jersey", Size = "Medium" }
};

Change the line in getPythonFun() that calls ScriptScope's GetVariable():

var filterFunc = scope.GetVariable("filter_" + foo.Size.ToLower());

And the new contents of filter.py

def filter_small(item):
    item.Bar = item.Bar.lower()

def filter_medium(item):
    item.Bar = item.Bar.title()

def filter_large(item):
    item.Bar = item.Bar.upper()

I have a bunch of more complete samples available at http://www.codevoyeur.com/Articles/Tags/ironpython.aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文