| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0bd8c47 commit eebe879
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + pythonnet | ||
| 2 | + ========= | ||
| 3 | + | ||
| 4 | + This fork of http://sourceforge.net/projects/pythonnet/ allows easy calling of python functions from C#. | ||
| 5 | + | ||
| 6 | + All calls to python should be inside a "using (Py.GIL()) {/* Your code here */}" block. | ||
| 7 | + Import python modules using dynamic mod = Py.Import("mod"), then you can call functions as normal, eg | ||
| 8 | + mod.func(args). | ||
| 9 | + Use mod.func(args, Py.kw("keywordargname", keywordargvalue)) to apply keyword arguments. | ||
| 10 | + All python objects should be declared as 'dynamic' type. | ||
| 11 | + Mathematical operations involving python and literal/managed types must have the python object first, eg np.pi*2 works, 2*np.pi doesn't. | ||
| 12 | + | ||
| 13 | + EG: | ||
| 14 | + | ||
| 15 | + static void Main(string[] args) | ||
| 16 | + { | ||
| 17 | + using (Py.GIL()) { | ||
| 18 | + dynamic np = Py.Import("numpy"); | ||
| 19 | + dynamic sin = np.sin; | ||
| 20 | + Console.WriteLine(np.cos(np.pi*2)); | ||
| 21 | + Console.WriteLine(sin(5)); | ||
| 22 | + Console.WriteLine(np.cos(5) + sin(5)); | ||
| 23 | + dynamic a = np.array(new List<float> { 1, 2, 3 }; | ||
| 24 | + dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32)); | ||
| 25 | + Console.WriteLine(a.dtype); | ||
| 26 | + Console.WriteLine(b.dtype); | ||
| 27 | + Console.WriteLine(a * b); | ||
| 28 | + Console.ReadKey(); | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + which outputs: | ||
| 33 | + | ||
| 34 | + 1.0 | ||
| 35 | + -0.958924274663 | ||
| 36 | + -0.6752620892 | ||
| 37 | + float64 | ||
| 38 | + int32 | ||
| 39 | + [ 6. 10. 12.] | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments