Posts

Showing posts from May, 2010

list as default value in class __init__() - watchout for surprise

Note to self: don't use list as a default value in class methods if its going to be modified later, as it might cause some trouble. Just realized something: In [1]: class MyClass(object): ...: def __init__(self,data=[]): ...: self.data = data ...: ...: def addData(self,value): ...: self.data.append(value) ...: ...: In [2]: obj = MyClass() In [3]: obj.addData('hello') In [4]: obj.data Out[4]: ['hello'] In [5]: obj2 = MyClass() In [6]: obj2.data Out[6]: ['hello'] In [7]: obj3 = MyClass([]) In [8]: obj3.data Out[8]: [] In [12]: def myfunc(val,data=[]): ....: data.append(val) ....: return data ....: In [13]: myfunc(1) Out[13]: [1] In [14]: myfunc(2) Out[14]: [1, 2] In [15]: myfunc(3) Out[15]: [1, 2, 3] At first i thought its a bug/weirdness, but after banging my head a bit to wall and a long stare at the ceiling, it made sense. During class definition, the

Using deliverance as WSGI pipeline to elegantly theme a repoze.bfg project

I was looking around for template inheritance solution for repoze.bfg, as it seems like by default, BFG does not have context view traversal support to acquire template macros. In Plone/Grok template inheritance can be done by registering main_template as a view, and using a template that fill the slots in main_template such as this: <html xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:metal="http://xml.zope.org/namespaces/metal" metal:use-macro="context/main_template/macros/master"> <head>&lt/head> <body> <div metal:fill-slot="main"> Some content </div> </body> </html> However, by default, repoze.bfg does not support getting view macro from a context. So a PageTemplate object need to by passed by a view in order to use the macro from the template. Eg: from repoze.bfg.chameleon_zpt import get_template def my_view(context,request): main_template = get_template(

repoze.bfg - Introduction and bootstrapping it on buildout

2 months without a post here .. T_T ... must .. restart .. blogging ... Anyway, I've been poking around with repoze.bfg since last week and growing to love it by the days. Been thinking to post a blog on getting started on it but was a bit lazy .. until I saw lowkster's post about bfg at planet.foss.org.my a few days ago. Full documentation at http://docs.repoze.org/bfg A little review I love Zope Component Architecture because the modularity/reusability it offers, but Bluebream , Grok and Zope2 feels a bit big for simple web apps or for introducing ZCA to new people. Then I saw BFG. BFG simplifies many concepts which I'm familiar with in Zope2/Zope3, and it does it without overwhelming developers with other parts of the framework. Not endorsing any type of storage backend, and giving an option between URL routing and traversal or a mix of both is a plus. The initial base code is simple and straightforward, and there are no need to subclass any parent class w