New theme

Posted on Monday, February 1st, 2010 under ,

Got myself a new blog theme. The old one was getting…well…old. The design was made by Andrei Chirea. It’s still in beta. Some things may work “funny”, other may not work at all. Should work in Internet Explorer.

Suggestions are welcome!

My Vim configuration

Posted on Tuesday, January 26th, 2010 under , ,

Vim is the editor of the Gods. The Elder Gods. It’s by far the best text editor I’ve ever encountered. Although I’m not a “vim purist” (no hjkl ;) ), I’m fascinated with its power. And there are always new things one can learn about it. It never gets old.

For instance, today the head of my company’s IT department showed me the following cool trick: if you have an XML file loaded in a buffer and you want to have it properly indented, you just hit Escape in your Vim editor and type

:% !xmllint --format %

… and…Magic!

Anyway, lately I’ve spent some time puting together a list of very useful plugins for PHP/Zend Framework and python/django development in order to speed things up and become more productive. My (g)Vim configuration currently uses:

And a custom gvimrc file. If you want to give it a try, check out my GitHub repository at http://github.com/motanelu/GVim-configuration. Installation is very simple, just follow the instructions below (PS: I’m using GVim. If you’re using Vim replace gvimrc with vimrc):

cd ~/
mv .vim .vim.bak
mv .gvimrc .gvimrc.bak
git clone git://github.com/motanelu/GVim-configuration.git
mv GVim-configuration .vim
ln -s ~/.vim/gvimrc ~/.gvimrc

Post a comment and tell me if you find it useful. If you’re a Zend Framework user, have a look over the snippets.

Django “anonymous_required” decorator

Posted on Wednesday, January 6th, 2010 under , ,

I like Django’s login_required decorator. It’s a clean and simple way to allow and/or deny un-logged-in users to access parts of the website. But I also felt the need for a decorator to allow me to restrict access to some views only to non logged-in users. For instance, if an user in logged in, it should be denied access to views like /accounts/register or /accounts/login and redirected to his/her profile.

I’ve looked for one on the web, but couldn’t find anything suitable to my needs, so I’ve wrote my own:

from django.http import HttpResponseRedirect
 
def anonymous_required( view_function, redirect_to = None ):
    return AnonymousRequired( view_function, redirect_to )
 
class AnonymousRequired( object ):
    def __init__( self, view_function, redirect_to ):
        if redirect_to is None:
            from django.conf import settings
            redirect_to = settings.LOGIN_REDIRECT_URL
        self.view_function = view_function
        self.redirect_to = redirect_to
 
    def __call__( self, request, *args, **kwargs ):
        if request.user is not None or request.user.is_authenticated():
            return HttpResponseRedirect( self.redirect_to ) 
        return self.view_function( request, *args, **kwargs )

It’s also available on Django Snippets. Its usage is quite simple:

@anonymous_required
def my_view( request ):
    return render_to_response( 'my-view.html' )

That’s about it!

Changing the time zone in Ubuntu

Posted on Monday, January 4th, 2010 under , ,

Ubuntu This one is from the “d’oh” category. As recently I’ve moved to Barcelona and since Bucharest and Barcelona are in different time zones, I wanted to set my system’s time an hour back, in order to display the correct time.

I went the easy way: right clicked on the clock in the upper right, selected “Adjust date and time” and made the modifications. After a few minutes the clock was showing Bucharest’s time and my changes were overwritten. I though I must have imagined setting the clock right in the first place – since I “tasted” Moritz thoroughly – and did it again.

