<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>KodoBlog | tech practice &amp; philosophy</title><generator>Tumblr (3.0; @kodoblog)</generator><link>http://kodoblog.05bit.com/</link><item><title>Dynamic ModelForm creation</title><description>&lt;p&gt;That looks amazing!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def get_model_form_class(model_class, fields_list=None, exclude_list=None):
    class form_class(forms.ModelForm):
        class Meta:
            model = model_class
            fields = fields_list
            exclude = exclude_list
    return form_class
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The idea taken from:
&lt;a href="http://stackoverflow.com/questions/297383/dynamically-update-modelforms-meta-class/297478#297478" target="_blank"&gt;http://stackoverflow.com/questions/297383/dynamically-update-modelforms-meta-class/297478#297478&lt;/a&gt;&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/8258348266</link><guid>http://kodoblog.05bit.com/post/8258348266</guid><pubDate>Sat, 30 Jul 2011 18:57:00 +0400</pubDate><category>python</category><category>django</category><category>django-models</category><category>django-forms</category></item><item><title>Shortcut for printing user's full name or username in template</title><description>&lt;p&gt;That&amp;#8217;s it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from django import template

@register.filter
def nice_name(user):
    """
    Example::

        Hi, {{ user|nice_name }}
    """
    return user.get_full_name() or user.username
&lt;/code&gt;&lt;/pre&gt;</description><link>http://kodoblog.05bit.com/post/833684240</link><guid>http://kodoblog.05bit.com/post/833684240</guid><pubDate>Tue, 20 Jul 2010 02:25:59 +0400</pubDate><category>python</category><category>snippet</category><category>templatetags</category><category>django</category></item><item><title>Great setup example for Nginx + FastCGI + Mercurial hgwebdir</title><description>&lt;a href="http://streamhacker.com/2009/07/28/how-to-deploy-hgwebdir-fcgi-behind-nginx-with-fab/"&gt;Great setup example for Nginx + FastCGI + Mercurial hgwebdir&lt;/a&gt;: &lt;p&gt;It also uses fabfile for automation of start / restart process.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/805965415</link><guid>http://kodoblog.05bit.com/post/805965415</guid><pubDate>Tue, 13 Jul 2010 14:04:29 +0400</pubDate><category>unix</category><category>mercurial</category><category>fastcgi</category><category>nginx</category><category>example</category></item><item><title>Seems like i&amp;#8217;m in trend. The idea to build decentralized network comes to me again and...</title><description>&lt;p&gt;Seems like i&amp;#8217;m in trend. The idea to build decentralized network comes to me again and again.&lt;/p&gt;

&lt;p&gt;And it seems, we&amp;#8217;ll see first p2p social networks soon. &lt;a href="http://www.wuala.com" target="_blank"&gt;Wuala&lt;/a&gt; is running. &lt;a href="http://www.kickstarter.com/projects/196017994/diaspora-the-personally-controlled-do-it-all-distr" target="_blank"&gt;Diaspora&lt;/a&gt; is coming. Actually, their start is amazing: they&amp;#8217;ve raised investments on Kickstarter and &lt;a href="http://www.joindiaspora.com/2010/07/01/one-month-in.html" target="_blank"&gt;made friends&lt;/a&gt; with &lt;a href="http://pivotallabs.com" target="_blank"&gt;Pivotal Labs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m working on social network prototype at the moment. The key feature although is not p2p at all. Another major trend is: there&amp;#8217;s too much information. So, we need filter and customize information flows completely. So, custom &lt;strong&gt;design&lt;/strong&gt; matters :)&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/779999154</link><guid>http://kodoblog.05bit.com/post/779999154</guid><pubDate>Wed, 07 Jul 2010 10:10:00 +0400</pubDate><category>technologies</category><category>trends</category><category>news</category></item><item><title>thecodefarm team</title><description>&lt;a href="http://thecodefarm.com"&gt;thecodefarm team&lt;/a&gt;: &lt;p&gt;I’m going to play with &lt;a href="http://dajaxproject.com/" target="_blank"&gt;Dajax&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It’s created by &lt;a href="http://github.com/jorgebastida" target="_blank"&gt;Jorge Bastida&lt;/a&gt; from &lt;a href="http://thecodefarm.com/en/team/" target="_blank"&gt;thecodefarm team&lt;/a&gt;. I like them. :)&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/715775356</link><guid>http://kodoblog.05bit.com/post/715775356</guid><pubDate>Sat, 19 Jun 2010 23:13:00 +0400</pubDate><category>teams</category><category>people</category><category>projects</category></item><item><title>How to extract html page title by URL</title><description>&lt;p&gt;Actually the subject can be divided into two tasks:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;retreive data&lt;/li&gt;
&lt;li&gt;extract information from it&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;There&amp;#8217;s standard library &lt;code&gt;urllib2&lt;/code&gt; in Python for retreiving data over HTTP and a number of libraries for parsing HTML data. I&amp;#8217;ll use &lt;code&gt;html5lib&lt;/code&gt; in this example.&lt;/p&gt;

&lt;h2&gt;First iteration of retrieving data&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;import urllib2

def read_url(url):
    try:
        response = urllib2.urlopen(url)
    except urllib2.URLError:
        return u''
    encoding = get_charset(response.headers)
    return unicode(data, encoding)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We need extra utility function &lt;code&gt;get_charset&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def get_charset(headers, default='utf-8'):
    try:
        content_type = headers['content-type'].lower()
        if content_type.find('charset=') &amp;gt; 0:
            return content_type.split('charset=')[-1].lower()
    except KeyError:
        pass
    return default
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can get data!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; d = read_url('http://python.org')
&amp;gt;&amp;gt;&amp;gt; d[:50]
u'&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Seems like that&amp;#8217;s what wee need.&lt;/p&gt;

&lt;h2&gt;Extracting title with html5lib&lt;/h2&gt;

&lt;p&gt;There are examples for it: &lt;a href="http://www.sal.ksu.edu/faculty/tim/NPstudy_guide/web/parsing_html.html#html5lib-usage" target="_blank"&gt;&lt;a href="http://www.sal.ksu.edu/faculty..." target="_blank"&gt;http://www.sal.ksu.edu/faculty&amp;#8230;&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s extractor function based on that examples:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from html5lib import HTMLParser, treebuilders, treewalkers

parser = HTMLParser(tree=treebuilders.getTreeBuilder("dom"))
walker = treewalkers.getTreeWalker("dom")

def extract_title(html):
    domtree = parser.parse(html)
    titleNode = False
    title = u''
    for token in walker(domtree):
        if token['type'] == 'StartTag' and token['name'] == 'title':
            titleNode = True
        elif titleNode:
            if token['type'] == 'EndTag' and token['name'] == 'title':
                break
            elif token.has_key('data'):
                title += token['data']
    return title.strip()
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Let&amp;#8217;s try!&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; extract_title(d)
u'Python Programming Language -- Official Website'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Amazing! That&amp;#8217;s working!&lt;/p&gt;

&lt;h2&gt;Optimization, possibly&lt;/h2&gt;

&lt;p&gt;The one drawback of extraction method above is that page has to be completely downloaded and parsed for title extraction. I&amp;#8217;ve tried to optimize it: read HTTP data just until title data is read.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s &lt;code&gt;read_url&lt;/code&gt; function revisited. It&amp;#8217;s designed to read data by chunks until specified string is met.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import re
import urllib2

def read_url(url, until=None, chunk=100):
    try:
        response = urllib2.urlopen(url)
    except urllib2.URLError:
        return u''

    encoding = get_charset(response.headers)

    if until:
        next, data, trunk_at = True, '', None
        while next:
            next = response.read(chunk)
            data += next
            until_match = re.search(until, data, re.IGNORECASE)
            if until_match:
                response.close()
                data = unicode(data, encoding)
                return data[:data.find(until) + len(until)]
    else:
        data = response.read()
    return unicode(data, encoding)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, we can now read until &lt;code&gt;&amp;lt;/title&amp;gt;&lt;/code&gt;!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; d = read_url('http://python.org/', until='&amp;lt;/title&amp;gt;')
&amp;gt;&amp;gt;&amp;gt; d
u'&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;\n\n\n&amp;lt;html xmlns
="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&amp;gt;\n\n&amp;lt;head&amp;gt;\n  &amp;lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&amp;gt;\
n  &amp;lt;title&amp;gt;Python Programming Language -- Official Website&amp;lt;/title&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;#8217;s test perfomance. The very-very basic test looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def test():
    from time import time

    t1 = time()
    d1 = read_url('http://python.org/', until='&amp;lt;/title&amp;gt;')
    t2 = time()

    t3 = time()
    d2 = read_url('http://python.org/')
    t4 = time()

    print t2-t1
    print t4-t3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; test()
0.131000041962
0.31500005722
&amp;gt;&amp;gt;&amp;gt; test()
0.12700009346
0.318000078201
&amp;gt;&amp;gt;&amp;gt; test()
0.125999927521
0.31299996376
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Optimized extractor shown considerable faster results.&lt;/p&gt;

&lt;h2&gt;That&amp;#8217;s it&lt;/h2&gt;

&lt;p&gt;Other HTML parsing libraries are mentioned &lt;a href="http://stackoverflow.com/questions/717541/parsing-html-in-python" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/663157309</link><guid>http://kodoblog.05bit.com/post/663157309</guid><pubDate>Fri, 04 Jun 2010 16:09:00 +0400</pubDate><category>python</category><category>html parsing</category><category>html5lib</category><category>urllib2</category></item><item><title>Django Widget for CommaSeparatedIntegerField with pre-defined choices</title><description>&lt;a href="http://djangosnippets.org/snippets/2051/"&gt;Django Widget for CommaSeparatedIntegerField with pre-defined choices&lt;/a&gt;: &lt;p&gt;Assume, we need a registration form for some event with time frame for 3 days, starting at Monday. User can select any of 3 days, so we need to show 3 checkboxes.&lt;/p&gt;

&lt;p&gt;Here’s the basic example for such form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class EventRegistrationForm(forms.Form):
    days = forms.CharField(widget=NumbersSelectionWidget(
                    ['Mon', 'Tue', 'Wed'], range(1, 4)))
&lt;/code&gt;&lt;/pre&gt;</description><link>http://kodoblog.05bit.com/post/662963797</link><guid>http://kodoblog.05bit.com/post/662963797</guid><pubDate>Fri, 04 Jun 2010 14:27:00 +0400</pubDate><category>django</category><category>python</category><category>snippet</category><category>django-widget</category></item><item><title>Unicode 'funny characters'</title><description>&lt;p&gt;There&amp;#8217;re characters that &lt;strong&gt;sometimes&lt;/strong&gt; cause strange behaviour, when trying to print them to console.&lt;/p&gt;

&lt;p&gt;It seems that depends on environment and Python compilation. I&amp;#8217;ve tested it on Windows Vista and it failed, than it worked on some *nix machines and failed on others. I used Python 2.6.x version. So, it&amp;#8217;s possible you will be unable to rebpoduce it!&lt;/p&gt;

&lt;h2&gt;An example&lt;/h2&gt;

&lt;p&gt;Russian character &amp;#8216;ы&amp;#8217; has code U+044B, symbol &amp;#8216;©&amp;#8217; has code U+00A9.&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;    &amp;gt;&amp;gt;&amp;gt; a = u'ы'
    &amp;gt;&amp;gt;&amp;gt; a
    u'\u044b'
    &amp;gt;&amp;gt;&amp;gt; b = u'\u00a9'
    &amp;gt;&amp;gt;&amp;gt; b
    u'\xa9'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Trying to print:&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;    &amp;gt;&amp;gt;&amp;gt; print a
    ы
    &amp;gt;&amp;gt;&amp;gt; print b
    ...
    UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 0: character maps to &amp;lt;undefined&amp;gt;
    &amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Try to write to file:&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;    &amp;gt;&amp;gt;&amp;gt; f = open('test.txt', 'w')
    &amp;gt;&amp;gt;&amp;gt; f.write((a + b).encode('utf-8'))
    &amp;gt;&amp;gt;&amp;gt; f.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s working ok!&lt;/p&gt;

&lt;h2&gt;What to do&lt;/h2&gt;

