Archive

Archive for August, 2010

Spring Python 1.1 book review

August 22nd, 2010 Comments off

Here’s a copy of my Amazon’s Spring Python 1.1 book review.

Spring Python 1.1 is a book for professional programmers who either wish to venture into a world beyond traditional OOP and tackle problems differently thanks to Spring Python’s IoC & AOP features or simply have practical issues – such as spreading the application across multiple nodes – to solve. The book does not merely rephrase the comprehensive reference documentation available at the project’s website and I was actually very curious on how the author would be laying out the material.

Greg took a very good approach of explaining the basic of IoC first and building upon it in subsequent chapters although they can all be read separately. All of the major Spring Python’s building blocks are covered and I can only applaud that despite its Java roots almost no XML is used – that’s a good news for all Python programmers fearing that “Spring” means a lot of unnecessary XML, the prevailing majority of examples is in pure Python. Java programmers wishing to leverage their existing Spring skills won’t be disappointed though as there are some chapters devoted to how one can easily migrate a Spring Java IoC container over to Spring Python one, there’s also a discussion of how to use Spring Python & Jython which, along with CPython, is also a target Python implementation Spring Python can run on. But let that not confuse you, the book is mostly written for Python programmers.

I liked it that the book was focusing on getting things done without turning aside into discussing vaguely related concepts or nuances, for instance the chapter on SQL doesn’t do any ‘quick recaps’ of what SQL and relational databases are as is unfortunately quite common in many other books. On the other hand, not everyone is familiar with IoC and AOP and I know that anyone unfamiliar with those concepts – regardless of their primary programming language of choice – can easily take the book and get acquainted with them. A superb idea was that most of the chapters teach you how to write unit tests for the given Spring Python component you’d want to use in your code, IoC, AOP, security, database access and so on, that’s an often neglected area and I was very happy to see the author having placed an importance on it. Another good idea was to create a case-study chapter which combines all the knowledge and discusses it thoroughly.

All in all, it’s a good book written by a practitioner for practitioners, full of diagrams and examples that by necessity are simple yet don’t feel contrived and it’s certainly not something you need to plough through, it does a very good job of introducing the reader to Spring Python and showing how to use its pluggable components to write interesting and useful applications solving real-world problems. If I were to pick a nit I’d only say that it would be nice if the second edition contained at least a couple of examples of using Spring Python’s IoC YamlConfig, but that’s only a matter of syntax so the issue is really minor.

@fourthrealm

Share
Categories: Software Tags: ,

ToStringBuilder.reflectionToString in Python

August 20th, 2010 Comments off

I use Apache Commons ToStringBuilder.reflectionToString whenever I code in Java and thought it would come handy to have a similar construct in Python. What I’ve finally come up with is called make_repr and may be downloaded here along with a simple usage example (it’s all Python license).

So how does it work? Say you have a class like the one below

class Foo(object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

and want to add a nice string representation of it. You could carefully pick the attributes manually and add them to the __repr__ method or you can use make_repr to achieve what follows (I’m assuming make_repr has been saved to a util.py module):

from util import make_repr
 
class Foo(object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
 
    def __repr__(self):
        return make_repr(self)
 
print Foo(1,2,3)

Which will show <Foo at 0x7f2ed9873510 x=[1] y=[2] z=[3]> in the terminal. Callable objects won’t be returned and by the default attributes shown will be sorted alphabetically while any attributes with names starting with a double underscore will be ignored. Another customization option is to_avoid_list – if there’s a class attribute which goes by the name it points to (by default it’s repr_to_avoid, it must be in iterable object, such as a tuple or a list whose elements are names of attributes that must not be returned on the output, like below:

from util import make_repr
 
class Foo(object):
 
    repr_to_avoid = ("x",)
 
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
 
    def __repr__(self):
        return make_repr(self)
 
print Foo(1,2,3)

Noticed that “x” wasn’t returned on the output?

Well, that’s about it, I hope you like the little tool :-)

Share
Categories: Software Tags: ,