Re: [Vala] Embed Python into Vala, extend Vala with Python



Hi,

here's a proof of concept. Compile with:

$ valac pythondemo.vala -X -lpython2.6

If you want to do more interesting things (e.g. function callbacks) you
will have to add more API bindings.

(Python code from http://99-bottles-of-beer.net/language-python-808.html)


Best regards,

Frederik
[CCode (header_filename = "python2.6/Python.h")]
namespace Python {
        [CCode (cname = "Py_Initialize")]
        public extern static void initialize ();
        [CCode (cname = "PyRun_SimpleString")]
        public extern static void run_simple_string (string code);
        [CCode (cname = "Py_Finalize")]
        public extern static void finalize ();
}

int main () {
        Python.initialize ();

        Python.run_simple_string ("""

for quant in range(99, 0, -1):
   if quant > 1:
      print quant, "bottles of beer on the wall,", quant, "bottles of beer."
      if quant > 2:
         suffix = str(quant - 1) + " bottles of beer on the wall."
      else:
         suffix = "1 bottle of beer on the wall."
   elif quant == 1:
      print "1 bottle of beer on the wall, 1 bottle of beer."
      suffix = "no more beer on the wall!"
   print "Take one down, pass it around,", suffix
   print "--"

""");

        Python.finalize ();

        return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]