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 = ,
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;
Python实例--C#执行Python脚本,传参的更多相关文章
- expect脚本远程登录、远程执行命令和脚本传参简单用法
expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...
- npm脚本传参问题
npm脚本传参问题(比如设置env参数) windows环境下: "build": "set NODE_ENV=dev&& gulp", &qu ...
- C# 服务里面调用Python.exe 来执行python文件
问题描述:在WCF服务里面通过调用python.exe来执行py文件,像下面这样py文件路径+参数,用空格隔开.会出现调用结果为空的现象 System.Diagnostics.ProcessStart ...
- python--脚本传参与shell脚本传参(位置参数)
写一个最简单的shell脚本,了解shell脚本是如何传参 1. vim test1.sh name=$1 age=$2 echo ${name} echo ${age} 2.调用脚本并传参 sh t ...
- shell 脚本传参
在 shell 中我们会见到 $0.$1.$2这样的符号,这是什么意思呢? 简单来说 $0 就是你写的shell脚本本身的名字,$1 是你给你写的shell脚本传的第一个参数,$2 是你给你写的sh ...
- python 执行文件时传参
## test.py ## ####################### import sys if __name__ == "__main__": args = sys.arg ...
- Python编程系列---使用装饰器传参+字典实现动态路由
# 实现一个空路由表,利用装饰器将url和功能函数的对应关系自动存到这个字典中 router_dict = {} # 定义一个装饰器 # 再给一层函数定义,用来传入一个参数,这个参数就是访问的页面地址 ...
- Python利用ctypes实现按引用传参
C的代码 void test_cref(char *a, int *b, char *data) { , sizeof(char)); strcpy(p, "cute"); a[] ...
- shell脚本传参执行spark-submit
读取多个目录下的本地文件,多个目录通过循环遍历的方式,以参数传递: #!/bin/bash i=0while [ $i -lt 10000 ] do echo "i=$i" spa ...
随机推荐
- Entityframework Code First 系列
总篇, 下面会添加每个小篇的链接. 目录如下: 项目搭建 ……
- CF 334 div.2-D Moodular Arithmetic
思路: 易知k = 0的时候答案是pp-1,k = 1的时候答案是pp. 当k >= 2的时候,f(0) = 0,对于 1 <= n <= p - 1,如果f(n)确定,由题意可知f ...
- Elasticsearch--建议器
目录 可用的建议器类型 term建议器 term建议器的配置选项 phrase建议器 completion建议器 在考虑性能的情况下,允许用户的拼写错误,以及构建一个自动完成功能 可用的建议器类型 t ...
- JavaScript(第二部分)
一.DOM获取元素节点的子节点 1.getElementsByTagName() 返回当前节点的指定标签名子节点 2.childNodes 表示当前节点的所有子节点 3.firstChild ...
- Js变量类型
值类型和引用类型 值类型(基本类型):5种,Number String Boolean null undefined var a=10; var b=a; a=2; console.log(b); a ...
- 启用adb wifi无线调试功能(无需root)
1 工具 电脑.手机 2 前提 电脑和手机出于同一网段 3 步骤 以管理员方式打开cmd,运行 adb tcpip 5555(执行tcpip调试模式) adb connect 192.168. ...
- 【Python-2.7】list类型
list是Python中的一种数据类型,也就是"列表".在Python中我们可以对list类型进行插入,删除,修改等操作. ##新建list类型 >>> ball ...
- x86汇编之十(使用字符串)
x86汇编之十(使用字符串) 转自网络,出处不详 一.传送字符串 Intel提供了完整的字符串传送指令,就像是MOV指令一样. 1.MOVS指令 1)movs指令格式 把字符串从一个位内存位置传送到另 ...
- HDU_1176_免费馅饼_16.4.23再做
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) M ...
- 搭建FileZilla
FileZilla是C/S架构的,有服务端和客户端 客户端下载地址https://www.filezilla.cn/download/client 安装,一般就下一步下一步了. 服务端下载:https ...