Advertisement
Guest User

Untitled

a guest
Jul 27th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using IronPython.Hosting;
  8. using IronPython.Modules;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             var x = typeof(PythonMath);
  17.             if (x == null)
  18.                 throw new Exception("Do not remove - Used to force local copy.");
  19.  
  20.             var engine = IronPython.Hosting.Python.CreateEngine();
  21.             var script = @"def foo(y):
  22.    return y + 1
  23.  
  24. def bar(x):
  25.    return foo(x)
  26.  
  27. bar(-5)
  28. ";
  29.             var ast = @"import ast
  30. tree = ast.parse(script)
  31. callback(ast.dump(tree))";
  32.  
  33.  
  34.             var callback = new Action<object>(o =>
  35.             {
  36.                 Console.WriteLine(o);
  37.             });
  38.  
  39.  
  40.             engine.SetSearchPaths(new[] { "pylib" });
  41.             var scope = engine.CreateScope();
  42.             scope.SetVariable("script", script);
  43.             scope.SetVariable("callback", callback);
  44.  
  45.             engine.Execute(ast, scope);
  46.  
  47.            
  48.  
  49.             Console.ReadLine();
  50.         }
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement