<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Desk Checked &#187; Python</title>
	<atom:link href="http://www.deskchecked.com/category/languages/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deskchecked.com</link>
	<description>Thomas Lee's programming blog</description>
	<lastBuildDate>Sun, 29 Nov 2009 13:01:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python Language Internals: From Source to Execution</title>
		<link>http://www.deskchecked.com/2008/12/10/python-language-internals-from-source-to-execution/</link>
		<comments>http://www.deskchecked.com/2008/12/10/python-language-internals-from-source-to-execution/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 14:19:24 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.deskchecked.com/?p=145</guid>
		<description><![CDATA[Last week I gave a presentation in Sydney for the Open Source Developer&#8217;s Conference on the internals of the Python compiler. Now that the conference is over, I figure there&#8217;s no better place for the original paper than right here on my blog.
Thanks again to my employer, Shine for all their wonderful support with this. [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I gave a presentation in Sydney for the <a href="http://www.osdc.com.au">Open Source Developer&#8217;s Conference</a> on the internals of the Python compiler. Now that the conference is over, I figure there&#8217;s no better place for the original paper than right here on my blog.</p>
<p>Thanks again to my employer, <a href="http://www.shinetech.com">Shine</a> for all their wonderful support with this. Thanks too to the folks who reviewed my paper before it was submitted and my colleagues at Shine who endured a painful test run that ran well over time! <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>OSDC 2008 was a whole lot of fun. If you happened to miss the conference, I strongly recommend you make it along next year. It&#8217;s well worth the trip.</p>
<p>Download <a href="http://www.deskchecked.com/wp-content/uploads/2008/12/python-language-internals.pdf">Python Language Internals: From Source to Execution</a>, and a corresponding <a href="http://www.deskchecked.com/wp-content/uploads/2008/12/python-26-unless.patch">patch against Python 2.6 beta 3</a> implementing the &#8220;unless&#8221; statement. If you want to test the patch, you also be aware of the nasty little <a href="http://bugs.python.org/issue4347">circular dependency</a> that may cause you some grief &#8212; apply the latest (non-review) patch from the tracker before you start the build and you should be fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2008/12/10/python-language-internals-from-source-to-execution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Speaking at OSDC 2008</title>
		<link>http://www.deskchecked.com/2008/08/15/speaking-at-osdc-2008/</link>
		<comments>http://www.deskchecked.com/2008/08/15/speaking-at-osdc-2008/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 10:06:10 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/?p=86</guid>
		<description><![CDATA[I&#8217;ve been offered a position to speak at this year&#8217;s Open Source Developers&#8217; Conference on the internals of the Python compiler. I&#8217;ll be introducing the audience to the basic workings of the Python bytecode compiler, demonstrating how one would add a new construct to the language. 
If you&#8217;re at the conference and interested in messing [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been offered a position to speak at this year&#8217;s <a href="http://www.osdc.com.au">Open Source Developers&#8217; Conference</a> on the internals of the Python compiler. I&#8217;ll be introducing the audience to the basic workings of the Python bytecode compiler, demonstrating how one would add a new construct to the language. </p>
<p>If you&#8217;re at the conference and interested in messing with the guts of a real compiler but not quite sure where to start, please come along. The only real prerequisite is a sound understanding of the C language.</p>
<p>The conference runs from the 2nd to the 5th of December. Registration should open some time in October if you&#8217;re interested in tagging along.</p>
<p>Now, back to my paper &#8230;</p>
<p><a href="http://www.osdc.com.au/2008"><img src="http://www.osdc.com.au/2008/_img/OSDC2008BlogBadgeLarge.gif" alt="OSDC 2008: Sydney" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2008/08/15/speaking-at-osdc-2008/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Internals of Python&#8217;s IMPORT_NAME Bytecode</title>
		<link>http://www.deskchecked.com/2008/04/14/the-internals-of-pythons-import_name-bytecode/</link>
		<comments>http://www.deskchecked.com/2008/04/14/the-internals-of-pythons-import_name-bytecode/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 10:09:40 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/?p=59</guid>
		<description><![CDATA[This was originally planned as a response to this post by Paul Bonser, but grew a little unwieldy (and his comment submission form seems to be broken?).
Effectively, Paul was (somewhat sleepily) mulling over the workings of the IMPORT_NAME bytecode. This bytecode is generated in response to Python code like the following:
import sys
And also for:
from foo [...]]]></description>
			<content:encoded><![CDATA[<p>This was originally planned as a response to <a href="http://blog.paulbonser.com/2008/04/14/python-import_name/">this post</a> by Paul Bonser, but grew a little unwieldy <del>(and his comment submission form seems to be broken?)</del>.</p>
<p>Effectively, Paul was (somewhat sleepily) mulling over the workings of the IMPORT_NAME bytecode. This bytecode is generated in response to Python code like the following:</p>
<pre><code>import sys</code></pre>
<p>And also for:</p>
<pre><code>from foo import bar, baz</code></pre>
<p>You&#8217;ll have to see the original post for the actual bytecode generated for this code, but Paul was asking why the latter syntax generates an IMPORT_NAME bytecode instruction which seems to do nothing at all with the fromlist, then generates additional IMPORT_FROM bytecodes that fetch the fromlist attributes from the parent module.</p>
<p>The documentation for <a href="http://docs.python.org/lib/built-in-funcs.html">__import__</a> somewhat solves this mystery:</p>
<blockquote cite="http://docs.python.org/lib/built-in-funcs.html"><p>
Note that even though locals() and ['eggs'] are passed in as arguments, the __import__() function does not set the local variable named eggs; this is done by subsequent code that is generated for the import statement. (In fact, the standard implementation does not use its locals argument at all, and uses its globals only to determine the package context of the import statement.)
</p></blockquote>
<p>Essentially, when the IMPORT_NAME is executed for &#8216;from foo import bar, baz&#8217;, the __import__ builtin is called with the fromlist and a few other arguments (namely the globals() and locals() from the current frame of execution) to provide custom import handling for your Python programs. For example, you may want to prevent users of your program from writing scripts that import certain modules. I imagine Google&#8217;s App Engine might be using something like this to prevent access to certain evil or unavailable modules (but that&#8217;s just a wild, unfounded guess).</p>
<p>The code in Python/ceval.c for IMPORT_NAME seems to back this up (I&#8217;ve annotated the code with a few comments):</p>
<pre><code>
        case IMPORT_NAME:
            w = GETITEM(names, oparg);
            <span style="color: #0000ff">/* 1. LOCATE THE __import__ BUILTIN */</span>
            x = PyDict_GetItemString(f->f_builtins, "__import__");
            if (x == NULL) {
                PyErr_SetString(PyExc_ImportError,
                        "__import__ not found");
                break;
            }
            Py_INCREF(x);
            v = POP();
            u = TOP();
            <span style="color: #0000ff">/* 2. BUILD THE LIST OF ARGUMENTS FOR __import__ USING THE fromlist, globals() AND locals() */</span>
            if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
                w = PyTuple_Pack(5,
                        w,
                        f->f_globals,
                        f->f_locals == NULL ?
                          Py_None : f->f_locals,
                        v,
                        u);
            else
                w = PyTuple_Pack(4,
                        w,
                        f->f_globals,
                        f->f_locals == NULL ?
                          Py_None : f->f_locals,
                        v);
            Py_DECREF(v);
            Py_DECREF(u);
            if (w == NULL) {
                u = POP();
                Py_DECREF(x);
                x = NULL;
                break;
            }
            READ_TIMESTAMP(intr0);
            v = x;
            <span style="color: #0000ff">/* 3. CALL __import__ WITH THE module name, fromlist, globals() AND locals() */</span>
            x = PyEval_CallObject(v, w);
            Py_DECREF(v);
            READ_TIMESTAMP(intr1);
            Py_DECREF(w);
            SET_TOP(x);
            if (x != NULL) continue;
            break;
</code></pre>
<p>So this answers the question of why IMPORT_NAME needs the fromlist in the first place: it is merely passed along to __import__ to make it available to custom import handling code. But why aren&#8217;t the fromlist attributes added to the namespace inside IMPORT_NAME? I&#8217;m guessing it was a design decision: we already have an opcode for adding elements to the namespace, so why have a special case for imports? Of course the details may be more involved than that, but it&#8217;s the most obvious explanation I can think of.</p>
<p>In any case, thanks for the thought-provoking post, Paul!</p>
<p><strong>UPDATE:</strong> Seems my comment made it through to his blog after all. Sorry for the double-up!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2008/04/14/the-internals-of-pythons-import_name-bytecode/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python 2.6a2: Compile ASTs from within Python code</title>
		<link>http://www.deskchecked.com/2008/04/04/python-26a2-compile-asts-from-within-python-code/</link>
		<comments>http://www.deskchecked.com/2008/04/04/python-26a2-compile-asts-from-within-python-code/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 09:27:15 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/?p=57</guid>
		<description><![CDATA[I&#8217;m not going to go into too much depth because Georg Brandl has already covered it, but it&#8217;s an interesting topic. I couldn&#8217;t help but write a little entry about it.  
A new alpha of Python has just been released, including a patch I wrote for compiling Python Abstract Syntax Trees down to bytecode. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not going to go into too much depth because <a href="http://pyside.blogspot.com/2008/03/ast-compilation-from-python.html">Georg Brandl</a> has already covered it, but it&#8217;s an interesting topic. I couldn&#8217;t help but write a little entry about it. <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A new alpha of <a href="http://www.python.org">Python</a> has just been <a href="http://www.python.org/download/releases/2.6/">released</a>, including a patch I wrote for compiling Python <a href="http://en.wikipedia.org/wiki/Abstract_Syntax_Tree">Abstract Syntax Trees</a> down to bytecode. This means it&#8217;s now possible to manipulate ASTs from within your Python program, which lets you do all sorts of crazy things &#8211; <a href="http://www.deskchecked.com/wp-content/uploads/2008/04/wacky.py">like this</a>, for example.</p>
<p>Piping this little program into itself yields the following:</p>
<pre><code>$ ./python wacky.py &lt;wacky.py
Bwahaha! I was once an Assign node!
Bwahaha! I was once an Assign node!
</code></pre>
<p>Neat huh?</p>
<p>Anyway, since then &#8211; on Neal Norwitz&#8217;s advice &#8211; I&#8217;ve started on an experimental patch for what I hope will one day be an optimizer for Python ASTs. Even though it&#8217;s early days, the possibilities offered by optimizing at the AST level are very interesting. For example, the (dirty, filthy, ugly, hack of a) patch I&#8217;m working on at the moment has support for optimizing this code:</p>
<pre><code>if 1:
    'true'
else:
    'false'
</code></pre>
<p>Down to this (remember, no bytecode has been generated yet):</p>
<pre><code>'true'</code></pre>
<p>Very, very exciting stuff.</p>
<p>Anyway, I have a train to catch. More on this when I have more to show!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2008/04/04/python-26a2-compile-asts-from-within-python-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GTK Hello World in Six Different Languages</title>
		<link>http://www.deskchecked.com/2008/01/23/gtk-hello-world-in-six-different-languages/</link>
		<comments>http://www.deskchecked.com/2008/01/23/gtk-hello-world-in-six-different-languages/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 11:19:17 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[GTK]]></category>
		<category><![CDATA[Ocaml]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/2008/01/23/gtk-hello-world-in-six-different-languages/</guid>
		<description><![CDATA[I&#8217;m still somewhat in holiday mode, so this entry is probably going to feel a little cheap for those of you following my more technical posts. I&#8217;m a big fan of GTK+ for user interfaces. If you don&#8217;t have the option or desire to use the Java platform and Swing, GTK+ is one of the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still somewhat in holiday mode, so this entry is probably going to feel a little cheap for those of you following my more technical posts. I&#8217;m a big fan of <a href="http://www.gtk.org">GTK+</a> for user interfaces. If you don&#8217;t have the option or desire to use the Java platform and Swing, GTK+ is one of the better cross-platform user interface toolkits out there.</p>
<p>It&#8217;s high-level enough that it is easy to build quick, effective GUIs but low-level enough not to get in your way when you need to start messing around at the pixel level. GUIs can be built by hand using code, or designed using <a href="http://glade.gnome.com">Glade</a> and exported to an XML document to be loaded at runtime by any GTK+ binding. Currently it runs on Windows and Linux (the toolkit actually has its roots in GNOME) and has bindings for most popular programming languages. The only major downer is that Mac users are left out in the cold unless they go to the (herculean) effort of getting X11 up and running.</p>
<p>All the little differences between the various GTK+ bindings out there tend to get my goat when I move from one language to another. You would think that a GTK+ example in C could easily be translated to other languages without referencing documentation right? For one reason or another, the GTK+ bindings for other languages tend to diverge from the pleasant consistency of the C API to varying degrees. This post is all about those little differences that crop up even in the simplest applications: GTK&#8217;s take on &#8220;Hello World&#8221; in a few different languages.</p>
<p><strong>C</strong></p>
<p>C, being the language GTK+ is actually written in, is probably the most consistent with function naming across the different objects &#8230; the GTK_* and G_* macros are quite ugly though.</p>
<pre><code>#include &lt;gtk/gtk.h&gt;

int main (int argc, char **argv) {
  GtkWidget *window;
  gtk_init(&#038;argc, &#038;argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Hello, World");
  g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);
  gtk_widget_show_all (window);

  gtk_main();
  return 0;
}
</code></pre>
<p><strong>C#/Mono</strong></p>
<p>GTK# adds stuff like the Application object and delegates to produce a &#8220;Hello World&#8221; example I had to go digging around in documentation for. Not too bad on the whole, though.</p>
<pre><code>using Gtk;
using GtkSharp;

public class HelloWorld {
  public static void Main(string[] args) {
    Gtk.Window window = new Gtk.Window();
    window.Title = "Hello, World";
    window.DeleteEvent += delegate { Application.Quit(); };
    window.ShowAll();
    Application.Run();
  }
}
</code></pre>
<p><strong>Ocaml</strong></p>
<p>The Ocaml bindings diverge from the original C API much more so than the other languages listed here, most likely due to design decisions that had to be made to provide a C binding to a functional language. My only real grumble is the inconsistency with window#event#connect vs window#connect. I&#8217;m guessing there was a technical reason for that, but it still irks me every time I see it.</p>
<pre><code>let delete_event evt = false

let destroy () = GMain.Main.quit ()

let main () =
  let window = GWindow.window in
  let _ = window#set_title "Hello, World" in
  let _ = window#event#connect#delete ~callback:delete_event in
  let _ = window#connect#destroy ~callback:destroy in
  let _ = window#show () in
  GMain.Main.main ()
;;

let _ = main () ;;
</code></pre>
<p><strong>Perl</strong></p>
<p>Although I find writing Perl to be painful for everything but processing text files in a terminal, I found the Perl GTK bindings to be relatively straightforward.</p>
<pre><code>use strict;
use Gtk2 '-init';

my $window = Gtk2::Window->new;
$window->set_title("Hello, World");
$window->signal_connect('delete-event', sub { Gtk2->main_quit; });
$window->show_all;
Gtk2->main;</code></pre>
<p><strong>Python</strong></p>
<p>I&#8217;m more familiar with PyGTK than with other bindings, so this was a snap. Why they chose GtkObject.connect over GtkObject.signal_connect is a mystery and the pygtk.require(&#8217;&#8230;&#8217;) crap is a little weird, but aside from that there should be nothing surprising here (this is a <em>good thing!</em>). </p>
<pre><code>import pygtk
pygtk.require('2.0')
import gtk

window = gtk.Window()
window.set_title('Hello, World')
window.connect('delete-event', gtk.main_quit)
window.show_all()
gtk.main()</code></pre>
<p><strong>Ruby</strong></p>
<p>RubyGNOME provides a GTK+ binding for Ruby. Take an almost 1:1 port of the C API, take away the ugly casting macros, mix in closures for handling signals and Ruby really is one of the nicest ways to get intimate with GTK+.</p>
<pre><code>require 'gtk2'

window = Gtk::Window.new
window.title = 'Hello, World'
window.signal_connect(:delete-event) { Gtk.main_quit }
window.show_all

Gtk.main</code></pre>
<p>That&#8217;s all for now. There are many more <a href="http://www.gtk.org/bindings.html">language bindings for GTK+</a> out in the wild for languages like Lisp/Scheme, C++, Haskell and Erlang. If you&#8217;re looking around for a GUI toolkit, be sure to give GTK a go. There&#8217;s plenty of documentation available for all the bindings listed here, often with some very detailed and easy to follow tutorials.</p>
<p><strong>UPDATE:</strong> Miguel and she suggested some changes to the C#/Mono and RubyGNOME examples.<br />
<strong>UPDATE 2:</strong> Aristotle suggested an easier way to initialize the Perl GTK bindings.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2008/01/23/gtk-hello-world-in-six-different-languages/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Verlet Integration: A Little Physics Demo in Python</title>
		<link>http://www.deskchecked.com/2007/09/28/verlet-integration-a-little-physics-demo-in-python/</link>
		<comments>http://www.deskchecked.com/2007/09/28/verlet-integration-a-little-physics-demo-in-python/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 16:06:40 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[Graphics Programming]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/2007/09/28/verlet-integration-a-little-physics-demo-in-python/</guid>
		<description><![CDATA[Just before I crawl into bed, here&#8217;s a little demo based on the first fifth of Thomas Jakobsen&#8217;s Advanced Character Physics. I don&#8217;t really have the mathematical background to talk about verlet at any length other than to say it&#8217;s very interesting.  

The screenshot doesn&#8217;t really do the neat &#8220;physics&#8221; justice &#8211; it&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>Just before I crawl into bed, here&#8217;s a little demo based on the first fifth of Thomas Jakobsen&#8217;s <a href="http://www.teknikus.dk/tj/gdc2001.htm">Advanced Character Physics</a>. I don&#8217;t really have the mathematical background to talk about verlet at any length other than to say it&#8217;s very interesting. <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.deskchecked.com/wp-content/uploads/2007/09/screenshot.png" title="Verlet demo screenshot"><img src="http://www.deskchecked.com/wp-content/uploads/2007/09/screenshot.thumbnail.png" alt="Verlet demo screenshot" /></a></p>
<p>The screenshot doesn&#8217;t really do the neat &#8220;physics&#8221; justice &#8211; it&#8217;s a cheap yet interesting effect.</p>
<p>In a nutshell, velocity verlet is an alternative to Euler (i.e. <code>new_position = current_position + velocity * elapsed</code>). In verlet, velocity is implicitly calculated based on the last position of an entity such that when <em>elapsed</em> is fixed:</p>
<pre><code>new_position = 2 * current_position - last_position + acceleration * elapsed * elapsed</code></pre>
<p>This equation tends to change the way you do pretty much everything related to your game physics. Rather than simply setting your game object&#8217;s velocity, you might push it instead via a force or hack the position/last position to achieve the same effect. I&#8217;m finding the leap a little daunting myself.</p>
<p>I actually touched on verlet briefly years ago using DirectX thanks to one of my more interesting <a href="http://www.linkedin.com/in/christianschladetsch">lecturers</a> at QANTM. Hope you don&#8217;t consider the Googling of your name to be excessively creepy, Christian. It&#8217;s been a few years. <img src='http://www.deskchecked.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The supporting code is a little heavy for such a little demo &#8211; I plan to put it to wider use, so please excuse the mess for now.</p>
<p>You&#8217;ll need a reasonably recent version of Pygame and PyOpenGL installed in order to run this demo. Once it&#8217;s up and running, click your mouse on the window to watch some OpenGL triangles drop from the sky. Click, drag and release to fling triangles &#8230; almost like you were throwing them yourself.</p>
<p>Hopefully I&#8217;ll get a chance to expand on this among the thousand other things I want to do with my free time (not the least of which is writing more Ocaml tutorials)!</p>
<p><a href="http://www.deskchecked.com/wp-content/uploads/2007/09/python_engine.zip" title="Verlet Demo">Download the Python verlet demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2007/09/28/verlet-integration-a-little-physics-demo-in-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Passing Data Between GTK Applications With GtkClipboard</title>
		<link>http://www.deskchecked.com/2007/06/27/passing-data-between-gtk-applications-with-gtkclipboard/</link>
		<comments>http://www.deskchecked.com/2007/06/27/passing-data-between-gtk-applications-with-gtkclipboard/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 03:36:04 +0000</pubDate>
		<dc:creator>tom</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[GTK]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://www.vector-seven.com/2007/06/27/passing-data-between-gtk-applications-with-gtkclipboard/</guid>
		<description><![CDATA[GtkClipboard is a wonderful (if somewhat recent) addition to GTK. As you would expect, it allows users to pass data between your application and other GTK applications via the X11 clipboard (and vice versa). A common past annoyance with the clipboard under linux is that data stored disappeared from the clipboard when the application which [...]]]></description>
			<content:encoded><![CDATA[<p>GtkClipboard is a wonderful (if somewhat recent) addition to GTK. As you would expect, it allows users to pass data between your application and other GTK applications via the X11 clipboard (and vice versa). A common past annoyance with the clipboard under linux is that data stored disappeared from the clipboard when the application which set the data terminated. No longer so, thanks to gtk_clipboard_store and the GNOME Clipboard Daemon.</p>
<p>Now, tutorials on GtkClipboard are a little sparse and the documentation seems to leave a few questions unanswered, so here&#8217;s a quick and dirty primer on passing text data between your GTK applications using GtkClipboard.</p>
<p><strong>Python</strong></p>
<pre><code>import pygtk
pygtk.require('2.0')
import gtk

# get the clipboard
clipboard = gtk.clipboard_get()

# set the clipboard text data
clipboard.set_text('Hello!')

# make our data available to other applications
clipboard.store()</code></pre>
<p><strong>Ruby</strong></p>
<pre><code>require 'gtk2'

# initialize Ruby's GTK bindings
Gtk.init

# get the clipboard
clipboard = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD)

# set the text. Ruby-Gnome2 also provides a text= setter
clipboard.set_text('Hello, World')

# make the clipboard data available to external applications
clipboard.store
</code></pre>
<p><strong>C</strong></p>
<pre><code>#include &lt;gtk/gtk.h&gt;
#include &lt;gdk/gdk.h&gt;
#include &lt;string.h&gt;

int main (int argc, char **argv) {
    const char *message = "Hello, World";

    /* initialize GTK */
    gtk_init (&amp;argc, &amp;argv);

    /* set the clipboard text */
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), message, strlen(message));

    /* store the clipboard text */
    gtk_clipboard_store(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));

    return 0;
}</code></pre>
<p>You can also use gtk_clipboard_set_image (for Ruby and Python, there are equivalents) to pass GdkPixbuf data to the clipboard. Check the GTK/PyGTK/Ruby-Gnome2 documentation for more details.</p>
<p>It&#8217;s actually all quite easy, but I thought it might be nice to see the code in practice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deskchecked.com/2007/06/27/passing-data-between-gtk-applications-with-gtkclipboard/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
