• Tornado web application example

    Star

    Tornado web application example

    Tornado - async web framework for python. I’ll cover shortly pros and cons about tornado and introduce typical web project, that is built on top of it.

    By describing pros and cons i mean my own point of view in compare with django.

    Comments Read More
  • Django project: from beginning to working server

    In this post i’ll describe my experience of deploying django project to VPS-hosting.

    Steps of deploying and server setup:

    1. Creation of django project
    2. Deploy setup
    3. Server setup
    4. Deploy
    Comments Read More
  • Django logging settings

    Let’s look at default django logging settings and try to make them more useful.

    Here what we have in settings.py after command django-admin.py startproject project_name (django 1.5):

    Comments Read More
  • Python logging for every day

    When writing a small python program or script, it is sometimes needed to output debug message or maybe event. It is known, that python has logging module exactly for that purpose. But in my case usually such thing happens: it is lack of time and hands just writes print instead of logging, because i can’t remember all those complicated logging settings. But then, if script is launched often or i must provide it to customer, i have to replace all prints with logging, because it is better to log all messages to file. So, not to keep in my head all these settings, i am writing this post.

    Comments Read More
  • Django queryset.count cache

    Django queryset.count cache

    Once I mentioned, that my django application makes several similiar queries like SELECT COUNT(*) .... As it turns out (for me it was surprise), queryset.count() has not obvious cache. But let me start the story from the beginning (and sorry for my english :) ).

    Comments Read More
  • Unicode string formatting

    Did you know, if one of values in string formatting expression with % operator is unicode, then result string will also be unicode?

    >>> "Hello, %s" % u"Alex"
    u'Hello, Alex'
    >>> "Hello, %s" % u"Алексей"
    u'Hello, \u0410\u043b\u0435\u043a\u0441\u0435\u0439'
    
    Comments Read More
  • Parse url which contains unicode query, using urlparse.parse_qs

    Task: get dictionary of URL GET query. For example, we have following url:

    http://example.com/?key=value&a=b
    

    it is needed to get a dict:

    {'key': ['value'], 'a': ['b']}
    

    Values are lists, because one key may have several values:

    In: http://example.com/?key=value&a=b&a=c
    Out: {'key': ['value'], 'a': ['b', 'c']}
    
    Comments Read More
  • Django admin site optimisation

    Django admin site optimisation

    It is known, that less amount of database attempts leads to better performance. Usually admin page - part of site with low traffic, but anyway it is good, if no additional database calls happen there. It is more pleasant to use it, because page renders faster and also frees server resources.

    In this post i’ll try to reduce some database attempts in admin page, when model method __unicode__ contains related object field. I suppose, that examples describe the situation more clear.

    Comments Read More
  • Python function with mutable default arguments

    Python mutable default arguments

    In python default function arguments are created during executing instruction def and not at each function call. If argument value is immutable object (for example string, integer, tuple) it is ok, but if value is mutable, then there can be a trap:

    def foo(l=[]):
        l.append('x')
        return l
    

    It seems, that every call to foo() will return list [‘x’]. But:

    Comments Read More
  • Multilanguage site on django without redirects

    Star

    Starting from django 1.4, it is possible to set url prefix for each activated language. For example, we want site, that will have russian and english version.

    To do it, add following to settings.py:

    Comments Read More
  • Script for downloading music from vkontakte

    Script for downloading music from vkontakte

    A quick search of corresponding python script doesn’t give results. In post on habra link is broken. So i decided to write my own bicycle, it is avaliable here.

    Launch (needs installed python interpreter):

    python vkcom_audio_download.py
    

    Tested on python 2.6 and 2.7. No external libraries required.

    Comments Read More
  • Sublime text and github gists

    Sublime and github gist

    In Sublime text there are big variety of useful tools, that help to write code. I’ve learned only a small part of them, currently i’m trying to write in Vintage mode (vim style cursor management). But now i want to tell about integration github gists with sublime text. If you don’t now, github gists let you save snippets as a separate file without creation full repository. But it have many repository features - versions, possibility to fork.

    Comments Read More
  • MobileESP: Easily detect mobile web site visitors

    Script will be useful, if you want to show different version of site for desktop computers and mobile devices. Big variety of methods to detect mobile type. Avaliable in different languages, including python. The port to python was made by me with help from my freelance customer.

    Comments Read More
  • Debug django project with embedded python debugger pdb

    I use sublime-text as code editor. It doesn’t have a debugger, so to debug django projects i often used

    print var_name
    

    and look for output in local development server console. I use this approach today also, but sometimes it is great to run code step by step to see variables at each step.

    It can be done with embedded python debugger pdb:

    import pdb; pdb.set_trace()
    
    Comments Read More
  • Cloud service Openshift

    Cloud service Openshift

    I know a few hosting providers with free account and python availability. It is Google App Engine and Alwaysdata. But recently i found great project Openshift from RedHat and this blog site is working on it. Let me describe mentioned hostings first.

    Comments Read More
  • Aphorism messenger

    Aphorism messenger

    I have an interesting project I want to tell you about. The idea to create it was born when I was learning Java. I have read couple of books, made some small task programs but I would like to create something bigger.

    Comments Read More

Atom feed