&lt;p&gt;Walk wide.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve found a suggestion to remove such charecters from text: &lt;a href="http://code.activestate.com/recipes/546517-accent2htmlcodepy-convert-accents-and-special-char/" target="_blank"&gt;http://code.activestate.com/recipes/546517-accent2htmlcodepy-convert-accents-and-special-char/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Possibly that&amp;#8217;s not best solution, but may be nesseccary, if you want to print text with &amp;#8216;funny characters&amp;#8217; to console.&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;_spec_chars = [u'\xc1',u'\xe1',u'\xc0',u'\xc2',u'\xe0',u'\xc2',u'\xe2',u'\xc4',u'\xe4',u'\xc3',u'\xe3',u'\xc5',u'\xe5',u'\xc6',u'\xe6',u'\xc7',u'\xe7',u'\xd0',u'\xf0',u'\xc9',u'\xe9',u'\xc8',u'\xe8',u'\xca',u'\xea',u'\xcb',u'\xeb',u'\xcd',u'\xed',u'\xcc',u'\xec',u'\xce',u'\xee',u'\xcf',u'\xef',u'\xd1',u'\xf1',u'\xd3',u'\xf3',u'\xd2',u'\xf2',u'\xd4',u'\xf4',u'\xd6',u'\xf6',u'\xd5',u'\xf5',u'\xd8',u'\xf8',u'\xdf',u'\xde',u'\xfe',u'\xda',u'\xfa',u'\xd9',u'\xf9',u'\xdb',u'\xfb',u'\xdc',u'\xfc',u'\xdd',u'\xfd',u'\xff',u'\xa9',u'\xae',u'\u2122',u'\u20ac',u'\xa2',u'\xa3',u'\u2018',u'\u2019',u'\u201c',u'\u201d',u'\xab',u'\xbb',u'\u2014',u'\u2013',u'\xb0',u'\xb1',u'\xbc',u'\xbd',u'\xbe',u'\xd7',u'\xf7',u'\u03b1',u'\u03b2',u'\u221e']

def cleanspec(s, cleaned=_spec_chars):
    return ''.join([(c in cleaned and ' ' or c) for c in s])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Try print cleaned text:&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;    &amp;gt;&amp;gt;&amp;gt; print cleanspec(b + a)
     ы
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is workaround. May be, that&amp;#8217;s an issue for Python&amp;#8217;s &lt;em&gt;print&lt;/em&gt;, I&amp;#8217;m not quite sure about that.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s it :)&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/624238687</link><guid>http://kodoblog.05bit.com/post/624238687</guid><pubDate>Sun, 23 May 2010 10:16:00 +0400</pubDate><category>python</category><category>unicode</category></item><item><title>Next order value for Django model instance</title><description>&lt;p&gt;Assume, we have Django model with special &lt;em&gt;weight&lt;/em&gt; integer field for ordering. We may want to assign its value automatically on save. Here&amp;#8217;s the snippet implementing such behaviour:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class MyModel(models.Model):
    # some fields...
    weight = models.IntegerField(default=0)

    class Meta:
        ardering = ('weight',)

    def save(self, *args, **kwargs):
        self.weight = get_next_value(self, field_name='weight')
        super(MyModel, self).save(*args, **kwargs)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here&amp;#8217;s the &lt;em&gt;get_next_value&lt;/em&gt; implementation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def get_next_value(instance, field_name='order', step=10, **filter):
    model = instance.__class__
    qs = model.objects.order_by('-%s' % field_name)
    if filter:
        qs = qs.filter(**filter)
    try:
        max_value = getattr(qs[:1][0], field_name, 0)
    except IndexError:
        max_value = 0
    return (max_value / step + 1) * step
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The implementation above can handle any field name with specified step. It can also provide next field value for filtered queryset.&lt;/p&gt;

&lt;p&gt;Provided snippet is a part of &lt;a href="http://bitbucket.org/rudi/halfbit-web-helpers/src" target="_blank"&gt;halfbit-web-helpers&lt;/a&gt; collection.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/620138781</link><guid>http://kodoblog.05bit.com/post/620138781</guid><pubDate>Sat, 22 May 2010 01:39:00 +0400</pubDate><category>django</category><category>python</category><category>snippet</category><category>django-models</category></item><item><title>Smarter Django cart</title><description>&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://bitbucket.org/rudi/django-carting" target="_blank"&gt;django-carting&lt;/a&gt; is a basic online store application for Django. It is designed in a sketchy manner to be like a &lt;a href="http://kodoblog.05bit.com/post/542925796/rewritable-django-apps" target="_blank"&gt;rewritable application&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Demo is available:&lt;br/&gt;
&lt;a href="http://carting-demo.05bit.com" target="_blank"&gt;http://carting-demo.05bit.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Concept&lt;/h2&gt;

&lt;p&gt;It&amp;#8217;s conceptually differs both  from &lt;a href="http://www.satchmoproject.com" target="_blank"&gt;Satchmo&lt;/a&gt; and &lt;a href="http://www.getlfs.com" target="_blank"&gt;LFS&lt;/a&gt; projects. Basically, that&amp;#8217;s just a &amp;#8220;cart application&amp;#8221; with utilities, which can used to build full-featured online store.&lt;/p&gt;

