Language Server Protocol (LSP)
LSP - protocol for interactions between IDE and language server. The latter provides such means like autocompletion, goto implementation and etc. When IDE needs to show autocomplete choices on, for example, python language - it sends a request to the special server. And it responds with the necessary data. The cool part here is that it is an initiative of a big company - Microsoft.
Task: for every object count number of related objects satisfying some conditions.
When I needed to implement some functionality on model saving, I always asked a question to myself - where to place it. In signal or in model method
save()? Let’s see, what and when is more applicable.
New JSONField will be added in Django 1.9, it can be used with PostgreSQL >= 9.4. Let’s try to work with it and find out, in what situations it can be useful.
This is a well known topic, but i can’t find the existing solution that will fully satisfy me. So i write it by myself :).
Assume we have a “single page” web site, that is talking with backend via REST API. Client side can be written with ember, angularjs or some like this. Backend - django rest framework (DRF). We’ve got a task - add social login (OAuth protocol).
Recently (26.05.2015) new tornado 4.2 was released. It contains different updates, the most valuable i suppose are modules tornado.locks and tornado.queues. They migrated from package Toro, look detailed explanation in Jesse Jiryu Davis post.
Here i want to tell about another helpful function, that was added with my help - pgettext.
OAuth protocol has two versions: 1.0 and 2.0.
Most of services today use version 2.0, i suppose because it is easier to implement. Also, 2.0 can be realized in standalone applications (those, that don’t have a server).
To understand the protocols very useful to have a look at their realisation. Here i’ll show several scripts that talk to OAuth providers of different versions. Scripts will implement client application functionality. Only standard python libraries are used. This help to overview the OAuth protocol - everything is on single screen and familiar. Of course, for production application we must use third party oauth libs, they handle many special cases and so on. Purpose of these scripts is just understanding of the protocol and nothing else. It is often hard to keep the protocol flow in production-ready library, because it is splitted in many modules, some other packages are used. And the full vision is slipping out of sight.
Recently i’ve read the book Learning Python, 5th Edition by Mark Lutz. Here is a list of most interesting insights for me.
I always knew, that it is possible to catch wifi network packets. But haven’t done it in practise (i was analysing network packets, but not in HTTP protocol). So i decided to do it, as this is interesting and useful. Such experience help to understand TCP-IP and HTTP protocols and also to pay more attention for web security.
In django tests we can measure number of sql queries:
def test_home(self): with self.assertNumQueries(1): response = self.client.get('/') self.assertEqual(response.status_code, 200)
If code in context of
assertNumQuerieswill make other number of DB attempts than expected (here 1), test will throw error. But when test fails it is sometimes hard to understand, what unexpected query was made. To debug such case very useful to log SQL expressions to console. Below is description how to do it.
Let’s talk about i18n, i10n and tornado implementation. This post have a lot of text, but i wanted to describe many things that i faced during realization of i18n in tornado project. The step by step instruction is placed in the second part of this article.
Every record in mongoDB has field
_id, that must be unique inside collection. By default type of this field is ObjectId, and it is assigned automatically if field is not set.
Lets look at ObjectId more carefully.
To set url for tornado handlers we can pass list of tuples
(url regex, handler)into application initialisation:
application = tornado.web.Application([ (r"/", MainHandler), (r"/some/path/page/(?P<pk>[0-9]+)$", PageHandler), ])
To enable celery in new django project i often look in previous ones to refresh in my memory some steps: what settings should be specified, how to launch, how to stop and so on.
Here i want to combine all together in one place.
Did you know, that Django ORM can do nested SQL queries? Shame on me, but i’ve found it not so long ago.
New Relic - service for site monitoring. It shows statistic of your application, where it spend most of time, how often it access the database and many other information. For description of this cool service i need to write a separate post. Here i want to tell about another thing - how i’ve got a free t-shirt from New Relic.
Actually, it is needed just to register and setup New Relic on a working web site. As it said in their offer.
There is a nice tool called ngrok. It allows to bind the URL for your localhost server!
To send email messages from server we can just use SMTP protocol. But there is another way - special email services. I’ll describe one of them here, mandrill.com.
Recently i’ve completed course “M101P: MongoDB for Developers” (periodically repeats, next starts at April). During this course i’ve found to myself interesting features of mongodb.
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.