I’ve ignored the clock for a while and, later on, when I looked at it, it was displaying Bucharest’s time again :(

D’oh! Ubuntu queries it’s time servers from time to time and sets the hour according to the information retrieved from these servers. And since my timezone was still set to Europe/Bucharest, on each update my system was receiving the time for that area. I’ve changed the timezone accordingly and everything started working. Magic!

To change the timezone of on you Ubuntu system go to System > Administration > Time and date. Or, if you’re a console freak, just punch in:

sudo dpkg-reconfigure tzdata

in order to set the correct time zone.

Django configuration file

Posted on Monday, December 28th, 2009 under , ,

django web frameworkI’ve been using Django for quite some time now and I kind of like it. It provides a fast – really fast – and clean way of doing things. When I first started with Django, I’ve used it mainly for simpler projects and kept the larger, more complicated ones on Zend Framework. That’s because I feel much more comfortable with PHP and Zend Framework rather than python and Django.

But, lately, I’ve used django for more ambitious projects and some obvious flaws began to annoy me. The first thing – the one this entry’s about – is the configuration file. Django comes with a settings.py file in which all the settings are being held, without any regard to their purpose.

Environment settings like database connection strings, file system paths and debuging are mixed together with application settings like what middleware classes are used, context processors and so on. So I’ve decided to split the configuration file in two, like in the following example:

local.py

This file holds the host related configuration and each instance of the application should have its own local.py file. One for development, one for testing, one for staging, one for production and so on. This file should not be included in the source control repository, so add a svn/git/whatever ignore on it.

# debug settings
DEBUG = True
TEMPLATE_DEBUG = DEBUG
 
# database configuration
DATABASE_ENGINE = ''
DATABASE_NAME = ''
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''
 
# media root ex: /var/www/my-project/media
MEDIA_ROOT = ''
 
# media url ex: media.my-project.tld
MEDIA_URL = ''
 
# admin media url ex: /media
ADMIN_MEDIA_PREFIX = '/media/'
 
# application's secret key
SECRET_KEY = 'mY-S3Cr3t-K3y-m|_|st-b3-un1QuE-4nD-h4rD-t0-GueSs'

settings.py

This file holds the application related configurations and should be included in the version control system.

import os
 
# root directory for the project
ROOT_DIR = os.path.realpath( os.path.dirname( __file__ ) )
 
ADMINS = (
	( 'root', 'root@project.tld' ),
)
 
# import all the local settings
from local import *
 
MANAGERS = ADMINS
TIME_ZONE = 'Europe/Bucharest'
 
LANGUAGE_CODE = 'en-us'
 
SITE_ID = 1
 
USE_I18N = True
 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
)
 
MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
)
 
ROOT_URLCONF = 'my-project.urls'
 
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
)
 
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)
 
# template directories
TEMPLATE_DIRS = (
    os.path.join( ROOT_DIR, 'templates' ),
    os.path.join( ROOT_DIR, 'other', 'template', 'path' ),
)
 
# django debug toolbar configuration
INTERNAL_IPS = ( '127.0.0.1' )
if DEBUG:
    MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware', 
    INSTALLED_APPS += 'debug_toolbar',

The last bit is because I usually use in development the incredibly useful Django debug_toolbar, and of course, I want it turned off on the live environment.

Gworld

Posted on Thursday, December 17th, 2009 under ,

google Is Google taking over the web? It seems so. Lately Google launched its own services to compete with other companies / open source associations on all fronts – DNS, source code hosting, url shortening, instant messaging, email. You name it and Google has it. Of course, not all Google’s services are top of the line in their field, but, if you look at it as a whole, Google is slowly taking over the word.

I’ve compiled a list with all the stuff I usually do online, presented Google’s service and the service I’m using:

Action Google’s offer I use
browse the web Chrome Firefox
search for the information you want Google Google
Upload / store images Picasa Facebook / Flickr
“url shortening” service goo.gl re.pl.ly
blog Blogger Wordpress on private server
read email Gmail / Wave private mail server
chat with friends Gtalk Yahoo! Messenger / Skype
search for places on the map Google Maps Yahoo! Maps
watch online videos YouTube YouTube
DNS service Google Dns OpenDNS
cloud computing Google App Engine none – I didn’t need that kind of power so far, but Google’s alternative is great)
source code hosting/versioning Google Code github
hosting services for popular js frameworks (jQuery, etc.) Google Code local deployment / Google Code (scarcely)
document sharing/collaborating Google Docs Google Docs / private Wiki
groups / mailing lists Google Groups none
translation services Google Translate Google Translate

As it turns out, I’m not addicted to Google! I use some of Google’s services, but the only one I couldn’t live without is the search engine. But my question is what will happen when/if Google will control most of the services on the web? I mean when 90% of the users will use Google’s alternatives, will Google become the new Microsoft? Or they will keep to their “don’t be evil” creed?

What Google services do you use and what are the alternatives?

Help saving MySQL

Posted on Monday, December 14th, 2009 under , ,