&lt;p&gt;The two points are:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;em&gt;products catalog&lt;/em&gt; is always too custom to be customized&lt;/li&gt;
&lt;li&gt;&lt;em&gt;design and html layouts&lt;/em&gt; are usually totally new for commercial project&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So, that parts should be rewritten from scratch each time.&lt;/p&gt;

&lt;h2&gt;Feature&lt;/h2&gt;

&lt;p&gt;Cart is smartly binded to user or session. It binds to authenticated user or stores in session for anonymous user. So, cart is remebered for authenticated user and does not dissappear when session expires.&lt;/p&gt;

&lt;h2&gt;Projects powered by&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="http://mamagaz.ru" target="_blank"&gt;Mamagaz&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://isecretshop.ru" target="_blank"&gt;Sny i Sekrety&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://carting-demo.05bit.com" target="_blank"&gt;Joy points shop&lt;/a&gt; ;)&lt;/li&gt;
&lt;/ul&gt;</description><link>http://kodoblog.05bit.com/post/606848342</link><guid>http://kodoblog.05bit.com/post/606848342</guid><pubDate>Mon, 17 May 2010 16:57:00 +0400</pubDate><category>django</category><category>django-app</category><category>online store</category><category>python</category><category>django-rewritable</category></item><item><title>Smarter Django project configuration</title><description>&lt;h2&gt;Two points&lt;/h2&gt;

&lt;ol&gt;&lt;li&gt;&lt;p&gt;Generally we need different settings for Django project in development environment on localhost and on production environment. Settings may differ also across different development environments when application is developed by many programmers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Project source is good to be stored in some VCS repository, so settings should also be stored there. Settings modifications that are common for different environments should be automatically applied after updating from repository.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;h2&gt;How to&lt;/h2&gt;

&lt;p&gt;Here&amp;#8217;s a couple of recipes on how to configure Django project in a bit smarter way.&lt;/p&gt;

&lt;h3&gt;1. Define settings template&lt;/h3&gt;

&lt;p&gt;Just make a copy of &lt;em&gt;settings.py&lt;/em&gt; to &lt;em&gt;settings_template.py&lt;/em&gt; and then import &lt;em&gt;settings_template&lt;/em&gt; from settings:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from settings_template import *
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then you may override some settings, i.e. &lt;em&gt;DUBUG&lt;/em&gt; flag or database settings etc.:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from settings_template import *

DEBUG = False
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; now &lt;em&gt;settings.py&lt;/em&gt; should not be stored in repository, but &lt;em&gt;settings_template.py&lt;/em&gt; should be stored there.&lt;/p&gt;

&lt;h3&gt;2. Define paths based on project directory&lt;/h3&gt;

&lt;p&gt;I know many good programmers do that :) Add somewhere at start of settings, I mean &lt;em&gt;settings_template.py&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import os

PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can define media and template paths like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')

MEDIA_URL = '/media/'

ADMIN_MEDIA_PREFIX = '/media/admin/'

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates'),
)
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;3. Configure serving static files&lt;/h3&gt;

&lt;p&gt;Django documentation offers to &lt;a href="http://docs.djangoproject.com/en/dev/howto/static-files/#limiting-use-to-debug-true" target="_blank"&gt;serve on not to serve static files depenging on &lt;em&gt;DEBUG&lt;/em&gt; flag&lt;/a&gt;. Sometimes that may not be correct, as we may need debug mode on production environment, where static files are served by webserver.&lt;/p&gt;

&lt;p&gt;I offer to use separate &lt;em&gt;SERVE_STATIC&lt;/em&gt; flag in settings for this. So, here is &lt;em&gt;urls.py&lt;/em&gt; example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;from django.conf import settings

# some urls config ...

