欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

用ironpython驱动你的计算公式

发布时间:2025/6/17 python 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 用ironpython驱动你的计算公式 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
很多时候,很多应用都会使用到某些计算公式,而这些计算公式一段时间之后可能会调整。目前,一般处理的办法有硬编码、数据库表配置以及脚本。自ironpython发布之后,.net动态脚本的威力日增,以至于有DLR的诞生...
         以下是有关代码:(使用ironpython自带的示例文件first.py)          
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IronPython.Hosting;

namespace IronPythonIntegrationDemo
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"请输入阶乘数字");
            
long num = long.Parse(Console.ReadLine());
            PythonEngine engine 
= new PythonEngine();
            engine.Import(
"Site");
            CompiledCode cc 
= engine.CompileFile(@"D:\IronPython-1.1\Tutorial\first.py");
            cc.Execute();
            engine.Execute(
string.Format("total = factorial({0})", num));
            
long total = engine.EvaluateAs<long>("total");

            Console.WriteLine(total);
            Console.Read();
        }

    }

}

         这样,可以把动态脚本引擎集成进自己的程序里。当计算公式发生变化时,代码不需要修改,而只修改脚本即可。
         我觉得还是蛮好的,起码在程序的扩展性上。
         first.py文件内容如下:
#####################################################################################
#
#
  Copyright (c) Microsoft Corporation. All rights reserved.
#
#
  This source code is subject to terms and conditions of the Shared Source License
#
  for IronPython. A copy of the license can be found in the License.html file
#
  at the root of this distribution. If you can not locate the Shared Source License
#
  for IronPython, please send an email to ironpy@microsoft.com.
#
  By using this source code in any fashion, you are agreeing to be bound by
#
  the terms of the Shared Source License for IronPython.
#
#
  You must not remove this notice, or any other, from this software.
#
#
#####################################################################################

def add(a, b):
    
"add(a, b) -> returns a + b"
    
return a + b

def factorial(n):
    
"factorial(n) -> returns factorial of n"
    
if n <= 1return 1
    
return n * factorial(n-1)

hi 
= "Hello from IronPython!"

转载于:https://www.cnblogs.com/ofei/archive/2007/08/18/861122.html

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的用ironpython驱动你的计算公式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。