zoukankan      html  css  js  c++  java
  • Python实例--C#执行Python脚本,传参

    # -*- coding: utf-8 -*-
    # 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错)
    # 这个 Python 脚本, 用于被 C# 来调用.
    # 简单测试 Hello World 的效果.
    def welcome(name):
        return "hello " + name
    
    
    # 测试 参数为 C# 对象的效果. (获取/设置 C# 对象的属性)
    def testAddAge(obj):
        obj.Age = obj.Age + 1
        obj.Desc = obj.UserName + "又大了一岁 in Python."
    
    
    # 测试 参数为 C# 对象的效果. (调用 C# 对象的方法)
    def testAddAge2(obj):
        obj.AddAge(2)
    
    
    # 测试 List.
    def testList(lst):
        vResult = ""
        for each_item in lst:
            vResult = vResult + " " + each_item
        return vResult
    
    # 测试 Set.
    def testSet(pSet):
        vResult = ""
        for each_item in pSet:
            vResult = vResult + " " + each_item
        return vResult
    
    
    # 测试 Dictionary
    def testDictionary(pDictionary):
        vResult = ""
        for each_item in pDictionary:
            vResult = vResult + " " + each_item + "=" + pDictionary[each_item] + ";"
        return vResult
    
    
    
    
    
     
    using System; 
    
    using IronPython.Hosting;
    using Microsoft.Scripting.Hosting;
    
    namespace ConsoleApp1
    {
    
        /// <summary>
        /// 测试对象.
        /// 
        /// 用于传递数据给 Python 脚本
        /// </summary>
        public class TestDataObject
        {
            /// <summary>
            /// 用户名.
            /// </summary>
            public string UserName { set; get; }
            /// <summary>
            /// 年龄.
            /// </summary>
            public int Age { set; get; }
            /// <summary>
            /// 描述信息.
            /// </summary>
            public string Desc { set; get; }
            public void AddAge(int age)
            {
                this.Age = this.Age + age;
                this.Desc = String.Format("{0}又大了{1}岁 in C#", this.UserName, age);
            }
            public override string ToString()
            {
                return String.Format("姓名:{0}; 年龄:{1}; 描述:{2}", this.UserName, this.Age, this.Desc);
            }
    
        }
        public class RunPython
        {
            public void RunPythonTest()
            {
                // 加载外部 python 脚本文件.
                ScriptRuntime pyRumTime = Python.CreateRuntime();
                dynamic obj = pyRumTime.UseFile("TestPythonFile.py");
    
    
                // ==================================================
                // 简单调用脚本文件中的方法.
                Console.WriteLine(obj.welcome("Test C# Call Python."));
                Console.WriteLine(obj.welcome("测试中文看看是否正常!"));
    
    
    
                // ==================================================
                // 测试自定义对象.
                TestDataObject testObj = new TestDataObject()
                {
                    UserName = "张三",
                    Age = 20,
                    Desc = "",
                };
                Console.WriteLine("调用脚本前对象数据:{0}", testObj);
                obj.testAddAge(testObj);
                Console.WriteLine("调用 testAddAge 脚本后,对象数据={0}", testObj);
    
                obj.testAddAge2(testObj);
                Console.WriteLine("调用 testAddAge2 脚本后,对象数据={0}", testObj);
    
    
    
    
                // ==================================================
                // 测试 List.
                IronPython.Runtime.List testList = new IronPython.Runtime.List();
                testList.Add("List数据1");
                testList.Add("List数据2");
                testList.Add("List数据3");
                // 测试参数为 List.
                string result = obj.testList(testList);
                Console.WriteLine("调用 testList , 返回结果:{0}", result);
    
    
    
                // ==================================================
                // 测试 Set.
                IronPython.Runtime.SetCollection testSet = new IronPython.Runtime.SetCollection();
                testSet.add("Set数据1");
                testSet.add("Set数据2");
                testSet.add("Set数据3");
    
                // 测试参数为 Set.
                result = obj.testSet(testSet);
                Console.WriteLine("调用 testSet , 返回结果:{0}", result);
    
    
    
                // ==================================================
                // 测试 Dictionary.
                IronPython.Runtime.PythonDictionary testDictionary = new IronPython.Runtime.PythonDictionary();
                testDictionary["Key1"] = "Value1";
                testDictionary["Key2"] = "Value2";
                testDictionary["Key3"] = "Value3";
                // 测试参数为 Dictionary.
                result = obj.testDictionary(testDictionary);
                Console.WriteLine("调用 testDictionary , 返回结果:{0}", result);
    
                Console.ReadLine();
            }
        }
         
    }
    
    
    //-- 运行结果
    //hello Test C# Call Python.
    //hello 测试中文看看是否正常!
    //调用脚本前对象数据:姓名:张三; 年龄:20; 描述:
    //调用 testAddAge 脚本后,对象数据=姓名:张三; 年龄:21; 描述:张三又大了一岁 in Py
    //thon.
    //调用 testAddAge2 脚本后,对象数据= 姓名:张三; 年龄:23; 描述:张三又大了2岁 in C#
    //调用 testList , 返回结果: List数据1 List数据2 List数据3
    //调用 testSet , 返回结果: Set数据1 Set数据2 Set数据3
    //调用 testDictionary , 返回结果: Key3=Value3; Key2=Value2; Key1=Value1;
  • 相关阅读:
    说说我当初是如何学Linux的
    案例八:shell自动化管理账本脚本
    案例七:shell实现开机自动播放挂载本地yum仓库程序
    案例六:shell脚本监控httpd服务80端口状态
    案例五:shell脚本实现定时监控http服务的运行状态
    案例四:Shell脚本生成随机密码
    案例三:shell统计ip访问情况并分析访问日志
    案例二:shell脚本获取当前日期和时间及磁盘使情况
    案例一:shell脚本指定日期减去一天
    Linux:保证数据安全落盘
  • 原文地址:https://www.cnblogs.com/liuyongpingblog/p/8028903.html
Copyright © 2011-2022 走看看