private void button2_Click(object sender, EventArgs e) { ScriptRuntime pyRuntime = Python.CreateRuntime(); dynamic obj = pyRuntime.UseFile(@"recommend.py"); IronPython.Runtime.List mustlst = new IronPython.Runtime.List(); IronPython.Runtime.List wantlst = new IronPython.Runtime.List(); foreach (int i in index_must) { mustlst.Add(i); } foreach (int i in index_want) { wantlst.Add(i); } string result = obj.getPeople(mustlst, wantlst); purl = obj.getUrl(mustlst, wantlst); pname = result; MessageBox.Show("为你推荐:" + pname); People infofr = new People(pname, purl); infofr.Show(); }
private void button2_Click(object sender, EventArgs e) { ScriptRuntime pyRuntime = Python.CreateRuntime(); dynamic obj = pyRuntime.UseFile(@"cities.py"); IronPython.Runtime.List hadlst = new IronPython.Runtime.List(); foreach (int i in index_had) { hadlst.Add(i); } string result = obj.getPeople(hadlst); iurl = obj.getUrl(hadlst); iname = result; MessageBox.Show("为你推荐:" + iname); People infofr = new People(iname, iurl); infofr.Show(); }
public static IronPython.Runtime.List AsPyList(this IList source) { var pyList = new IronPython.Runtime.List(); Type someObjectType = source.GetType(); foreach (var item in source) { object newValue = null; try { if ((item.GetType().GetInterfaces().Contains(typeof(System.Collections.IList)))) { MethodInfo method = typeof(ObjectExtensions).GetMethod("AsPyList"); MethodInfo generic = method.MakeGenericMethod(someObjectType.GenericTypeArguments[0]); newValue = generic.Invoke(null, new object[] { item }); } else if (item.GetType().Namespace == typeof(ObjectExtensions).Namespace) { MethodInfo method = typeof(ObjectExtensions).GetMethod("AsPyDictionary"); newValue = method.Invoke(null, new object[] { item }); } else { newValue = item; } pyList.Add(newValue); } catch (Exception ex) { Console.WriteLine("Unable to add:" + newValue.ToString()); Console.WriteLine(ex.Message); } } return(pyList); }
static void Main(string[] args) { // 加载外部 python 脚本文件. ScriptRuntime pyRumTime = Python.CreateRuntime(); dynamic obj = pyRumTime.UseFile("hello.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); // 测试调用 MD5 MD5Test.Test(); MD5Test.Test2(); // 测试调用 GetSysPath GetSysPathTest.Test(); GetSysPathTest.Test2(); UmengTest.Test(); Console.ReadLine(); }
public IronPython.Runtime.List this[IronPython.Runtime.Slice xslice, IronPython.Runtime.Slice yslice] { get { if ((xslice.start == null) || (xslice.stop == null) || (yslice.start == null) || (yslice.stop == null)) { throw new ILArgumentException("mgrid: start and stop of slices must be provided"); } double xStep = 1, yStep = 1, xStart = 0, yStart = 0, xStop = 1, yStop = 1; // if (xslice.start is int) { xStart = (double)(int)(xslice.start); } if (xslice.start is double) { xStart = (double)(xslice.start); } if (xslice.stop is int) { xStop = (double)(int)(xslice.stop); } if (xslice.stop is double) { xStop = (double)(xslice.stop); } // if (yslice.start is int) { yStart = (double)(int)(yslice.start); } if (yslice.start is double) { yStart = (double)(yslice.start); } if (yslice.stop is int) { yStop = (double)(int)(yslice.stop); } if (yslice.stop is double) { yStop = (double)(yslice.stop); } // if (xslice.step == null) { if (xStop >= xStart) { xStep = 1; } else { xStep = -1; } } else { if (xslice.step is int) { xStep = (double)(int)(xslice.step); } if (xslice.step is double) { xStep = (double)(xslice.step); } } if (yslice.step == null) { if (yStop >= yStart) { yStep = 1; } else { yStep = -1; } } else { if (yslice.step is int) { yStep = (double)(int)(yslice.step); } if (yslice.step is double) { yStep = (double)(yslice.step); } } // IronPython.Runtime.List list = new IronPython.Runtime.List(); int nx = (int)Math.Floor((xStop - xStart) / xStep) + 1; int ny = (int)Math.Floor((yStop - yStart) / yStep) + 1; ILArray <double> x = ILMath.counter(xStart, xStep, nx); ILArray <double> y = ILMath.counter(yStart, yStep, ny); List <ILArray <double> > meshgrid = ILMath.meshgrid(x, y); list.Add(meshgrid[0]); list.Add(meshgrid[1]); return(list); } }
public static IronPython.Runtime.List ToPythonList <T>(this IEnumerable <T> collection) { IronPython.Runtime.List result = new IronPython.Runtime.List(); collection.Select(x => { result.Add(x); return(x); }).ToList(); return(result); }
public static IronPython.Runtime.List ToPythonList(LinkedList<object> nativeList) { IronPython.Runtime.List list = new IronPython.Runtime.List(); foreach (object obj in nativeList) { if (obj is Dictionary<string, object>) { list.Add(ToPythonDict(obj as Dictionary<string, object>)); } else if (obj is LinkedList<object>) { list.Add(ToPythonList(obj as LinkedList<object>)); } else { list.Add(obj); } } return list; }
public IronPython.Runtime.List this[IronPython.Runtime.Slice xslice, IronPython.Runtime.Slice yslice] { get { if ((xslice.start == null) || (xslice.stop == null) || (yslice.start == null) || (yslice.stop == null)) { throw new ILArgumentException("mgrid: start and stop of slices must be provided"); } double xStep = 1, yStep = 1, xStart = 0, yStart = 0, xStop = 1, yStop = 1; // if (xslice.start is int) xStart = (double)(int)(xslice.start); if (xslice.start is double) xStart = (double)(xslice.start); if (xslice.stop is int) xStop = (double)(int)(xslice.stop); if (xslice.stop is double) xStop = (double)(xslice.stop); // if (yslice.start is int) yStart = (double)(int)(yslice.start); if (yslice.start is double) yStart = (double)(yslice.start); if (yslice.stop is int) yStop = (double)(int)(yslice.stop); if (yslice.stop is double) yStop = (double)(yslice.stop); // if (xslice.step == null) { if (xStop >= xStart) xStep = 1; else xStep = -1; } else { if (xslice.step is int) xStep = (double)(int)(xslice.step); if (xslice.step is double) xStep = (double)(xslice.step); } if (yslice.step == null) { if (yStop >= yStart) yStep = 1; else yStep = -1; } else { if (yslice.step is int) yStep = (double)(int)(yslice.step); if (yslice.step is double) yStep = (double)(yslice.step); } // IronPython.Runtime.List list = new IronPython.Runtime.List(); int nx = (int)Math.Floor((xStop - xStart) / xStep) + 1; int ny = (int)Math.Floor((yStop - yStart) / yStep) + 1; ILArray<double> x = ILMath.counter(xStart, xStep, nx); ILArray<double> y = ILMath.counter(yStart, yStep, ny); List<ILArray<double>> meshgrid = ILMath.meshgrid(x, y); list.Add(meshgrid[0]); list.Add(meshgrid[1]); return list; } }
public static void PythonStructure(int newID, List <DesignerItem> triggerList, List <DesignerItem> conditionList, List <DesignerItem> actionList, List <DesignerItem> targetList, Dictionary <int, string> andOrder, Dictionary <int, string> orOrder) { var ipy = Python.CreateRuntime(); dynamic test = ipy.UseFile("Code\\monster_ai_data.py"); IronPython.Runtime.PythonDictionary data = test.data; IronPython.Runtime.PythonDictionary unit = new IronPython.Runtime.PythonDictionary(); /////trigger///// IronPython.Runtime.List Trigger = new IronPython.Runtime.List(); Trigger.Add(triggerList[2].IDName); for (int i = 0; i < triggerList[2].outputValue.Count; ++i) { MyTextBox tempTextBox = triggerList[2].outputValue[i]; //if (tempTextBox.type == "Range" || tempTextBox.type == "Vec3") //{ // List<int> paramList = parseString(tempTextBox.propertyValue); // IronPython.Runtime.PythonTuple paramTuple = new IronPython.Runtime.PythonTuple(paramList); // Trigger.Add(paramTuple); //} //else { Trigger.Add(tempTextBox.propertyValue); } Trigger.Add(tempTextBox.propertyValue); } unit["trigger"] = Trigger; /////trigger///// ////AIParam//// MyTextBox idTextBox = triggerList[1].outputValue[0] as MyTextBox; int aiID = Convert.ToInt32(idTextBox.propertyValue); Monster_Data(triggerList[0], aiID); for (int i = 1; i < triggerList[1].outputValue.Count; ++i) { MyTextBox tempTextBox = triggerList[1].outputValue[i]; string convertString = triggerList[1].outputValue[i].propertyValue; //////////////////////////handle temporarily/////////////////////////////// if (Regex.IsMatch(convertString, @"^\d+$") || Regex.IsMatch(convertString, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$")) { unit[tempTextBox.Property] = convertString; } } ////AIParam//// ////condition//// for (int i = 0; i < conditionList.Count; ++i) { unit["condition" + i] = conditionList[i].IDName; IronPython.Runtime.List conditionParam = new IronPython.Runtime.List(); for (int j = 0; j < conditionList[i].outputValue.Count; ++j) { MyTextBox tempTextBox = conditionList[i].outputValue[j]; conditionParam.Add(tempTextBox.propertyValue); } if (conditionParam.Count > 0) { unit["condition" + i + "Args"] = conditionParam; } ComboBox tempCombo = conditionList[i].myCombo; string selectedIndex = null; if (tempCombo.SelectedItem != null) { selectedIndex = ((ComboData)tempCombo.SelectedItem).Value; } if (selectedIndex != null) { unit["condition" + i + "Tgt0"] = selectedIndex; } } ////condition//// ////action//// for (int i = 0; i < actionList.Count; ++i) { unit["action" + i] = actionList[i].IDName; IronPython.Runtime.List actionParam = new IronPython.Runtime.List(); for (int j = 0; j < actionList[i].outputValue.Count; ++j) { MyTextBox tempTextBox = actionList[i].outputValue[j]; actionParam.Add(tempTextBox.propertyValue); } if (actionParam.Count > 0) { unit["action" + i + "Args"] = actionParam; } ComboBox tempCombo = actionList[i].myCombo; string selectedIndex = null; if (tempCombo.SelectedItem != null) { selectedIndex = ((ComboData)tempCombo.SelectedItem).Value; } if (selectedIndex != null) { unit["action" + i + "Tgt0"] = selectedIndex; } } ////action//// ////exp//// if (andOrder.Count > 0 && andOrder.Count <= 1 && orOrder.Count == 0) { unit["conditionExp"] = andOrder[1] == "True" ? true : false; } else { IronPython.Runtime.List orList = new IronPython.Runtime.List(); var dicSort = from objDic in orOrder orderby objDic.Key ascending select objDic; foreach (KeyValuePair <int, string> kvp in dicSort) { bool judge = kvp.Value == "True" ? true : false; orList.Add(judge); } IronPython.Runtime.List andList = new IronPython.Runtime.List(); int andLength = andOrder.Count; var newdicSort = from objDic in andOrder orderby objDic.Key ascending select objDic; int lastValue = 0; foreach (KeyValuePair <int, string> kvp in newdicSort) { if ((kvp.Key - lastValue) > 1) { andList.Add(orList); bool judge = kvp.Value == "True" ? true : false; andList.Add(judge); lastValue = kvp.Key; } else { bool judge = kvp.Value == "True" ? true : false; andList.Add(judge); lastValue = kvp.Key; } } if (andList.Count == andLength && orList.Count > 0) { andList.Add(orList); } IronPython.Runtime.PythonTuple expTuple = new IronPython.Runtime.PythonTuple(andList); unit["conditionExp"] = expTuple; } ////exp//// ////target//// if (targetList != null) { for (int i = 0; i < targetList.Count; ++i) { unit["tgtMethodName" + i] = targetList[i].IDName; IronPython.Runtime.List targetParam = new IronPython.Runtime.List(); for (int j = 0; j < targetList[i].outputValue.Count; ++j) { MyTextBox tempTextBox = targetList[i].outputValue[j]; targetParam.Add(tempTextBox.propertyValue); } if (targetParam.Count > 0) { unit["tgtMethodArgs" + i] = targetParam; } } } ////target//// data[newID] = unit; exportCode(data, "monster_ai_data.py"); }
public static string RunIronPython(string directory, string str_script_url, List<string> args) { GenUtils.LogMsg("status", "Utils.run_ironpython: " + str_script_url, args[0] + "," + args[1] + "," + args[2]); //var app_domain_name = "ironpython"; string result = ""; try { /* string domain_id = app_domain_name; var setup = new AppDomainSetup(); setup.ApplicationName = app_domain_name; setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; var python_domain = AppDomain.CreateDomain(domain_id, securityInfo: null, info: setup); */ var options = new Dictionary<string, object>(); options["LightweightScopes"] = true; var python = Python.CreateEngine(options); var paths = new List<string>(); paths.Add(directory + "Lib"); // standard python lib paths.Add(directory + "Lib\\site-packages"); paths.Add(directory + "ElmcityLib"); // Elmcity python lib GenUtils.LogMsg("status", "Utils.run_ironpython", String.Join(":", paths.ToArray())); python.SetSearchPaths(paths); var ipy_args = new IronPython.Runtime.List(); foreach (var item in args) ipy_args.Add(item); var s = HttpUtils.FetchUrl(new Uri(str_script_url)).DataAsString(); try { var common_script_url = BlobStorage.MakeAzureBlobUri("admin", "common.py", false); var common_script = HttpUtils.FetchUrl(common_script_url).DataAsString(); s = s.Replace("#include common.py", common_script); } catch (Exception e) { GenUtils.PriorityLogMsg("exception", "RunIronPython: cannot #include common.py", e.Message); } var source = python.CreateScriptSourceFromString(s, SourceCodeKind.Statements); var scope = python.CreateScope(); var sys = python.GetSysModule(); //sys.SetVariable("argv", new PythonArgs() { args = ipy_args } ); sys.SetVariable("argv", args); source.Execute(scope); try { result = scope.GetVariable("result").ToString(); } catch { // GenUtils.LogMsg("info", "RunIronPython: " + str_script_url, "no result"); } python.Runtime.Shutdown(); //AppDomain.Unload(python_domain); } catch (Exception e) { result = e.Message.ToString() + e.StackTrace.ToString(); } return result; }
public static string RunIronPython(string directory, string str_script_url, List <string> args) { GenUtils.LogMsg("status", "Utils.run_ironpython: " + str_script_url, args[0] + "," + args[1] + "," + args[2]); //var app_domain_name = "ironpython"; string result = ""; try { /* * string domain_id = app_domain_name; * var setup = new AppDomainSetup(); * setup.ApplicationName = app_domain_name; * setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; * var python_domain = AppDomain.CreateDomain(domain_id, securityInfo: null, info: setup); */ var options = new Dictionary <string, object>(); options["LightweightScopes"] = true; var python = Python.CreateEngine(options); var paths = new List <string>(); paths.Add(directory + "Lib"); // standard python lib paths.Add(directory + "Lib\\site-packages"); paths.Add(directory + "ElmcityLib"); // Elmcity python lib GenUtils.LogMsg("status", "Utils.run_ironpython", String.Join(":", paths.ToArray())); python.SetSearchPaths(paths); var ipy_args = new IronPython.Runtime.List(); foreach (var item in args) { ipy_args.Add(item); } var s = HttpUtils.FetchUrl(new Uri(str_script_url)).DataAsString(); try { var common_script_url = BlobStorage.MakeAzureBlobUri("admin", "common.py", false); var common_script = HttpUtils.FetchUrl(common_script_url).DataAsString(); s = s.Replace("#include common.py", common_script); } catch (Exception e) { GenUtils.PriorityLogMsg("exception", "RunIronPython: cannot #include common.py", e.Message); } var source = python.CreateScriptSourceFromString(s, SourceCodeKind.Statements); var scope = python.CreateScope(); var sys = python.GetSysModule(); //sys.SetVariable("argv", new PythonArgs() { args = ipy_args } ); sys.SetVariable("argv", args); source.Execute(scope); try { result = scope.GetVariable("result").ToString(); } catch { // GenUtils.LogMsg("info", "RunIronPython: " + str_script_url, "no result"); } python.Runtime.Shutdown(); //AppDomain.Unload(python_domain); } catch (Exception e) { result = e.Message.ToString() + e.StackTrace.ToString(); } return(result); }