Blog

Django: Choices & Enum

Informatique,
Tags: django, python, web

Choices can be efficiently represented in Django: you can use a small CharField of keys stored in the database, associated with verboses descriptions, which can even be translated.

Here is the example from the official doc:

from django.db import models

class Student(models.Model):
    FRESHMAN = 'FR'
    SOPHOMORE = 'SO'
    JUNIOR = 'JR'
    SENIOR = 'SR'
    YEAR_IN_SCHOOL_CHOICES = (
        (FRESHMAN, 'Freshman'),
        (SOPHOMORE, 'Sophomore'),
        (JUNIOR, 'Junior'),
        (SENIOR, 'Senior'),
    )
    year_in_school = models.CharField(max_length=2,
                                      choices=YEAR_IN_SCHOOL_CHOICES,
                                      default=FRESHMAN)

    def is_upperclass(self):
        return self.year_in_school in (self.JUNIOR, self.SENIOR)

year_in_school is then really convinient to use, because you get the HTML's <select> that exactly fits your needs, and Django also generates for you a get_year_in_school_display() method that outputs the verbose version you are looking for.

But, hey, who wants to write all of those constants ?
Here is my version for the same functionnalities, you'll quickly understand my point:

from django.db import models
from enum import IntEnum

def enum_to_choices(enum):
    return ((item.value, item.name) for item in list(enum))

class Student(models.Model):
    YEARS_IN_SCHOOL = IntEnum('years_in_school', 'freshman sophomore junior senior')
    year_in_school = models.IntegerField(choices=enum_to_choices(YEARS_IN_SCHOOL),
                                         default=1)

    def is_upperclass(self):
        return self.year_in_chool >= YEARS_IN_SCHOOL.junior

I think that years in school may benefits of beeing represented with numbers, but in other use cases, you can also use models.CharField and Enum


Let us play with D-Bus \o/ (quick example with python and spotify)

Informatique,
Tags: python, scripts

D-Bus is a free and open-source inter-process communication (IPC) system, allowing multiple, concurrently-running computer programs (processes) to communicate with one another.

But if you are here, you probably already know what it is, and you may want to know how to quickly use it to talk with your applications.

You have probably already heard about tools like dbus-send or qdbus, but you first need to know which messages you can send.

The solution's name is D-Feet. It's a graphical tool that allows you to seamlessly and easily use D-Bus introspection functions to quickly find which methods and properties are available for your applications.
If you need a console tool, you can also try mdbus2.

Now, one quick example: let's say that you would like to control your spotify client.

  1. Launch Spotify
  2. Type "spotify" in D-Feet's search field. If nothing appears, check that you are looking in the "Session Bus", and not the "System Bus"
  3. You should easily find methods such as "Play", "Pause", "PlayPause", "Next", "Previous", "Stop" and a few other ones that are pretty self-explaining. Here, you can double clic on methods to call them, or on readable properties to read them.
  4. Now launch a python, with dbus-python installed (dbus-python >= 1.0 works with py2 & py3) and imported: >>> import dbus
  5. Get an interface object
bus = dbus.SessionBus()
proxy = bus.get_object('org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2')
interface = dbus.Interface(proxy, dbus_interface='org.mpris.MediaPlayer2.Player')

Where org.mpris.MediaPlayer2.spotify is the "bus name" you can see on the left column of D-Feet, /org/mpris/MediaPlayer2 is the "object path" in the first level of the tree in the right column, and org.mpris.MediaPlayer2.Player is the "dbus interface" which will allow you to directly call methods.

  1. What now ? Nothing, you're done ! Wanna switch play/pause ? interface.PlayPause() ! Wanna stop ? interface.Stop()
  2. Ok, ok, if you need to get or set properties, you have to use the getter and setter in the interface org.freedesktop.DBus.Properties:
props_iface = Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
props_iface.GetAll("org.mpris.MediaPlayer2.Player")

But it seems to be a really poor example because the spotify client just does not implement all the standard functionnalities of org.mpris.MediaPlayer2:( (tried interface.Play() ? Sorry :P)

Oh, and three more things:

  • You can find my script in py3 on github
  • if you really want bash:
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
    /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  • Another example to connect your bluetooth device: github

Les limites du bluetooth

Voyages,
Tags: japon

En général, les ondes, c’est pas mon truc. Mais le bluetooth®, c’est quand même particulièrement pratique. D’ailleurs, visiblement, je ne suis pas le seul à apprécier ça, vu que dans les endroits les plus bondés, il est simplement satturé.


Métro

Voyages,
Tags: japon

Les transports en commun sont indispensable pour qui n’a ni vélo ni voiture, ce qui est mon cas (pour l’instant). Quelques petites remarques sur les pratiques courantes à l’emploi des réseaux ferrés Tokyoïtes:

  • quand ils sont contre une porte, les japonais préférent se tourner vers la porte, et donc avoir vue sur le mur du tunnel, en général.
  • aux heures de pointe, il faut pousser pour rentrer tout le monde. Il est alors particulièrement intéressant d’avoir des poches vides, pour pouvoir y mettre ses mains…
  • prendre la bonne ligne ne signifie pas toujours que vous arriverez à la bonne gare, surtout en soirée ; essayez donc de suivre l’itinéraire du train afin de ne pas vous faire avoir.
  • le matin, il vaut mieux éviter la première rame, vu qu’elle est réservée aux femmes.

Horaires de fermeture des services publics

Voyages,
Tags: japon

Quand on a grandi en France, on a l’habitude de ces bonnes vieilles blagues sur les horaires de la Poste.

Ici, quand une mairie annonce qu’elle ferme à 17h00 et que vous arrivez à 16h48, pas de soucis. Vous faites la queue jusqu’à 17h30, on vous prend en charge jusqu’à 17h32, on vous demande un moment pour traiter la demande, on vous reprend à 17h39 et à 17h41 vous êtes dehors.