Async Bitcoin RPC client
To work with Bitcoin RPC from python there is a library Python-BitcoinRPC.
But recently i need to call API from tornado application. Mentioned lib works in synchronous, i.e. blocking mode. For tornado it will be much better to use asynchronous version. Tried to search for existing solution, but can’t find it. So i create my async fork, that use tornado’s AsyncHTTPClient: https://github.com/st4lk/python-bitcoinrpc-tornado.
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.
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:
- Creation of django project
- Deploy setup
- Server setup
- Deploy
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):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.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 :) ).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'
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']}
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.Python function with 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:
Multilanguage site on django without redirects
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
: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.
Sublime text and github gists
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.
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.
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()
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.
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.