mysql-logo As you probably know, the most popular Open Source RDBMS, MySQL, is being acquired by Oracle, which – some say – will most likely mean the end of the open source project. MySQL has been eroding Oracle’s profits for quite some time now, and there’s a fat chance that Oracle will kill MySQL in order to keep its high profits.

Why should we care? Well, without MySQL, the web would be much different than it is today. Popular open source applications like Wordpress and Drupal, millions of websites and applications rely on MySQL. A lot of start-up companies use MySQL in order to cut down costs and not invest in expensive, proprietary RDBMSs.

But we, the people, have the power to stop it by writing to the EC in order to block the transaction. Read what Michael “Monty” Widenius, MySQL’s creator, has to say on the matter: Help saying MySQL.

PS: Do you know the difference between God and Larry Ellison? God knows He’s not Ellison…

Unit testing & TDD

Posted on Wednesday, December 2nd, 2009 under , ,

Last week, I had a discussion with Ionut – my colleague at Ninespices – about a piece of code I had wrote in one of our applications. The piece was meant to log the current user’s actions to the database for future reference. It looked something like this (more or less, the actual code isn’t that important):

protected function _log($info) {
    $identity = Zend_Auth::getInstance()->getIdentity();
    $this->save(
        array(
            'user_id' => $identity->id,
            'info' => $info,
        )
    );
}

Ionut said to me: “I don’t like it! It’s hardcoded!”. I said that it’s not, as I don’t hardcode anything, since we all know that each time one hardcodes something in a software application, God kills a kitten.

But Ionut asked me a simple question: “How do you test it? How do you mock Zend_Auth?”. For the record, Ionut is the Test Driven Development guy of the team. He always writes tests for his applications, whereas I don’t, since my code “just works”…well, kind of…sometimes :) . The point he made is interesting: how do you mock Zend_Auth? The answer is obvious: D’oh, you don’t!

In order to be able to properly use unit testing to test a piece of code, that code must be written in a manner that allows unit testing. To make a method “testable”, you must not instantiate other objects, call other classes’ static methods and so on. For instance, let’s consider a piece of code that looks like this:

public function methodThatRequiresTesting($input) {
    // get data on the current logged user
    $identity = Zend_Auth::getInstance()->getIdentity();
    $userId = $identity->id;
 
    // instantiate a helper class
    $helper = new MyLibrary_Helpers_Foo();
 
    // do whatever
}

It’s quite obvious that this code isn’t “unit testing friendly” as you cannot mock the helper or Zend_Auth. The proper way to write it in order to be “unit-testable” is either:

public function methodThatRequiresTesting($input, $userId, MyLibrary_Helpers_Foo $helper) {
    // do whatever
}

…or…

public function methodThatRequiresTesting($input) {
    // get the user id
    $userId = Zend_Registry::get('userId');
 
    // get the helper
    $helper = Zend_Registry::get('Helpers/Foo');
 
    // do whatever
}

This way, one can easily add mocks instead of the actual objects, isolate the functionality of this particular method and test it. Of course, the first “testable” code is wrong, as it breaks separation of concerns. A method should have input and output, and any additional entities (functions, classes) used inside the method’s body in order to compute the output from the input should be encapsulated within the method. Receiving these entities as input is a very bad practice, for obvious reasons.

The second code example is better from an architectural point of view, but it can be quite memory intensive, as one might instantiate a lot of helpers and place them in the registry without ever using them, thus wasting valuable system resources.

That very night, Ionut & I went to the 26th edition of Wurbe, where one the the topics was – beside the crappy beer – Test Driven Development. There was a somewhat heated debate on whether to use TDD or not, unit testing, functional (system) testing and so on. I particularly liked one of the opinions expressed there – by a guy who’s name I forgot :) – on functional testing. There’s no need to write unit tests for all your components. Only write tests for those components which contain complicated algorithms, so each time you refactor the code and optimise the algorithm, you know if what you did is good or not. And write a system test, in the end, to test the whole application.

It makes more sense to me this way.

Zend_Cache Dummy backend

Posted on Friday, November 27th, 2009 under , ,