if getattr(settings, 'SERVE_STATIC', False):
    urlpatterns += patterns('',
        (r'^' + settings.MEDIA_URL[1:] + r'(?P&amp;lt;path&amp;gt;.*)$',
         'django.views.static.serve',
         {'document_root': settings.MEDIA_ROOT})
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, when I need to serve static files by development server, I just add &lt;em&gt;SERVE_STATIC = True&lt;/em&gt; to &lt;em&gt;settings.py&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;Feedbacks&lt;/h2&gt;

&lt;p&gt;Please give a feedback, if it was useful or not. I hope it was :) If you have your own recipes on subject, you&amp;#8217;re welcome to share links or snippets.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/583464730</link><guid>http://kodoblog.05bit.com/post/583464730</guid><pubDate>Sun, 09 May 2010 11:45:00 +0400</pubDate><category>python</category><category>django</category><category>deployment</category></item><item><title>Python logging example and helper</title><description>&lt;p&gt;When &lt;a href="http://www.google.ru/search?q=python+logging+to+file+example" target="_blank"&gt;trying to find simple example&lt;/a&gt; on how to use Python logging module for writing logs to file, I became frustrated, as there are only a few useful examples. Here&amp;#8217;s one that really works and covers most of use cases: &lt;a href="http://www.mechanicalcat.net/richard/log/Python/Simple_usage_of_Python_s_logging_module" target="_blank"&gt;see it on mechanicalcat.net&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And here&amp;#8217;s simple helper to open log file, based on that example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import re
import logging

def openlog(filename, logger_name=None, level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s'):
    if not logger_name:
        logger_name = filename[filename.rfind('/')+1:]
    logger = logging.getLogger(logger_name)
    handler = logging.FileHandler(filename)
    formatter = logging.Formatter(format)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.setLevel(level)
    return logger
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, you can simply use this helper like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="python"&gt;logger = openlog('/var/log/just_a_test.log')
logger.debug("hello, i'm debug message!")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Actually, basic examples are &lt;a href="http://docs.python.org/library/logging.html#simple-examples" target="_blank"&gt;provided in docs&lt;/a&gt;. I missed them first time, because reference seems to be complicated at start point.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/552701880</link><guid>http://kodoblog.05bit.com/post/552701880</guid><pubDate>Tue, 27 Apr 2010 09:40:00 +0400</pubDate><category>python</category><category>logging</category><category>helper</category><category>example</category></item><item><title>Flavoured Markdown</title><description>&lt;p&gt;&lt;a href="http://daringfireball.net/projects/markdown/" target="_blank"&gt;Markdown&lt;/a&gt; is good, but it has 2 features that&amp;#8217;s hard to explain to inexperienced user:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;it doesn&amp;#8217;t convert urls to links automatically&lt;/li&gt;
&lt;li&gt;it doesn&amp;#8217;t convert line breaks to html &amp;lt;br&amp;gt; tags&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It seems, for 80% cases this would better be done. &lt;a href="http://gitorious.org/python-markdown" target="_blank"&gt;Python Markdown&lt;/a&gt; supports extensions and thus can be &amp;#8220;flavoured&amp;#8221;.&lt;/p&gt;

&lt;p&gt;So, there are extensaions:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="http://bitbucket.org/rudi/halfbit-web-helpers/src/tip/python/markdown_flavours/mdx_autobr.py" target="_blank"&gt;autobr&lt;/a&gt; for converting line breaks and&lt;/li&gt;
&lt;li&gt;&lt;a href="http://bitbucket.org/rudi/halfbit-web-helpers/src/tip/python/markdown_flavours/mdx_autolink.py" target="_blank"&gt;autolink&lt;/a&gt; for converting urls&lt;/li&gt;
&lt;/ul&gt;</description><link>http://kodoblog.05bit.com/post/550989429</link><guid>http://kodoblog.05bit.com/post/550989429</guid><pubDate>Mon, 26 Apr 2010 19:47:00 +0400</pubDate><category>python</category><category>markdown</category><category>helpers</category></item><item><title>Automatically converting urls to html links</title><description>&lt;p&gt;Here&amp;#8217;s simple Python snippet for converting urls to links in single text line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import re

link_re = re.compile('(\s+\(?|^)((http|ftp|https)://[-\w\#$%&amp;amp;~/.;:=,?@+]+)', re.IGNORECASE)

def autolinks(line):
    return link_re.sub(r'\1&amp;lt;a href="\2" target="_blank"&amp;gt;\2&amp;lt;/a&amp;gt;', line)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;#8217;s simple yet smart enough:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;doesn&amp;#8217;t touch urls inside html links &amp;lt;a href=&amp;#8221;some url&amp;#8221;&amp;gt;some text&amp;lt;/a&amp;gt;&lt;/li&gt;
&lt;li&gt;can be used with &lt;a href="http://daringfireball.net/projects/markdown/" target="_blank"&gt;Markdown&lt;/a&gt;, as it doesn&amp;#8217;t touch Markdown links&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;To enable auto links replacement in Markdown, you may use &lt;a href="http://bitbucket.org/rudi/django-rudi-helpers/src/tip/helpers/markdown_flavours/mdx_autolink.py" target="_blank"&gt;autolink extension&lt;/a&gt;.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/548235931</link><guid>http://kodoblog.05bit.com/post/548235931</guid><pubDate>Sun, 25 Apr 2010 19:13:00 +0400</pubDate><category>python</category><category>markdown</category><category>html</category></item><item><title>Highway to brilliant wiki, part 1. Brillixy.</title><description>&lt;h3&gt;Hey!&lt;/h3&gt;

&lt;p&gt;I &lt;strong&gt;am&lt;/strong&gt; unsatisfied.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s 21st century, the age of perfect web-frameworks and rapid development. But there&amp;#8217;s no good wiki-like engine to satisfy reasonable requirements.&lt;/p&gt;

&lt;p&gt;There are &lt;a href="http://trac.edgewall.org" target="_blank"&gt;Trac&lt;/a&gt;, &lt;a href="http://www.dokuwiki.org" target="_blank"&gt;DokuWiki&lt;/a&gt; and many &lt;a href="http://www.wikimatrix.org/" target="_blank"&gt;others&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/List_of_wiki_software" target="_blank"&gt;others&lt;/a&gt;. By the way, I really appreciate Trac and DokuWiki. But I need less. And more.&lt;/p&gt;

&lt;p&gt;So, I just need an engine which:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;has wiki-like basis: users can create, edit and delete pages,&lt;/li&gt;
&lt;li&gt;has &lt;em&gt;hierarchical&lt;/em&gt; pages structure,&lt;/li&gt;
&lt;li&gt;can be &lt;em&gt;easily&lt;/em&gt; themed, &lt;em&gt;design&lt;/em&gt; is really matters,&lt;/li&gt;
&lt;li&gt;is written in &lt;em&gt;Python / Django&lt;/em&gt; and uses best web-development practices.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Brillixy&lt;/h3&gt;

&lt;p&gt;I started project to make such engine. I called it &amp;#8220;Brillixy&amp;#8221;. It&amp;#8217;s hosted here: &lt;a href="http://bitbucket.org/rudi/brillixy" target="_blank"&gt;http://bitbucket.org/rudi/brillixy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some design goals are defined:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;keep specific part as tiny as possibe, reuse proven Django applications, write new ones for generic tasks&lt;/li&gt;
&lt;li&gt;it should has multiple-project structure, so single installation may be used by some team for a number of projects&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;And some design decisions are done:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;use &lt;a href="http://code.tabo.pe/django-treebeard/" target="_blank"&gt;django-treebeard&lt;/a&gt; for storing pages hierarchically&lt;/li&gt;
&lt;li&gt;use &lt;a href="http://code.google.com/p/django-tagging/" target="_blank"&gt;django-tagging&lt;/a&gt; for tagging&lt;/li&gt;
&lt;li&gt;use &lt;a href="http://gitorious.org/python-markdown" target="_blank"&gt;markdown&lt;/a&gt; as a basis for markup language, use &lt;a href="http://github.com/rudyryk/markdown-flavours" target="_blank"&gt;flavours for it&lt;/a&gt;, use &lt;a href="http://github.com/rudyryk/django_autotext" target="_blank"&gt;autotext&lt;/a&gt; to provide specific syntax&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;What&amp;#8217;s next?&lt;/h3&gt;

&lt;p&gt;I&amp;#8217;m planning to release v0.1 with most basic functions:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;creating, editing, deleting pages&lt;/li&gt;
&lt;li&gt;commenting pages&lt;/li&gt;
&lt;li&gt;simple tagging, no AJAX&lt;/li&gt;
&lt;li&gt;simple permissions scheme, allow everything to authenticated users &lt;/li&gt;
&lt;li&gt;no changesets view at this point&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It should be enougth to start use it for some project. I&amp;#8217;m planning to complete it before 9 May 2010.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/545679427</link><guid>http://kodoblog.05bit.com/post/545679427</guid><pubDate>Sat, 24 Apr 2010 19:13:00 +0400</pubDate><category>brillixy</category><category>project</category><category>django</category><category>python</category></item><item><title>Rewritable Django apps</title><description>&lt;p&gt;The common good approach in designing applications for &lt;a href="http://djangoproject.com" target="_blank"&gt;Django&lt;/a&gt; is write them to be &lt;a href="http://www.google.ru/search?source=hp&amp;amp;q=reusable+django+apps&amp;amp;lr=&amp;amp;aq=f&amp;amp;aqi=g1&amp;amp;aql=&amp;amp;oq=&amp;amp;gs_rfai=" target="_blank"&gt;reusable&lt;/a&gt;. Another approach is to build &lt;strong&gt;rewritable&lt;/strong&gt; applications on top of &lt;strong&gt;reusable&lt;/strong&gt; ones.&lt;/p&gt;

&lt;h3&gt;What&amp;#8217;s the point?&lt;/h3&gt;

&lt;p&gt;There&amp;#8217;re number of specific tasks, which can be splitted to generic blocks. But it seems, some &amp;#8220;generic&amp;#8221; blocks better not to be generic by theirs nature, because they need &amp;#8220;a little bit specific&amp;#8221; usage for every project.&lt;/p&gt;

&lt;p&gt;We can build such apps in two ways:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;make big reusable application with &lt;strong&gt;customization&lt;/strong&gt; settings or&lt;/li&gt;
&lt;li&gt;build &lt;strong&gt;rewritable&lt;/strong&gt; application&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Reusable vs Rewritable&lt;/h3&gt;

&lt;p&gt;Here&amp;#8217;s comparison table of approaches.&lt;/p&gt;

&lt;table cellpadding="10"&gt;&lt;tr&gt;&lt;th&gt;Reusable&lt;/th&gt;
&lt;th&gt;Rewritable&lt;/th&gt;
&lt;/tr&gt;&lt;tr style="vertical-align: top; background-color: #eee;"&gt;&lt;td&gt;Try to cover most generic use cases &amp;#8220;out-of-box&amp;#8221; and provide settings&lt;/td&gt;
&lt;td&gt;Set of &amp;#8220;sketches&amp;#8221; to provide basic use cases with examples for usages and possible extendings&lt;/td&gt;
&lt;/tr&gt;&lt;tr style="vertical-align: top;"&gt;&lt;td&gt;Source code is redundant as it covers different use cases&lt;/td&gt;
&lt;td&gt;Source code is minimalistic&lt;/td&gt;
&lt;/tr&gt;&lt;tr style="vertical-align: top; background-color: #eee;"&gt;&lt;td&gt;Usually depends only on Django and extra generic Python libs&lt;/td&gt;
&lt;td&gt;Usually are build on top of other proven applications&lt;/td&gt;
&lt;/tr&gt;&lt;tr style="vertical-align: top;"&gt;&lt;td&gt;You&amp;#8217;d better not modify application source&lt;/td&gt;
&lt;td&gt;Application source can be rewritten or extended using provided basis&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;&lt;h3&gt;Examples&lt;/h3&gt;

&lt;p&gt;Reusable:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="http://code.google.com/p/django-tagging/" target="_blank"&gt;django-tagging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://code.google.com/p/sorl-thumbnail/" target="_blank"&gt;sorl-thumbnail&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;and many other Django apps&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Rewritable:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="http://bitbucket.org/rudi/django-accounts-basic" target="_blank"&gt;django-accounts-basic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;(&amp;#8230; actually, it&amp;#8217;s hard to find examples, as that&amp;#8217;s not common practice to publish such apps, but they MAY be useful! Here may be: basic e-shop application, basic wiki application, etc. Yes. I know, there ARE e-shops and wikis, but they are designed to be &lt;strong&gt;reusable&lt;/strong&gt;, not to be &lt;strong&gt;rewritable&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Conclusions&lt;/h3&gt;

&lt;p&gt;Both approaches are for applications made by programmers for programmers.&lt;/p&gt;

&lt;p&gt;Reusable applications are good for providing &lt;strong&gt;&amp;#8220;out-of-box&amp;#8221; solutions&lt;/strong&gt; for generic tasks, they are rather &amp;#8220;solid&amp;#8221;, so fine customization is hard or can cause overcomplication.&lt;/p&gt;

&lt;p&gt;Rewritable applications are good for providing &lt;strong&gt;code basis on top of reusable apps&lt;/strong&gt;. They must be clean and as small as possible and it&amp;#8217;s critical to provide working examples.&lt;/p&gt;</description><link>http://kodoblog.05bit.com/post/542925796</link><guid>http://kodoblog.05bit.com/post/542925796</guid><pubDate>Fri, 23 Apr 2010 15:24:00 +0400</pubDate><category>python</category><category>django</category><category>programming</category><category>approach</category></item></channel></rss>

