Python Snippet: Converting a complex object to json


Here’s a simple recipe, to convert most Python class-objects to json and back.

Here’s the setup:

import json

class Foo:
    def __init__(self, name, values): = name
        self.values = values

Converting to json:

an_object = Foo('John', (123, 456, 789))

json_string = json.dumps(vars(an_object))

Quite simple, isn’t it? Using the vars function, Python converts the object to a dictionary which can then be jsonified, using the json.dums function.

The reverse is quite simple as well:

a_fresh_object = Foo(**json.loads(json_string))

This simply takes the output of json.loads, which is a dict, and maps the keys and values of that dict to the names arguments of Foo’s __init__ method.


So long…

Writing Python Function Decorators

Python has a pretty neat way, of letting you write function decorators.

A Function decorator in general, alters or appends to the functionality performed by the decorated method, without having to directly depend on or know it.

Thus it is possible, to use decorators to declare aspects, without re-writing the same code over and over again.

In python, you can decorate a function by using the following syntax:

def my_function(arg1, arg2):
    print "Something with %s and %s" % (arg1, arg2)

By doing that, python exchanges any calls to your method, by calls to the decorator, defined as my_decorator. This decorator can now decide, to do some additional work and (if wanted) call the original function.

To define a decorator, you have to write a function factory. That is a function, that defines and returns another function, that can be called with the original function as an argument.

def my_decorator(argument):
    def outer_wrapper(original_method):
        # This code gets called, when the decorated
        # function gets defined

        def inner_wrapper(*args, **kwargs):
            # This method gets called, instead of the
            # actual function call to the decorated
            # function.
            print "I come from before the function"
            return_value = original_method(args, kwargs)
            print "The method returned %s" % return_value
            return return_value

        return inner_wrapper
    return outer_wrapper

Have fun.

A Block of Modern Flats in Django

I have tried my hand a bit at using Django the last weeks and it is a web framework, that really is fun to develop in.

However there is one thing that I came to fight with quite a bit. That is building a good multi-tenant aware application in django.

It is to be said, that I wanted a true multi-tenant application, that could serve different data universes from the same running server and the same database, which is a focus that django simply hadn’t been build for yet. Nevertheless, since it is a very transparent and straight forward framework, I actually got it to work quite nicely.
Continue reading

Day 2: Using Python to compile C++

This is the next step, in creating a simpler C++ build environment.

The tools available, to build, compile and link in C++ allow you a great freedom in how you may set up your final application. That flexibility comes at a higher price in complexity. Most of the time, you would want to build either a framework, or an application, using a standard approach, that works best on your chosen target environment.

If the goal is, to have a build environment, that is extremely easy to use, using the convention over configuration approach, one step on that road is to simplify the compiling & linking step in the chain. In the end, the compiler interface should know which target it is supposed to be building for, which libraries and includes to use and which sources to compile into the final product. It should behave similar on any supported platform. The end-product of the compilation step should be a static or shared library or an executable.

To accomplish that goal, I created a compiler Class in Python, that gives me the abstract handles I need and implemented it for OsX. I also wrote a simple script, to call the compiler.

Next up:
Integrating this compiler and a script to automatically create a main for the UnitTest framework, from Day 1 so that I can run unit tests on any c++ project in a very simple way.