-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIronPython-Scripting.linq
More file actions
74 lines (60 loc) · 2.32 KB
/
IronPython-Scripting.linq
File metadata and controls
74 lines (60 loc) · 2.32 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<Query Kind="Program">
<NuGetReference>IronPython</NuGetReference>
<Namespace>IronPython.Hosting</Namespace>
<Namespace>Microsoft.Scripting</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<IncludePredicateBuilder>true</IncludePredicateBuilder>
</Query>
async void Main()
{
var linqPadAssemblyName = GetType().Assembly.FullName.Split(new [] { ',' })[0];
IEnumerable<Model.ProcessingModel> models =
Enumerable.Range(0, 1000000)
.Select(n => new Model.ProcessingModel { InputA = n, InputB = n * 0.5M, Factor = 0.050M });
var sw = Stopwatch.StartNew();
var pyCodeReadingTask = Task.FromResult(
File.ReadAllText(@"D:\work\Courses\MetaProgramming\Snippets\sample.py")
.Replace("LinqPadAssemblyName", linqPadAssemblyName));
// create IronPython engine
var pyEngine = Python.CreateEngine();
var pyScope = pyEngine.CreateScope();
var source = pyEngine.CreateScriptSourceFromString(await pyCodeReadingTask, SourceCodeKind.File);
// execute script
source.Execute(pyScope);
dynamic businessRule = pyEngine.Operations.Invoke(pyScope.GetVariable("BusinessRule"));
IEnumerable<dynamic> results =
models
.Select(model => businessRule.calculate(model))
.ToList();
sw.Stop();
string.Format("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds).Dump();
results
.Zip(models, (result, model) => new { result, model })
.Select(@group =>
new
{
@return = @group.result,
ResultModel = @group.model
})
.Take(10)
.Dump();
}
}
namespace Model
{
public class ProcessingModel
{
public decimal InputA { get; set; }
public decimal InputB { get; set; }
public decimal Factor { get; set; }
public decimal? Result { get; set; }
public decimal? Delta { get; set; }
public string Description { get; set; }
public decimal? Addition { get; set; }
}
public class ReportModel
{
public decimal? Σ { get; set; }
public decimal? Δ { get; set; }
public string λ { get; set; }
}