Modules

Python

Python

Description

Allow to use the Python language from urbiscript. Features two modes, simple Python code execution, and Python objects wrapping.

Dependencies

This UObject requires Python and Boost Python libraries.

Download

download here: python.tgz

Use in urbiscript

/!\ WARNING /! Load this UObject from Urbi with:

  loadLibrary ("python.so");

DO NOT USE:

  urbi-launch --start python.so

It won't load library correctly !

/!\ WARNING /!

Compilation

  • Linux

Install Urbi SDK on your computer. Install Python library (python-dev package on ubuntu). Install Boost Python library (libboost-python-dev package on ubuntu) Then run:

  make -f Makefile.linux

It will generate a dynamic library (python.so) that you can load in Urbi.

NB: If Urbi SDK is not installed in a standard location on your computer, then you can explicitely give its path:

  make -f Makefile.linux SDK_PATH=<path_to_urbi_sdk_on_your_computer>
  • Windows

// FIXME: compilation under Windows.

  • Mac OS X

// FIXME: compilation under Mac OS X.

Example of use

  • Execute some Python code:
   loadLibrary("python.so");
   Python.execute ("hello = file('hello.txt', 'w')\n"
                   "hello.write('Hello world!')\n"
     		   "hello.close()");
  • Evaluate a Python simple expression
   loadLibrary("python.so");
   Python.eval("5 ** 2");
  • Connect with telnet
   loadLibrary("python.so");
   Python.execute("import getpass");
   Python.execute("import sys");
   Python.execute("import telnetlib");
   var telnet = PyObject.create ("telnetlib.Telnet", "localhost", "54000");
   telnet.read_very_eager ();
   telnet.write ("wall(42);\n");
   telnet.close ();
   telnet.destroy ();
   removeSlot("telnet");
  • Send a mail (requires smtp and a smtp server)
   loadLibrary("python.so");
   var fromaddr = "mike@gostai.com";
   var toaddrs = [ "bob@gmail.com, bob@gostai.com" ];
   var msg = "From: " + fromaddr + "\r\n" +
             "To: " + toaddrs.join (",") + "\r\n";
   msg += "Hello my dear friend! How are you?";

   Python.execute("import smtplib");
   var smtp = PyObject.create ("smtplib.SMTP", "your.smtp.server.com", "25");
   smtp.set_debuglevel(1);
   smtp.sendmail(fromaddr, toaddrs.join(","), msg, "", "");
   smtp.quit ();
   smtp.destroy ();
   removeSlot("smtp");
  • Create a Qt window (requires python-qt4)
    loadLibrary("python.so");
    Python.execute("import sys\n"
                 "import PyQt4\n"
                 "from PyQt4 import QtCore\n"
                 "from PyQt4 import QtGui\n"
                 "app = QtGui.QApplication([\"\"])\n"
                 "window = QtGui.QDialog()\n"
                 "window.show()\n"
                 "app.exec_()\n");

Options: