Sunday, January 6, 2013

Python Decorators

I've now been working in Python for more than a year and we have been doing some pretty crazy stuff, especially around decorators and authorization/permissions implementations.

Admittedly, Python decorators are one of the bits of the language that can be best defined as 'magic' and certainly remains puzzling for someone like me, used to Java strict and (I still believe, much safer) compile-time type-checking.

Anyway, sparked by this very useful article on decorators, I have decided to further explore the topic of class decorators, that in the original article was just mentioned in passing, and found a few twists - the most surprising of which has certainly been finding out that the call sequence, and the type of objects being associated with type names depends on whether the decorator annotation is followed by a parameter list.
It is worth noting that python Mocks use decorators pretty heavily, and it's easy to see why, once you browse the code below: it's heavily commented and should give enough of an idea of what goes on; it can be easily executed by just running

@copyright: (c) 2013. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.


Code to try out Python decorators.

Original idea from:

@author: Marco Massenzio (
         Created on [2013-01-05]

class DecoratorClass(object):

    def __init__(self, klass=None, **kwargs):
        """This gets called every time the @DecoratorClass annotation
        is encountered.

        @param klass: the type of the class being decorated, it will be 
                passed in if no parameters are passed in the annotation
        @type klass: type
        @param kwargs: a dictionary containing any of the named 
                parameters passed in the decorator's declaration
        @type kwargs: dict

        # If the decorator is declared without parameters, 
        # the class type will be passed in
        self._name = 'default'
        self._klass = None
        if klass:
            print 'No-args decorator for: ', klass.__name__
            self._klass = klass
            print 'Args declared: ', kwargs
            self._name = kwargs.get('name')

    def __call__(self, *args, **kwargs):
        """This gets invoked every time a decorated class gets created, 
        with the actual arguments in what would appear to look like a 
        constructor call: they do not have to (and in fact, won't) match 
        the actual formal argument list of the
        constructor of the decorated class (which may not even get invoked).

        If the decorator is declared with one or more arguments 
        (see Different class below), then the first item in ``args`` will be the
        **type** of the class being decorated (which
        will not have been passed in to the __init__()).

        This gets invoked immediately after self#__init__() if the decorator is 
        declared with one or more arguments, or when the "constructor" is invoked.
        print 'Decorator __call__ invoked with: ', args, kwargs
        if self._klass:
            print 'Decorated instance: ', self._klass.__name__
            return self._klass(self._name)
            print 'Setting up klass'
            self._klass = args[0]
            # Here we inject a 'class-level' static method
            self._klass.get_name = self.get_name
        return self._klass

    def get_name(self):
        return self._name

class Decorated(object):
    def __init__(self, name):
        print '>>>>> I am being decorated! <<<<<' = name

    def call_me(self, *args):
        print 'I\'m being called: ',
        print 'These are my args: ', args

# Notice how here a 'type' name (Decorated) has been completely 
# 'hijacked' to point to a specific
# instance of a DecoratorClass object (this provides 'closure')

print 'what is Decorated here?', Decorated
# >> what is Decorated here? <__main__.DecoratorClass object at 0x7f34b71308d0>

class Different(object):
    def __init__(self):
        # Notice how here we are using a 'static' method 
        # that has been injected by the decorator = Different.get_name()

    def method(self, *args):
        """This is the same implementation as Decorated#call_me(), 
        just to show how they behave differently
        print 'I\'m being called: ',
        print 'These are my args: ', args

    def __call__(self, *args, **kwargs):
        return 'Different called with: ', args, kwargs

# Here, Different is whatever DecoratorClass#__call__()  returned: 
# this happens to be what one would expect it to be (a Different class 
# type) but that's only because the code makes it so

print 'what is Different here? ', Different
# >> what is Different here?  

print 'Calling Decorated class constructor'
# Note how the params being passed here have no relation with the 
# constructor argument list and in fact, the name of the 'decorated' 
# class is not even given here, but in the decorator

# Due to the 'magic' of decorators, DecoratorClass#__call__() is
# instead invoked here,
# on the instance that was created at declaration
deco = Decorated(123, "Hello", foo='foo', baz='baz')

# Again, now ``deco`` here happens to be what one would expect it to 
# be (an instance of the Decorated class) but solely because the code 
# makes it so - it could have been anything, really

deco.call_me(1, 2, 3, 1)
# >> I'm being called:  default
# >> These are my args:  (1, 2, 3, 1)

print 'Creating now a Different class:'
# Here the call does actually invoke the constructor (__init__()) 
# for the Different class, and the returned object is again what one 
# would expect it to be (an instance of Different):
diff = Different()

diff.method('quart', 'naught')
# >> I'm being called:  another
# >> These are my args:  ('quart', 'naught')

# Obviously, as the Different#__call__() method is defined, we can call it too:

print diff(1, 2, 3, value='val')
# >> ('Different called with: ', (1, 2, 3), {'value': 'val'})

# If we now create an entirely different instance of a Different object,
# it will still have the same 'static' method injected by the decorator:
another_diff = Different()

print 'My name is:',
# >> My name is: another

Sunday, December 23, 2012

Fireside Chat with Jeff Bezos

AWS re:Invent post 3 of 3

trust me - the one on the right is Jeff Bezos

A guiding principle for Amazon is its focus on "stuff that won't change over time:" for example, it's extremely unlikely that people will ever want stuff at higher prices or slower delivery.

As an entrepreneur  one should equally focus on the 'basics' of the respective problem domain: ask yourself, "what are the key user requirements, that won't change in 3-5 years?"

This blog has now moved to please read the rest of this post here.

Saturday, December 8, 2012

AWS re:Invent - my notes (post 2 of 3)

Note - the first post (Notes about the AWS JDK) can be found here

The following are the (minimally edited) notes I've taken while attending the session at AWS re:Invent conference in Las Vegas, NV on 27-30 November, 2012.

Keynote speech by Andy Jassy (Sr. Vice President, Amazon Web Services)

This blog has moved to please read the rest of this post (and all the others) there.

Sunday, December 2, 2012

AWS re:Invent Notes (1 of 3): SDK for Java

The following are my (only minimally edited) notes from the AWS re:Invent session (I highly recommend watching the recordings of the keynote sessions)

There are two usage levels in the SDK:
  1. Low level access to AWS system APIs; and
  2. Higher level facilities, that make it easier to access the underlying services.
Most notably, the SDK provides support for S3, DynamoDB, Policy API, Flow (SWF).

This blog has now moved to, please read the rest of this post there.

Prune local tracking of stale remote branches

If you suffer from OCD like myself, you'll likely be just as annoyed by the output of 

git branch -a


We-ve moved! Please read the rest of this post on

Tuesday, August 28, 2012

Tags in git

Tag a release

Once one is happy with the code and the build, a given commit can be tagged with a proper release tag::

    $ git checkout release

    # Code changes, fixes, build, tests, etc.
    $ git commit -m"Release candidate complete"

    # To view the latest commit hash:
    $ git log
    commit 2e4a6e1a7cc3bf2298055937ab79f72fb58abf1f
    Author: Marco Massenzio
    Date:   Tue Aug 28 22:49:04 2012 -0700

            Release candidate complete

    # Tag it with the appropriate release number
    git tag -m"Rel. 0.6 RC3" rel_0.6rc3 2e4a6e1a

    # Push the commits to origin:
    $ git push origin release

    # The tag will be kept local to the repository, must be pushed too:
    $ git push origin rel_0.6rc3:refs/tags/rel_0.6rc3

When others pull from the remote origin repo, the tag will be pulled too.


A nice feature of github is that it records the tags separately, and clicking on any of them will
cause a download of a 'clean' working directory into a ZIP file or tarball, reflecting the state of
the project at the exact point when the tagged commit was pushed.

Friday, March 23, 2012

Eclipse Juno has a new look!

Just downloaded the latest milestone release (M6) of Eclipse Juno (4.2) and I was quite amazed at finding out it has a brand new splashscreen, which, I must confess, quite rather like!

And Juno brand new UI looks really really neat!