1 module pyind; 2 3 import std.stdio; 4 import pyd.pyd; 5 import deimos.python.Python: Py_ssize_t, Py_Initialize; 6 import pyd.embedded; 7 8 shared static this() { 9 on_py_init({ 10 def!(knock, ModuleName!"office", 11 Docstring!"a brain specialist works here")(); 12 add_module!(ModuleName!"office")(); 13 }); 14 py_init(); 15 16 wrap_class!(Gumby, 17 Def!(Gumby.query), 18 ModuleName!"office", 19 Property!(Gumby.brain_status), 20 Property!(Gumby.resolution, Mode!"r"), 21 )(); 22 } 23 24 void knock() { 25 writeln("knock! knock! knock!"); 26 writeln("BAM! BAM! BAM!"); 27 } 28 29 class Gumby { 30 void query() { 31 writeln("Are you a BRAIN SPECIALIST?"); 32 } 33 34 string _status; 35 void brain_status(string s) { 36 _status = s; 37 } 38 string brain_status() { 39 return _status; 40 } 41 42 string resolution() { 43 return "Well, let's have a look at it, shall we Mr. Gumby?"; 44 } 45 } 46 47 48 void main() { 49 // simple expressions can be evaluated 50 int i = py_eval!int("1+2", "office"); 51 writeln(i); 52 53 // functions can be defined in D and invoked in Python (see above) 54 py_stmts(q"< 55 knock() 56 >", "office"); 57 58 // functions can be defined in Python and invoked in D 59 alias py_def!( 60 "def holler(a): 61 return ' '.join(['Doctor!']*a)", 62 "office", 63 string function(int)) call_out; 64 65 writeln(call_out(1)); 66 writeln(call_out(5)); 67 68 // classes can be defined in D and used in Python 69 70 auto y = py_eval("Gumby()","office"); 71 y.method("query"); 72 73 // classes can be defined in Python and used in D 74 py_stmts(q"< 75 class X: 76 def __init__(self): 77 self.resolution = "NO!" 78 def what(self): 79 return "Yes, yes I am!" 80 >", "office"); 81 82 auto x = py_eval("X()","office"); 83 writeln(x.resolution); 84 writeln(x.method("what")); 85 86 py_stmts(q"< 87 y = Gumby(); 88 y.brain_status = "HURTS"; 89 print ("MY BRAIN %s" % y.brain_status) 90 print (y.resolution) 91 >","office"); 92 } 93 94 95 96