Interesting articles

| Comments

Several interesting articles I found recently. Some of them I think from Dan Bader twitter.

Django vs Flask - a detailed comparison Django vs Flask.

In most cases I preferer Django.

  • It has better documentation
  • There are better apps in core, with Flask you need to search for an app, compare alternatives and sometimes they are not updated for a long time
  • You can choose which core apps to use
  • I think it’s better developed and supported

How the heck does async/await work in Python 3.5? - a long read about async in python

Using Docker for Flask Application Development (not just Production!) - i use it the same way, the only problem I have with Python development in Docker is debugging in Intellij Idea. There is a remote interpreter, but it does not work well for me, will try it again with the latest version.

Flask-RESTful migration to Flask-RESTPlus

| Comments

In a simple application with a several endpoints, it’s ok to use the default Flask routes.

@app.route('/api/v1.0/users', methods=['GET'])
def get_users():
    return jsonify({'objects': users})

But when an application becomes bigger, it can be useful to use a some REST framework or split it on the smaller apps. The REST framework is used to:

  • enforce best practices
  • simplify: versioning, serialization, documentation, authenication
  • provide browsable API

Makefile help target

| Comments

In many projects to run complex commands or group of commands I use Makefile. To document it I add a help target.

Here is an exemple for dump and restore commands for PostgreSQL in docker-compose under alias db.

DATE = $(shell date +%d%m%Y)
path ?= .

help: ## show this help
	@echo 'usage: make [target] ...'
	@echo ''
	@echo 'targets:'
	@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#'

dump: ## dump db, usage 'make dump path=/path/to/dumps'
	docker-compose exec --user postgres db pg_dumpall --clean | gzip > $(path)/db_$(DATE).sql.gz
restore: ## restore db from dump file, usage 'make restore dump=dump.sql.gz'
	docker-compose stop web
	gunzip -c $(dump) | docker exec -i --user postgres `docker-compose ps -q db` psql
	docker-compose start web

So make or make help outputs:

usage: make [target] ...

targets:
help         show this help
dump         dump db, usage `make dump path=/path/to/dumps`
restore      restore db from dump file, usage `make restore dump=dump.sql.gz`

Docker clean commands

| Comments

When you start to use Docker, at some point there will be a question - where is all my space. Most of it is occupied by containers, images and dangling volumes.

An easy way to clean is to use Docker garbage collection by Spotify and docker volume rm commands. I have an alias for this.

alias docker-gc='docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc -e GRACE_PERIOD_SECONDS=172800 spotify/docker-gc && docker volume rm $(docker volume ls -qf dangling=true)'

On mac sometimes it’s easier to remove all, for this you need to remove Docker.qcow2 in the ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux folder. You can find this path in the Docker -> Preferences -> Advanced dialog.

« 10/13 »