python

PyCon Italia 4 is over!

0

At the end we succeeded. PyCon 4 is finished. Everything was great!

Many interesting talks, some of which I could not follow because of my commitment to the Develer SRL stand.

I want to thank all the staff at PyCon. And a big thank you goes to Develer that allowed me to attend this great event.

See you next year at EuroPython!

The end of PyConThe real end of PyCon

Simple trick for deploy friendly custom settings/behaviors

0

I think that only a few of you know that I’m also a web developer (with poor results…). In this field, my favorite technology is django, a complex and complete web framework written in python that permits the creation of rich web applications.

But this is only partially related to what I’m going to explain you. A django project was only the first project in which I used this approach. Now I use it in most of my python projects.

So, what I’m going to explain? I’m going to explain the simplest and painless way to modify an application’s settings and some behaviors without touching its code. This means that you can do all the tests you want, without changing the deploy settings. A colleague of mine taught me this trick, as a wise Zen master, so that I could teach it to my adepts and continue the tradition.

  • The first thing you need is a place you can use as hook. In django, the settings.py module (which contain all the application-related settings) is the right place.
  • Then you have to add a few lines to your file. Something like this:
    # ...
    # The content of the module ends here!
    
    try:
        import settings_locale  # You will redefine the settings in this module...
    except ImportError:
        pass
    
    # That's all. Really.
    

    Obviously, any name can be fine.

  • Now you have to create the other module file in the same directory of the previous edited one (in this example settings_locale.py). This file can contain everything you want (ie redefined variables, redefined functions, monkey-patches for classes or functions in the application…).

This trick is pretty simple, but the result is that you have a very useful module that could override about everything in your application, for debug or testing purpose. And when you need the real application behavior all you have to do is to remove this module. And all just works.

A great ‘thank you’ to dvd, that helped me in web development projects and taught me practically everything I know about webapp’s development process.

Go to Top