<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tudor Barbu&#039;s professional blog &#187; python</title>
	<atom:link href="http://blog.motane.lu/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.motane.lu</link>
	<description>Ramblings about software development</description>
	<lastBuildDate>Thu, 02 Feb 2012 17:38:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Iterate thru dates in Python</title>
		<link>http://blog.motane.lu/2010/02/19/iterate-thru-dates-in-python/</link>
		<comments>http://blog.motane.lu/2010/02/19/iterate-thru-dates-in-python/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 14:06:14 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=1317</guid>
		<description><![CDATA[Few days ago I was working on some python scripts that needed to iterate back and forth through calendar dates. Working with dates in python is pretty easy, due to its datetime package. Basically is like this: This works, but is somewhat lame and not quite reusable. The most &#8220;python-ish&#8221; way to do it is [...]]]></description>
			<content:encoded><![CDATA[<p>Few days ago I was working on some python scripts that needed to iterate back and forth through calendar dates. Working with dates in python is pretty easy, due to its <a href="http://docs.python.org/library/datetime.html" title="datetime package entry in the manual" class="outgoing">datetime</a> package. </p>
<p>Basically is like this:</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python

import datetime

start_date = datetime.date( year = 2010, month = 2, day = 1 )
end_date = datetime.date( year = 2010, month = 1, day = 1 )

list = []

if start_date &lt;= end_date:
    for n in range( ( end_date - start_date ).days + 1 ):
        list.append( start_date + datetime.timedelta( n ) )
else:
    for n in range( ( start_date - end_date ).days + 1 ):
        list.append( start_date - datetime.timedelta( n ) )

for d in list:
    print d
</pre>
<p>This works, but is somewhat lame and not quite reusable. The most &#8220;python-ish&#8221; way to do it is to create a generator function that will <a href="http://docs.python.org/reference/simple_stmts.html#the-yield-statement" title="Generator functions in python's docs" class="outgoing">yield</a> the current date and can be used in a <em>for</em> loop. So I did it:</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python
import datetime

def daterange( start_date, end_date ):
    if start_date &lt;= end_date:
        for n in range( ( end_date - start_date ).days + 1 ):
            yield start_date + datetime.timedelta( n )
    else:
        for n in range( ( start_date - end_date ).days + 1 ):
            yield start_date - datetime.timedelta( n )

start = datetime.date( year = 2010, month = 2, day = 1 )
end = datetime.date( year = 2010, month = 1, day = 1 )

for date in daterange( start, end ):
    print date
</pre>
<p>Much more elegant <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2010/02/19/iterate-thru-dates-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django &#8220;anonymous_required&#8221; decorator</title>
		<link>http://blog.motane.lu/2010/01/06/django-anonymous_required-decorator/</link>
		<comments>http://blog.motane.lu/2010/01/06/django-anonymous_required-decorator/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 11:12:20 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[django decorators]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=1269</guid>
		<description><![CDATA[I like Django&#8217;s login_required decorator. It&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>I like Django&#8217;s login_required decorator. It&#8217;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.</p>
<p>I&#8217;ve looked for one on the web, but couldn&#8217;t find anything suitable to my needs, so I&#8217;ve wrote my own:</p>
<pre class="brush: python; title: ; notranslate">
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 and request.user.is_authenticated():
            return HttpResponseRedirect( self.redirect_to )
        return self.view_function( request, *args, **kwargs )
</pre>
<p>It&#8217;s also available on <a href="http://www.djangosnippets.org/snippets/1849/" title="anonymous decorator on django snippets" class="outgoing">Django Snippets</a>. Its usage is quite simple:</p>
<pre class="brush: python; title: ; notranslate">
@anonymous_required
def my_view( request ):
    return render_to_response( 'my-view.html' )
</pre>
<p>That&#8217;s about it!</p>
<p><!-- BBAGUYPYCUC7 --></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2010/01/06/django-anonymous_required-decorator/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Django configuration file</title>
		<link>http://blog.motane.lu/2009/12/28/django-configuration-file/</link>
		<comments>http://blog.motane.lu/2009/12/28/django-configuration-file/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 11:01:47 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=1245</guid>
		<description><![CDATA[I&#8217;ve been using Django for quite some time now and I kind of like it. It provides a fast &#8211; really fast &#8211; and clean way of doing things. When I first started with Django, I&#8217;ve used it mainly for simpler projects and kept the larger, more complicated ones on Zend Framework. That&#8217;s because I [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.motane.lu/wp-content/uploads/2009/07/django-logo-negative.png" alt="django web framework" title="django web framework" width="200" height="91" class="alignleft size-full wp-image-882" />I&#8217;ve been using <a href="http://www.djangoproject.com/" title="Django's project homepage" class="outgoing">Django</a> for quite some time now and I kind of like it. It provides a fast &#8211; really fast &#8211; and clean way of doing things. When I first started with Django, I&#8217;ve used it mainly for simpler projects and kept the larger, more complicated ones on <a href="http://framework.zend.com" title="Zend Framework's page">Zend Framework</a>. That&#8217;s because I feel much more comfortable with PHP and Zend Framework rather than python and Django. </p>
<p>But, lately, I&#8217;ve used django for more ambitious projects and some obvious flaws began to annoy me. The first thing &#8211; the one this entry&#8217;s about &#8211; is the configuration file. Django comes with a <a href="http://docs.djangoproject.com/en/dev/topics/settings/" title="Django's manual entry on settings.py" class="outgoing">settings.py</a> file in which all the settings are being held, without any regard to their purpose. </p>
<p>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&#8217;ve decided to split the configuration file in two, like in the following example:</p>
<h2><span>local.py</span></h2>
<p>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.</p>
<pre class="brush: python; title: ; notranslate">
# 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'
</pre>
<h2><span>settings.py</span></h2>
<p>This file holds the application related configurations and should be included in the version control system.</p>
<pre class="brush: python; title: ; notranslate">
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',
</pre>
<p>The last bit is because I usually use in development the incredibly useful Django <a href="http://github.com/robhudson/django-debug-toolbar" title="Django debug_toolbar on Github" class="outgoing">debug_toolbar</a>, and of course, I want it turned off on the live environment. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/12/28/django-configuration-file/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Advertising blog entries on Pidgin&#8217;s status</title>
		<link>http://blog.motane.lu/2009/08/21/advertising-blog-entries-on-pidgins-status/</link>
		<comments>http://blog.motane.lu/2009/08/21/advertising-blog-entries-on-pidgins-status/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 14:36:12 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pidgin]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=967</guid>
		<description><![CDATA[I wanted to write this post ever since I&#8217;ve read Radu&#8217;s Fortune and Pidgin&#8217;s status on Ubuntu post. Radu&#8217;s approach is quite lame and hard to use, because it relies on the user exporting a SQL dump file every time he posts something on the blog. And really, do you need *all* the posts in [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to write this post ever since I&#8217;ve read Radu&#8217;s <a href="http://radu.cotescu.com/2009/08/07/fortune-and-pidgins-status-on-ubuntu-linux/" title="Fortune and Pidgin's status on Ubuntu" class="outgoing">Fortune and Pidgin&#8217;s status on Ubuntu</a> post. Radu&#8217;s approach is quite lame and hard to use, because it relies on the user exporting a SQL dump file every time he posts something on the blog. </p>
<p>And really, do you need *all* the posts in the database? Do you really want to advertise blog entries from 2 years ago? Come on! </p>
<p>My idea is much better&#8230;obviously <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> &#8230;and it goes like this: parse the blog&#8217;s RSS and take &#8220;inspiration&#8221; from there for the statuses. The weapon of choice for this task is python with its <a href="http://www.feedparser.org/" title="feedparser project's webpage" class="outgoing">feedparser</a> library.</p>
<p>First, some prerequisites:</p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install python-feedparser
</pre>
<p>&#8230;and then, the python script:</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/python
import feedparser
import random
import os

# feed url
FEED = 'http://feeds2.feedburner.com/motanelu'

feed = feedparser.parse( FEED )
index = random.randrange(0, len( feed['items'] ) - 1 )
status = 'purple-remote &quot;setstatus?status=available&amp;message=%s %s&quot;' % ( feed['items'][index].title, feed['items'][index].link )
os.system( status )

# EOF
</pre>
<p>I consider this approach better than Radu&#8217;s, because it doesn&#8217;t require exporting the database or messing around with fortune. Read this <a href="http://earlruby.org/2008/08/update-pidgin-status-using-cron/" title="Update Pidgin IM status on Ubuntu using cron" class="outgoing">post</a> to see how to update Pidgin&#8217;s status using cron. And enjoy <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/08/21/advertising-blog-entries-on-pidgins-status/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Downloading a page&#8217;s content with python and WebKit</title>
		<link>http://blog.motane.lu/2009/07/07/downloading-a-pages-content-with-python-and-webkit/</link>
		<comments>http://blog.motane.lu/2009/07/07/downloading-a-pages-content-with-python-and-webkit/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 11:35:40 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gtk]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pywebkitgtk]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=896</guid>
		<description><![CDATA[I&#8217;ve been bragging with this post for quite some time now. Well, I won&#8217;t do that any more, because it seems that pywebkitgtk isn&#8217;t the best way to to things out there and that my first solution to the problem sucks Yes, the sad truth&#8230; Yesterday, I tried to put the application on the server [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been bragging with <a href="http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript-from-python/" title="Pywebkitgtk - execute Javascript from python ">this post</a> for quite some time now. Well, I won&#8217;t do that any more, because it seems that <a href="http://code.google.com/p/pywebkitgtk/" title="PyWebkitGtk's homepage on Google Code" class="outgoing">pywebkitgtk</a> isn&#8217;t the best way to to things out there and that my first solution to the problem sucks <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  Yes, the sad truth&#8230;</p>
<p>Yesterday, I tried to put the application on the server &#8211; a Debian Lenny machine without X. And this is where it all broke down. I don&#8217;t want to install Xorg on this machine just so that a small script will work, so I&#8217;ve looked for alternatives ways to run the script. One of the first alternatives I&#8217;ve found was <a href="http://www.xfree86.org/4.0.1/Xvfb.1.html" title="Xvfb manual page" class="outgoing">Xvfb</a>. which, according to <a href="http://en.wikipedia.org/wiki/Xvfb" title="Xvfb entry in Wikipedia" class="outgoing">Wikipedia</a>&#8230;</p>
<blockquote><p>In the X Window System, Xvfb or X virtual framebuffer is an X12 server that performs all graphical operations in memory, not showing any screen output. From the point of view of the client, it acts exactly like any other server, serving requests and sending events and errors as appropriate. However, no output is shown. This virtual server does not require the computer it is running on to even have a screen or any input device. Only a network layer is necessary.</p></blockquote>
<p>&#8230;should get the job done. But it didn&#8217;t. While running under Xvfb, GTK kept throwing segmentation faults and crashing the whole script. </p>
<p>I was faced with the following decision: spend hours or perhaps days trying to see why Xvfb and GTK make such uneasy bed fellows or rewrite a 50 lines crawler script. I knew from my previous research on the matter that python also had binding with WebKit and Qt, so I&#8217;ve gave it a try. And it proved to be a much better solution than GTK. </p>
<h2><span>QT to the rescue</span></h2>
<p>Although I&#8217;m a Gnome/GTK fan, I must admit that Qt is a much better candidate for this job. First of all, it has <a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/classes.html" title="PyQt4 docs" class="outgoing">extensive documentation</a>, whereas pywebkitgtk&#8217;s is scarce. And, the second being that it works in my particular case, which can prove to be a huge advantage <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Under Ubuntu and Debian, you can install the package by simply typing&#8230;</p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install python-qt4 libqt4-webkit
</pre>
<p>&#8230;in the console. And you&#8217;re done. You can run applications with python and Qt. The rewritten crawler code is:</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python

import sys
import signal

from optparse import OptionParser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

class Crawler( QWebPage ):
	def __init__(self, url, file):
		QWebPage.__init__( self )
		self._url = url
		self._file = file

	def crawl( self ):
		signal.signal( signal.SIGINT, signal.SIG_DFL )
		self.connect( self, SIGNAL( 'loadFinished(bool)' ), self._finished_loading )
		self.mainFrame().load( QUrl( self._url ) )

	def _finished_loading( self, result ):
		file = open( self._file, 'w' )
		file.write( self.mainFrame().toHtml() )
		file.close()
		sys.exit( 0 )

def main():
	app = QApplication( sys.argv )
	options = get_cmd_options()
	crawler = Crawler( options.url, options.file )
	crawler.crawl()
	sys.exit( app.exec_() )

def get_cmd_options():
	&quot;&quot;&quot;
		gets and validates the input from the command line
	&quot;&quot;&quot;
	usage = &quot;usage: %prog [options] args&quot;
	parser = OptionParser(usage)
	parser.add_option('-u', '--url', dest = 'url', help = 'URL to fetch data from')
	parser.add_option('-f', '--file', dest = 'file', help = 'Local file path to save data to')

	(options,args) = parser.parse_args()

	if not options.url:
		print 'You must specify an URL.',sys.argv[0],'--help for more details'
		exit(1)
	if not options.file:
		print 'You must specify a destination file.',sys.argv[0],'--help for more details'
		exit(1)

	return options

if __name__ == '__main__':
	main()
</pre>
<p>This time it really works. I feel warm and fuzzy on the inside <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/07/07/downloading-a-pages-content-with-python-and-webkit/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Django&#8230;for the very first time</title>
		<link>http://blog.motane.lu/2009/07/02/django-for-the-very-first-time/</link>
		<comments>http://blog.motane.lu/2009/07/02/django-for-the-very-first-time/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 10:22:03 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=864</guid>
		<description><![CDATA[This is a post I&#8217;ve wanted to write for quite some time now, but there is so much say that I couldn&#8217;t get the time to write it all down. So I&#8217;ve decided to split the first impression on django topic into smaller articles, this being the first post from a longer django series. I [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.motane.lu/wp-content/uploads/2009/07/django-logo-negative.png" alt="django web framework" title="django web framework" width="200" height="91" class="alignright size-full wp-image-882" />This is a post I&#8217;ve wanted to write for quite some time now, but there is so much say that I couldn&#8217;t get the time to write it all down. So I&#8217;ve decided to split the first impression on django topic into smaller articles, this being the first post from a longer <a href="http://blog.motane.lu/tag/career/" title="Articles tagged django" class="outgoing">django</a> series.</p>
<p>I did some projects with python lately, and I like the language a lot, but none of them were involved building web pages. Some vim scripts, some automatization scripts or web crawlers. But not a single web script. Usually a company doesn&#8217;t switch easily from PHP to python, or to any other programming language, because of the costs involved (training, etc.), so it&#8217;s pretty hard to start a project on another framework. But here, things are a lot simpler. My job description consists of only 4 words: <em>get the sh*t done</em>. Aside from this, I have complete ownership of the projects I&#8217;m working on and I&#8217;m free to choose whatever technology I please.</p>
<h2><span>Prerequisites</span></h2>
<p><strong>Installing <a href="http://www.djangoproject.com/" title="django project" class="outgoing">django</a></strong> is a piece of cake. Just follow the instructions on their site and it works like a charm. If you&#8217;re using python 2.6, you will receive some warnings concerning the MySQLdb package which uses a deprecated package. Just ignore them, at least until you decide to upgrade to python 3.0 <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><strong>Preparing the editor.</strong> I use vim for editing and I needed something to help me out with code completion, mainly because I&#8217;m lazy and don&#8217;t like to type long name, and second because I don&#8217;t know all the django&#8217;s components&#8217; names by hard. I&#8217;ve tried pysmell, but it didn&#8217;t worked well, and, since it&#8217;s marked as experimental, I don&#8217;t think it is supposed to. So I&#8217;ve tried good ol&#8217; ctags. This is my recommendation. To create a ctags index, simply type:</p>
<pre class="brush: plain; title: ; notranslate">
ctags -R -f ~/.vim/tags/python.ctags /usr/local/lib/python2.6/dist-packages
</pre>
<p>and in Vim</p>
<p>set tags+=$HOME/.vim/tags/python.ctags </p>
<p>And voila. It works. If you&#8217;re using python 2.5 use the appropriate path. </p>
<h2><span>What I like about django</span></h2>
<p><strong>The admin.</strong> It rules, it&#8217;s simple to customise, it saves a lot of time. I love it. Back in the days, when I was using CakePHP, I&#8217;ve often wished that its scaffolding system did what django&#8217;s admin does now. I also love the <strong>authentication system</strong>. It is another well built component that saves a lot of time of routine work. <strong>The models</strong> &#8211; not having to write SQL statements by hand all the time gives me a hard-on. They also provide a logical separation between a single model object (which encapsulates a row in the table) and the statically declared information retrieval methods.</p>
<h2><span>What I don&#8217;t like about django</span></h2>
<p>The <strong>implementation of the MVC</strong> pattern is a little bit strange. They call it MVT (Model View Template), where &#8220;the view&#8221; is basically a controller&#8217;s action in the &#8220;traditional&#8221; MVC approach, and &#8220;the template&#8221; is what you&#8217;d expect to be the view. </p>
<p>It offers decent degree of logic separation, I don&#8217;t like it though that the action and method attributes are being set in the <del>view</del>&#8230;template. No one can tell me that where a form sends its data is presentation logic that should reside in a template. </p>
<p>The <strong>code generation scripts</strong> don&#8217;t always work in the way that you&#8217;d expect, for instance, if you change something in a model and run the </p>
<pre class="brush: bash; title: ; notranslate">
python manage.py sql myApp
python manage.py syncdb
</pre>
<p>sequence and the table already exists, it won&#8217;t update it. And I hate the <strong>template system</strong>. Really do. I don&#8217;t get its purpose &#8211; I don&#8217;t get any templating system&#8217;s purpuse &#8211; and some things in it are really weird. I mean <em>ifequal</em>&#8230;hello? Not exactly syntactic sugar <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<h2><span>Conclusion</span></h2>
<p>I like django, as it provides a quick and clean way of doing things. I will definitely use it in the future, possibly in my next project, so stay tuned for more <a href="http://blog.motane.lu/tag/django/" title="post entries labeled django">django</a> articles <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/07/02/django-for-the-very-first-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Pywebkitgtk &#8211; execute Javascript from python</title>
		<link>http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript-from-python/</link>
		<comments>http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript-from-python/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 09:21:06 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cross-browser]]></category>
		<category><![CDATA[gtk]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pywebkitgtk]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=821</guid>
		<description><![CDATA[Last week I&#8217;ve got a new assignment at my job: a crawler that was supposed to periodically visit some sites and download their content. Sounds simple, isn&#8217;t it? Well, it&#8217;s not. Mainly because we want to also get all the flash content and some of it is inserted with Javascript, via various libraries like SWFobject [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.motane.lu/wp-content/uploads/2009/06/python_logo_without_textsvg.png" alt="Python" title="Python" width="280" height="280" class="alignleft size-full wp-image-835" /> Last week I&#8217;ve got a new assignment at my job: a crawler that was supposed to periodically visit some sites and download their content. Sounds simple, isn&#8217;t it? Well, it&#8217;s not. Mainly because we want to also get all the flash content and some of it is inserted with Javascript, via various libraries like SWFobject or directly with document.write in some cases. I needed a snapshot of how the page actually looks like when the user is looking at it in a browser. </p>
<p>This meant that I had to get the content *after* all the javascripts contained in page finished execution. In developer language, this means after the window.onload event takes place. And, of course, I also needed a Javascript interpreter. So any attempt to use wget/cURL/file_get_contents was destined to fail from the start. I needed browser power <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So I&#8217;ve googled around for some.</p>
<p>The first thing I came across was using <a href="http://www.microsoft.com/com/default.mspx" title="Component Object Model on Microsoft.com" class="outgoing">COM</a> to connect to an Internet Explorer instance from python, use it to navigate back and forth and get the HTML content as it&#8217;s interpreted by IE&#8217;s engine. This had 3 major drawbacks:</p>
<ul>
<li>it requires Internet Explorer</li>
<li>it requires Microsoft Windows</li>
<li>it requires an opened IE window</li>
</ul>
<p>Since we want to migrate everything from our windows servers to linux, it would be pointless to go with this approach, since I&#8217;d have to rewrite in a month or so. Let aside the &#8220;lameness&#8221; of the technologies involved <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  And I&#8217;m looking for a solution that doesn&#8217;t require an opened browser window, <del datetime="2009-06-18T09:32:53+00:00">mainly because it should work on servers without X</del> because I don&#8217;t want to <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  (GTK doesn&#8217;t work without X &#8211; credits go to Alex Novac &#8211; and yes, it was retarded of me to think otherwise). </p>
<p>This solution wasn&#8217;t good enough, so I kept looking and came across the <a href="http://htmlunit.sourceforge.net/" title="HtmlUnit's page on SourceForge" class="outgoing">HtmlUnit</a> Java library. This library is used to write tests in Java for web based applications. Pretty cool. And not so much. Although Java was once my one true love, after all these years spent with scripting languages, declaring variables, compiling the code, writing only OOP code and so on seemed a little&#8230;unfamiliar. But it takes more than anApiWithReallyLongCamelCasedClassNames to stop me, so I&#8217;ve installed Eclipse and made some tests. Disappointing! The library isn&#8217;t very tolerant with messy HTML and Javascript, and, since nobody out there, in the real world, actually abides to W3C recommendations, this library it&#8217;s somewhat useless in my case. </p>
<p>The next thing I&#8217;ve tried was a solution based on python that relied on integration with Gecko via <a href="http://wiki.laptop.org/go/HulaHop" title="Hulahop wiki" class="outgoing">hulahop</a>. I must admit that I couldn&#8217;t get it to work under Ubuntu Jaunty Jackalope, due to incompatibilities in the system&#8217;s libraries. I&#8217;m sure that with enough time and patience, it can be  pursued to work. But, as I didn&#8217;t had any,  I&#8217;ve moved on and tried <a href="http://code.google.com/p/pywebkitgtk/" title="PyWebKitGtk's page on Google Code" class="outgoing">pywebkitgtk</a>. This proved to be quite okay (I&#8217;m not a Safari fan) and it worked out of the box. </p>
<p>After spending several days searching the web, reading articles and trying out different softwares, I decided to share my findings with the world and write a tutorial on <strong>how to get the content of a page in python *after* its javascript finished execution</strong>. Here it goes:</p>
<p>First of all, install pywebkitgtk. Under Ubuntu, you can do it directly from the repository:</p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install python-webkitgtk libwebkit-1.0-1 libwebkit-dev
</pre>
<p>&#8230;it will attempt to install a lot of other stuff, linked libraries and so on. Just say yes <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /><br />
After the installation is complete, it&#8217;s generally a good idea to test it! The following code should display a window with Google&#8217;s first page in it:</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python

import gtk
import webkit

window = gtk.Window()
view = webkit.WebView()
view.open('http://www.google.com')
window.add(view)
window.show_all()
window.connect('delete-event', lambda window, event: gtk.main_quit())

gtk.main()
</pre>
<p>&#8230;if it doesn&#8217;t, maybe you did something wrong. See if all the packages are in their place. For the conversation&#8217;s sake, let&#8217;s assume it worked move on. As I said in the first paragraph, I wan to load a webpage, wait for it to execute all the JS in it and take the generated HTML source. A strange problem with pywebkitgtk is that nor the WebView object, nor the encapsulated WebFrame object don&#8217;t have a &#8220;get_html()&#8221; method or something similar. Really, there is no clean way to get the site&#8217;s content. But, fortunately, on <a href="http://code.google.com/p/pywebkitgtk/wiki/HowDoI" title="pywebkitgtk's wiki" class="outgoing">pywebkitgtk&#8217;s wiki</a>. I&#8217;ve found this hack that does just that:</p>
<pre class="brush: python; title: ; notranslate">
class WebView(webkit.WebView):
    def get_html(self):
        self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
        html = self.get_main_frame().get_title()
        self.execute_script('document.title=oldtitle;')
        return html
</pre>
<p>It executes a javascript that takes the content of the whole document and stores it in the title. And since there is a get_title() method that returns the title&#8217;s content, this workaround gets the job done. Kind of lame, but it suffices. </p>
<p>As previously stated, in my application I didn&#8217;t want to have a browser window open and with GTK is possible to run your app without calling window.show() or window.show_all(). Long story short, this is how I did it:</p>
<pre class="brush: python; title: ; notranslate">
#!/usr/bin/env python
import sys, threads # kudos to Nicholas Herriot (see comments)
import gtk
import webkit
import warnings
from time import sleep
from optparse import OptionParser

warnings.filterwarnings('ignore')

class WebView(webkit.WebView):
	def get_html(self):
		self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
		html = self.get_main_frame().get_title()
		self.execute_script('document.title=oldtitle;')
		return html

class Crawler(gtk.Window):
	def __init__(self, url, file):
		gtk.gdk.threads_init() # suggested by Nicholas Herriot for Ubuntu Koala
		gtk.Window.__init__(self)
		self._url = url
		self._file = file

	def crawl(self):
		view = WebView()
		view.open(self._url)
		view.connect('load-finished', self._finished_loading)
		self.add(view)
		gtk.main()

	def _finished_loading(self, view, frame):
		with open(self._file, 'w') as f:
			f.write(view.get_html())
		gtk.main_quit()

def main():
	options = get_cmd_options()
	crawler = Crawler(options.url, options.file)
	crawler.crawl()

def get_cmd_options():
	&quot;&quot;&quot;
		gets and validates the input from the command line
	&quot;&quot;&quot;
	usage = &quot;usage: %prog [options] args&quot;
	parser = OptionParser(usage)
	parser.add_option('-u', '--url', dest = 'url', help = 'URL to fetch data from')
	parser.add_option('-f', '--file', dest = 'file', help = 'Local file path to save data to')

	(options,args) = parser.parse_args()

	if not options.url:
		print 'You must specify an URL.',sys.argv[0],'--help for more details'
		exit(1)
	if not options.file:
		print 'You must specify a destination file.',sys.argv[0],'--help for more details'
		exit(1)

	return options

if __name__ == '__main__':
	main()
</pre>
<p>Download it, try it out. I worked wonders for me and I hope it will prove useful to other people too&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript-from-python/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Vim and python</title>
		<link>http://blog.motane.lu/2009/05/25/vim-and-python/</link>
		<comments>http://blog.motane.lu/2009/05/25/vim-and-python/#comments</comments>
		<pubDate>Mon, 25 May 2009 09:04:01 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=719</guid>
		<description><![CDATA[Few months ago, at a wurbe edition, I&#8217;ve seen two great editors in action: vim and emacs. At first, I was impressed by Alex Nedelcu&#8216;s presentation of emacs and I gave it a try, but I&#8217;ve switched to vim soon after, because emacs just&#8230;&#8221;didn&#8217;t feel right&#8221;. What I liked about emacs was that could be [...]]]></description>
			<content:encoded><![CDATA[<p>Few months ago, at a <a href="http://wurbe.ro/" title="wurbe" class="outgoing">wurbe</a> edition, I&#8217;ve seen two great editors in action: vim and emacs. At first, I was impressed by <a href="http://blog.alexn.org/" title="Alex Nedelcu's blog" class="outgoing">Alex Nedelcu</a>&#8216;s presentation of emacs and I gave it a try, but I&#8217;ve switched to vim soon after, because emacs just&#8230;&#8221;didn&#8217;t feel right&#8221;. What I liked about emacs was that could be easily extended and customised to fit the user&#8217;s needs with lisp. Alex showed us some scripts made by him to improve his productivity. </p>
<p>I also like to customise my tools, and that what drawn me to emacs in the first place. And when I&#8217;ve switched over to vim, I&#8217;ve tried to write some custom plugins, but with vim it&#8217;s not that simple. Vim uses a built in scripting language, which it&#8217;s really weird and badly documented and since it can&#8217;t be used anywhere outside of vim, mastering it would be a waste of time. So I&#8217;ve postponed the customisation of the editor until I had enough time to look over the vim scripting language. Until now. I&#8217;ve recently read an article presenting vim scripting in python, and I&#8217;ve decided to give it a try. And it proved to be much simpler than I&#8217;ve thought.  </p>
<p>First of all, install vim&#8217;s python support. If you&#8217;re an Ubuntu / Debian user, simply pop this in the console:</p>
<pre class="brush: plain; title: ; notranslate">
tudor@thor:~$ sudo apt-get install vim-python
</pre>
<p>If not, compile vim from source / use your package manager to install vim with python support. After vim is up and running, create the two files in the <strong><em>~/.vim/plugin/</em></strong> directory: <strong><em>my_plugin.vim</em></strong> and <strong><em>my_plugin.py</em></strong>.</p>
<p>The  <strong><em>my_plugin.vim</em></strong> should look something like this:</p>
<pre class="brush: plain; title: ; notranslate">
if !has(&quot;python&quot;)
	call confirm(&quot;You must have vim compiled with python in order for this to work&quot;, 'OK')
	finish
endif

if filereadable($HOME.&quot;/.vim/plugin/my_plugin.py&quot;)
	pyfile $HOME/.vim/plugin/my_plugin.py
else
	call confirm(&quot;Error: my_plugin.py cannot be found! Please reinstall the plugin&quot;, 'OK')
	finish
endif

&quot;commands for invoking the functions
command! -nargs=1 MyPluginDoSomething python do_something('&lt;args&gt;')
command! -nargs=1 MyPluginDoSomethingElse python do_something_else('&lt;args&gt;')
</pre>
<p>This is all the vim script you need to know <img src='http://blog.motane.lu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  And now, in <strong><em>my_plugin.py</em></strong> file, write your plugin&#8217;s logic in python, knowing that each vim command will call a function from this file:</p>
<pre class="brush: python; title: ; notranslate">
def do_something( argument ):
    print 'wassup %s' % argument

def do_something_else:
    pass
</pre>
<p>If you print something in python, it will be shown in vim&#8217;s error messages area, at the bottom of the window. But the vim module is available in python allows you to read and write into vim&#8217;s buffers and therefore inserting data in the opened document. Type <strong>:help python</strong> in vim for more details. </p>
<p>I&#8217;ve started working on a vim plugin that will aid me with my Zend Framework development. A Zend_Tool integrated with vim that actually does something useful :p</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/05/25/vim-and-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python &#8211; first impression</title>
		<link>http://blog.motane.lu/2009/05/12/python-first-impression/</link>
		<comments>http://blog.motane.lu/2009/05/12/python-first-impression/#comments</comments>
		<pubDate>Tue, 12 May 2009 12:19:09 +0000</pubDate>
		<dc:creator>Tudor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[future technologies]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.motane.lu/?p=661</guid>
		<description><![CDATA[This is a post I&#8217;ve been trying to write for about 2 weeks now. As some of you might know, I&#8217;ve spent the previous weeks studying python and writing small scripts and I&#8217;ve decided to write a blog entry about it. As a matter of fact, I&#8217;ve also looked over the Pylons framework, but I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>This is a post I&#8217;ve been trying to write for about 2 weeks now. As some of you might know, I&#8217;ve spent the previous weeks studying python and writing small scripts and I&#8217;ve decided to write a blog entry about it. As a matter of fact, I&#8217;ve also looked over the Pylons framework, but I&#8217;ll write about it in a another post. So here it is, my opinion about python alone: </p>
<h2><span>What I like about python</span></h2>
<p>Well, I loooooooooove the indentation. I really do. Python made it impossible for lamers to write ugly &#8220;one liners&#8221;. Everything must be indented and in its place or it won&#8217;t even compile (compiling aka no syntax errors as python is an interpreted language). After years of dealing with ugly sources with no braces, no indentation and so on, this feature is like a gift from heavens for me. I really hope it will catch on and be implemented in other languages.</p>
<p>I also like the <code>for in</code> iteration over&#8230;well&#8230;everything. This code:</p>
<pre class="brush: python; title: ; notranslate">
for item in collection:
    do_stuff(item)
</pre>
<p>&#8230;works in most cases, even when collection is a file. In which case the loop iterates over the file&#8217;s lines. Tuples, dictionaries and lists are cool features. </p>
<h2><span>What I don&#8217;t like about python</span></h2>
<p>Of course, there are some things I dislike about this programming language. The first thing is that sometimes is too verbose. Python doesn&#8217;t have an post/pre increment operator. You can&#8217;t write <code>i++</code> or <code>++i</code>, although this code compiles. Further more, it compiles and does nothing, taking the act of debugging to a whole new level of annoyance. </p>
<p>You always have to write <code>i += 1</code>. It also doesn&#8217;t have a ternary operator.  If you write <code> a = (condition) ? b : c</code> it will give you an compiling error.</p>
<p>Another weak point is its OOP capabilities. Object orientated programming is very strangely implemented in python. A class example in python looks something like this:</p>
<pre class="brush: python; title: ; notranslate">
class MyClass:
    def __init__(self):
        self.attribute = 'default value'
    def custom_method(self, attribute):
        self.attribute = attribute
    def print_data(self):
        print self.attribute

obj = MyClass()
obj.custom_method('wassabi')
obj.print_data()
</pre>
<p>As you can see, there are no access modifiers (private, protected, public), no instantiation operator (new), the <strong>this</strong> keyword is replaced by <strong>self</strong>, and you must write it every single time you define a new method in the class. And python also allows multiple inheritance, which does one thing: annoys people. </p>
<h2><span>Conclusion</span></h2>
<p>Apart from some really annoying &#8220;features&#8221;, I&#8217;m starting to like python. It provides a quick way and pretty clean way to do get things done. And in the end, this is all that matters&#8230;Python is cool!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.motane.lu/2009/05/12/python-first-impression/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

