Skip to content

Instantly share code, notes, and snippets.

@shigehiro-yanbe
Last active October 4, 2018 09:40
Show Gist options
  • Save shigehiro-yanbe/b7e2a16743cc08c72e34 to your computer and use it in GitHub Desktop.
Save shigehiro-yanbe/b7e2a16743cc08c72e34 to your computer and use it in GitHub Desktop.
IronPythonを利用してC#からpythonを呼んでみるテスト
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Scripting.Hosting;
using SheetType = System.Collections.Generic.List<
System.Collections.Generic.Dictionary< string, object > >;
using RecordType = System.Collections.Generic.Dictionary< string, object >;
namespace PythonEmbedTest
{
class Program
{
static void Main(string[] args)
{
// スクリプトエンジンを作成
ScriptEngine engine = IronPython.Hosting.Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
// スクリプトをファイルから読み込む
const string filepath = @"c:\home\PythonEmbedTest\PythonEmbedTest\test.py";
ScriptSource source = engine.CreateScriptSourceFromFile(filepath);
// コンパイルする
CompiledCode compiled = source.Compile();
// 実行(関数定義)
compiled.Execute(scope);
// 関数を取り出す
var main = engine.Operations.GetMember< Func<SheetType,bool> >(scope, "main");
// 関数実行
var hoge = new SheetType();
hoge.Add( new RecordType(){ {"abc",100}, {"def", 3.14}, {"xyz", "piyo"} } );
hoge.Add( new RecordType(){ {"abc",200}, {"def", 2.45}, {"xyz", "abc" } } );
bool result = main(hoge);
// 結果を表示
Console.WriteLine(result);
// キー入力待ち
Console.ReadKey();
}
}
}
# -*- encoding:utf-8 -*-
def main(sheet):
print sheet.Count
for record in sheet:
checkRecord(record)
return True
def checkRecord(record):
print record["abc"], record["def"], record["xyz"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment