Python Programming, news on the Voidspace Python Projects and all things techie.

The Tart and his Sole Scion

emoticon:men The esteemed Mr Tartley has been up to his usual tricks. This time he has been playing with the Chipmunk physics engine and Pyglet to flesh out some prototypes for his 'summer-doss-out' game:

2D Pysics type stuff from Mr Tartley

In true freetard style he's made the code available so that you can have fun too, but with a typically odd name....

Although he says he has only tested it with Linux, at Resolver Systems we've had it running on Windows. Smile

Hopefully in the next day or so I'll do a 'release' of Python in the Browser (interactive Python interpreter running in an HTML textarea using Silverlight 2). I've fixed all the basic usability issues and added some 'preloaded examples' to illustrate how it might be used in tutorials / documentation. All the code is already available in the Google Code repository, but I need to write some brief docs. There is only around 200 lines of Python, 75 lines of Javascript and 25 lines of C# in the whole project - so customising it should be very easy.

Like this post? Digg it or it.

Posted by Fuzzyman on 2008-06-18 22:52:02 | |

Categories: , Tags: , , , ,

Chapter 14: Extending IronPython with C# / VB.NET

emoticon:torch I've just finished chapter 14 of IronPython in Action - well, finished a version I can send to my editor anyway. I had more fun writing this chapter than I expected. It's called "Extending IronPython with C# / VB.NET", and is all about writing class libraries in C# or VB.NET and using them from IronPython. This might be for performance reasons, for accessing features of the .NET framework like attributes or Linq that are hard to use from IronPython, or for simply making your class libraries as nice to use from IronPython as possible.

The table of contents for the chapter (pre-edit!) is:

14.1. Writing a Class Library for IronPython

14.1.1. Working with Visual Studio or MonoDevelop
14.1.2. Python Objects from Class Libraries
14.1.3. Calling Unmanaged Code with the P/Invoke Attribute
14.1.4. Methods with Attributes through Subclassing

14.2. Creating Dynamic (and Pythonic) Objects from C# / VB.NET

14.2.1. Creating Dynamic Objects
14.2.2. APIs with Keyword and Multiple Arguments

14.3. Compiling and Using Assemblies at Runtime

14.4. Summary

This is one of the nice things about IronPython, dropping down into C# to extend it is worlds easier than writing C extensions for CPython. There is no C API to have to worry about, and most of the normal C# you write will 'just work' when used from IronPython.

The chapter has five class libraries, each written in both C# and VB.NET. Inevitably in the process I learned more about both these languages.

VB.NET is not so bad, although more verbose than C# (semantically they're very similar of course). My favourite comparison is defining a method that provides iteration. In Python this is def __iter__(self):. In VB.NET it is Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator. Smile

Although I love the dynamic goodness of Python, the C# syntax for properties and indexers is nicer than the Python syntax.

Next onto chapter 15, which is the final chapter. I've been looking forward to writing this chapter for a long time: it is on embedding IronPython in .NET applications and interacting with DLR objects from C# (and VB.NET).

Like this post? Digg it or it.

Posted by Fuzzyman on 2008-06-15 19:11:59 | |

Categories: , Tags: ,

Python in the Browser: Live Interactive Interpreter in an HTML Textarea

emoticon:info A while ago I experimented with an interactive Python interpreter that ran in the browser using Silverlight. Unfortunately a couple of bugs with the version of IronPython for Silverlight prevented it from working properly.

Now that Silverlight 2 Beta 2 is out, along with an updated version of IronPython for Silverlight, those bugs have been fixed.

"Python in the Browser" is an interactive Python interpreter that runs in the browser, using Silverlight 2 and IronPython.

Python in the browser with IronPython and Silverlight 2

This is ideal for tutorials and documentation, where example Python code can actually be tried in the browser.

You can see a live demo of the current version at:

It requires Silverlight 2 Beta 2, and the Python version is 2.5. Builtin modules are available, but I haven't made much of the standard library available in the demo (they simply need to be added to the 'xap' file containing the silverlight application.)

Although the version in the repository works, it has some known limitations. See the issues page for the known ones so far.

The interpreter runs in an HTML textarea, with Javascript that communicates with Silverlight and prevents you deleting text from the console except after the interactive prompt.

Target browsers are Firefox 2 & 3, Safari and IE 7 & 8. (It won't work in other browsers until there is a version of Silverlight that works with them.) Silverlight 2 is currently only available for Windows and Mac OS X, with the Firefox, Safari or IE browsers. Linux support is in the works via the Moonlight project from Mono.

The project is a combination of IronPython (for the interpreter loop), Javascript (for the 'console behaviour' in the textarea) and C# (as a helper to call into Silverlight from Javascript). On every keypress Javascript calls into IronPython (via the C#!). If the keypress is an 'enter', then it pushes the current line into the interpreter loop (which uses the standard library code module). Stdout is diverted to print into the textarea, where tracebacks are also sent. If you are attempting to type over, or delete, previous output then the keypress is cancelled.

Like this post? Digg it or it.

Posted by Fuzzyman on 2008-06-15 17:41:27 | |

Categories: , , Tags:

Hosted by Webfaction