SQLALchemy vs Django ORM
If you are working with Django ORM most of the time and then switching to SQLAlchemy - you may face some unexpected behavior. In this post I’ll try to describe the most important differences from my point of view.
All examples for SQLAlchemy will be shown in async code, in context of PostgreSQL. Django version - 4.2, SQLAlchemy version - 2.0.
The full examples can be found here https://github.com/st4lk/sqlalchemy-vs-django-orm, in the article the code will be cutted to be short.
Sublime Text and Language Server Protocol
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.
Trap in counting related objects in Django
Task: for every object count number of related objects satisfying some conditions.
Django: signal or model method?
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.Trying JSON in Django and PostgreSQL (and compare with MongoDB)
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.
OAuth and django rest framework
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).
Tornado and pgettext
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 step by step
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.
Python tips & tricks
Recently i’ve read the book Learning Python, 5th Edition by Mark Lutz. Here is a list of most interesting insights for me.
Listen wifi with wireshark
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.
Debug SQL in django test
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
assertNumQueries
will 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.Tornado i18n and l10n
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.
Timestamp and ObjectId in mongoDB
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.
Set url for Tornado handlers
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), ])
But it is more convenient to use wrapper
tornado.web.url
, that allows to assign meaningful names for paths (similar to django url).Django celery setup
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.
Nested SQL queries in Django
Did you know, that Django ORM can do nested SQL queries? Shame on me, but i’ve found it not so long ago.
Free t-shirt from New Relic
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.
Remote url for localhost server
There is a nice tool called ngrok. It allows to bind the URL for your localhost server!
Send email in django project with mandrill service
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.
What you should know about mongodb indexes
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.