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

Open source from my colleagues: texttree and svgbatch

emoticon:envelope A couple of my colleagues at Resolver Systems have been up to interesting things in their spare time.

Jonathan Hartley has been threatening to make progress on his game for months, but in the meantime he has been having fun with pyglet and SVG vector graphics:

  • svgbatch

    SvgBatch is a pure Python package to load SVG vector graphic files, and convert them into pyglet Batch objects, for OpenGL rendering.

Actually he has made progress on the game, with a little spaceship that flies around a world populated with giant stick men and pacmen. He owes us all a blog entry on it with some screenshots...

Tom Wright has released a bit of code he needed. Fairly specific but it could be useful:

  • texttree

    texttree is a basic python module for parsing textual representations of trees of strings which use indentation to indicate nesting.

On his website (which I only discovered today), Tom has an interesting article:

  • Runtime DLL Patching

    Obviously, monkey-patching is clearly impossible in static, compiled languages since the addresses of the functions you call are fixed at compile time leaving you no indirection to use at run-time...

    Sort of - unless you want to write non-portable, machine-dependent, operating-system-dependent code. If so, it can at least be done - on 32-bit windows machines.

This is the technique Ironclad uses to fake being the Python25.dll (soon to be Python26.dll) so that it can losd Python C extensions into IronPython.

Like this post? Digg it or it.

Posted by Fuzzyman on 2009-08-14 23:28:49 | |

Categories: , , Tags: , , , , ,

ctypes for C#? Dynamic P/Invoke with .NET 4

emoticon:avocado One of the big new features of .NET / C# 4.0 is the introduction of the dynamic keyword. This is the fruit of Microsoft employing Jim Hugunin as an architect for the Common Language Runtime; charged with making .NET a better platform for multiple languages including dynamic languages.

dynamic is a new static type for C#. It allows you to statically declare that objects are dynamic and all operations on them are to be performed at runtime. This delegates to the dynamic language runtime, at least parts of which are becoming a standard part of the .NET framework in version 4.0. One of the main use cases for this is to simplify interacting between dynamic languages and statically typed languages. Currently if you use IronPython or IronRuby to provide scripting in a .NET application, or write part of your application in a dynamic language, all interaction with dynamic objects from a statically typed language (C# / VB.NET / F#) has to be done through the DLR hosting API. This is not difficult but can feel a bit clumsy. In C# 4.0 interacting with dynamic objects gets a lot easier.

dynamic can also be used for other purposes, like late bound COM or even duck-typing. Another interesting use is to create the same kinds of fluent interfaces that are both common and intuitive to use in dynamic languages. One example that most developers have worked with at some point is DOM traversal on document in Javascript, where member resolution is done at runtime. You implement this in Ruby with method_missing and in Python the equivalent is __getattr__ (or __getattribute__ for the really brave). In C# you inherit from DynamicMetaObject and override BindInvokeMember.

The Mono guys have been working on supporting dynamic in their implementation of .NET 4.0. Miguel de Icaza (lead developer of Mono) blogged about it recently: C# 4's Dynamic in Mono.

The .NET Foreign Function Interface (FFI - how you call into C libraries) is called P/Invoke (Platform Invoke). Included in Miguel's blog entry was an example, by Mono developer Marek, of creating a dynamic PInvoke. Miguel describes it as "similar to Python's ctype library in about 70 lines of C# leveraging LINQ Expression, the Dynamic Language Runtime and C#'s dynamic". Instead of writing wrapper methods for every function you call, they can be looked up at runtime!

The PInvoke class is demonstrated on Mono using libc and the printf function. The class itself compiles without change on the Visual Studio 2010 beta. To use it on Windows swap out libc for msvcrt and printf for wprintf:

dynamic d = new PInvoke("msvcrt");

for (int i = 0; i < 100; ++i)
    d.wprintf("Hello world, %d\n", i);

Very nice. Smile

Like this post? Digg it or it.

Posted by Fuzzyman on 2009-08-14 17:14:19 | |

Categories: , Tags: , , , , ,

Resolver One 1.6 Beta and competition winners (Sudoku solver and more...)

emoticon:mirrormask After a lot of work, and the usual last minute blind panic, we have got to a beta release of Resolver One version 1.6. As always this release includes a combination of standard spreadsheet features, bug fixes and usability improvements plus features unique to Resolver One.

If you've not heard of Resolver One it is the creation of Resolver Systems. Resolver One is an IronPython powered highly programmable spreadsheet for Windows. As well as being written in IronPython it is programmable with IronPython. For a good introduction to Resolver One, and how it is different from legacy spreadsheets, I recommend the article Resolver One and Illustrative Programming.

Of course if you're a programmer the most interesting thing about Resolver One is that it provides a powerful UI for wrangling and presenting data whilst letting you actually work with it from Python (including now using the numerical processing library Numpy)...

Resole One, the spreadsheet development environment

The major new features in Resolver One 1.6 are:

  • Expanded cells and images in cells

    A long awaited feature! Your charts and dynamically generated images no longer need to go in a separate worksheet (although you can still use image worksheets if you prefer). Expanded cells are particularly useful for holding images but can also be used just for formatting.

  • Numpy support comes out of beta

    import numpy in user code now 'just works'. You can use numpy arrays and functions in your formulae or user code, with support for displaying and unpacking numpy arrays into the grid. The NumPy in Resolver One screencast shows off the feature.

    This is made possible with Ironclad which also allows you to use Python C extensions like Scipy from IronPython.

  • Autocomplete in the console

    The interactive Python console is an invaluable tool for experimenting whilst you are developing spreadsheets. We've now added autocomplete, activated with control-space.

  • The Resolver One Player

    The Resolver One Player is intended for users of spreadsheets rather than creators. It allows you to deploy applications created with Resolver One to end users without exposing them to the scary intricacies of Python code (and protecting your code from them). Resolver One itself has a 'Player mode' so you can see what they look like when deployed with the player.

  • Configurable fonts everywhere

    You can now configure the default font for the grid plus the fonts used by the console, code box and formula bar. To those of our users who have an allergic response to Courier New this will come as a great relief.

  • Drag to reorder worksheets

    Another long awaited feature.

There are a couple of minor known bugs in the beta (both already fixed in our Subversion repository here) and there is one feature that will be in the final that didn't make it in time for the beta. This is a separate packaging of the dependencies needed to run Resolver One spreadsheets, without the user interface, from IronPython or other .NET languages. This will make it easier to do things like distributed computing with Resolver One spreadsheets and uses the RunWorkbook function which effectively embeds the Resolver One calculation engine.

If you would like to try the 1.6 beta email

Entries in the Resolver One Spreadsheet Challenge

We had some awesome entries in the latest round of our spreadsheet challenge competition, and most of them are now available to download from the Resolver Exchange.

The winner of the June / July round was Suresh Shanmugam, who integrated Resolver One with Microsoft's Solver Foundation to create a spreadsheet that solves Sudoku puzzles.

Solving Sudoku

You'll need to install these two dependencies to get it working, but the Sudoku solver is great fun:

As well as being fun it is a good example of how to use the Microsoft Solver Foundation from Resolver One and IronPython.

The other entries also have some great examples of how to do funky things from Resolver One:

Like this post? Digg it or it.

Posted by Fuzzyman on 2009-08-12 14:31:24 | |

Categories: , Tags: , , , , ,

Hosted by Webfaction