-
Notifications
You must be signed in to change notification settings - Fork 0
/
getting_a_list.cs
41 lines (33 loc) · 1.24 KB
/
getting_a_list.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Getting a List From Python Into C#, IronPython Example
// Walter Gordy 2020
using System;
using System.Collections.Generic;
using IronPython.Hosting;
using IronPython.Runtime;
public class Program
{
static void Main(string[] args)
{
// python is picking about white space. No whitespace here.
string python_script = @"
x_range = range(10)
x_squared = [i**2 for i in x_range]
print(x_squared)
";
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
scope.SetVariable("app", new Program());
var ops = engine.Operations;
Console.WriteLine("Executing script");
engine.Execute(python_script, scope);
Console.WriteLine("Getting variable from script");
// Grab the variable from the executed script
var python_x_squared = scope.GetVariable("x_squared");
// python_x_squared is interable.
Console.WriteLine("{0}", String.Join(" ", python_x_squared));
// Here we convert it to a list and output the contents of the list
ListGenericWrapper<int> x_squared = new ListGenericWrapper<int>(python_x_squared);
Console.WriteLine("{0}", String.Join(" ", x_squared));
Console.ReadKey();
}
}