I was working today on a caching system for one of our projects written using Zend_Framework. And, of course, in order to see what I was doing I needed to turn off the cache in the development stage. A task that proved to be more complicated that it would seem, as Zend_Cache doesn’t have a “turn off” feature. As pointed out by robo in this comment, Zend_Cache *does* have a turn off feature. Documented. There! On the first paragraph of the first page! Man l feel dumb :( Well…live and learn. Special thanks go to Robo47 for pointing out my error.

Setting the “caching” attribute to false in the caching frontend will stop Zend_Cache. Use this in your bootstrap:

/**
 * inits the cache
 *
 * @return void
 */
protected function _initCache() {
    $frontendOptions = array(
        'lifeTime'                => 60,
        'automatic_serialization' => true,
    );
 
    $backendOptions = array(
        'cache_db_complete_path'  => APPLICATION_PATH . '/cache/cache.sqlite',
        'automatic_vacuum_factor' => 5,
 
    );
 
    if(APPLICATION_ENV != 'development') {
        $frontendOptions['caching'] = false;
    }
 
    $cache = Zend_Cache::factory('Core', 'Sqlite', $frontendOptions, $backendOptions);   
    Zend_Registry::set('cache', $cache);
}

Simple as that. But if you don’t have anything better to do now, you can read original post (non edited) below and see how complicated a simple thing can get:

After looking dumb to the screen for several minutes – hoping that the problem will fix itself or something – I came to the conclusion that the easiest way to turn off the cache would be, in my case, to use a dummy cache backend, that doesn’t cache anything and always returns false when you try to fetch an object from it. Just like django has.

But Zend Framework doesn’t have such a class. It has a caching backend class called Zend_Cache_Backend_Test, but its method return stuff like ‘foo’ and ‘bar’. Really! They do! I don’t get this class’s purpose, as it’s clear that it can’t be used for testing. So I’ve decided to write my own dummy caching backend class and share it with my readers. It can be downloaded directly from my GitHub repository, by following this link Generic_Cache_Dummy.php.

The usage is quite simple, just add this to your bootstrap:

/**
 * inits the cache
 *
 * @return void
 */
protected function _initCache() {
    $frontendOptions = array(
        'lifeTime'                => 60,
        'automatic_serialization' => true,
    );
 
    $backendOptions = array(
        'cache_db_complete_path'  => APPLICATION_PATH . '/cache/cache.sqlite',
        'automatic_vacuum_factor' => 5,
 
    );
 
    if(APPLICATION_ENV != 'development') {
        $cache = Zend_Cache::factory('Core', 'Sqlite', $frontendOptions, $backendOptions);
    }
    else {
        $dummyBackend = new Generic_Cache_Dummy();
        $cache = Zend_Cache::factory('Core', $dummyBackend, $frontendOptions, $backendOptions);
    }
    Zend_Registry::set('cache', $cache);
}

…and, whenever you need to save/retrieve something from the cache, just do…

$cache = Zend_Registry::get('cache');
if(!($object = $cache->load($cacheKey))) {
	// bla bla bla...fetch object
	$cache->save($object, $key);
}
 
return $object;

Happy caching!

Backslash hell

Posted on Thursday, November 26th, 2009 under , , ,

How do you write a regular expression that will match \\test – two backslashes followed by some text in PHP? Well, obviously you’ll try something like this:

echo preg_match('/^\\([a-z]+)/', $word) ? 'match' : 'no match';

…but it won’t work. Furthermore, it will yield an error saying something like:

Compilation failed: unmatched parentheses at offset …

And it makes sense, since the backslash has a special meaning, as it’s the escape character, to give it a literal meaning you have to escape it. Like such:

echo preg_match('/^\\\\([a-z]+)/', $word) ? 'match' : 'no match';

…each of the two backslashes is escaped by another backslash placed in front of it. But this also doesn’t work :) The correct way to do it is:

echo preg_match('/^\\\\\\\\([a-z]+)/', $word) ? 'match' : 'no match';

Yeap. That’s right! With 8 backslashes. For each backslash you wanted matched, you need to add 4 in the regular expression. That’s because the backslash is considered to be an escape character by both PHP’s parser and its regexp engine. So, if you type a backslash in PHP, it’s considered and escape character by PHP’s parser.

If you write two backslashes in PHP, this construct will be interpreted as a literal backslash by PHP’s paser and sent to the regexp engine as an escape character. That’s right: ‘\\’ in PHP means a ‘\’ for the regexp engine. So, in order to pass a literal backslash to the regexp engine, you need to add 4 backslashes in PHP.

This is what I call backslash hell :(