Timeout a function in python

Posted on 2018-06-02 in Trucs et astuces • Tagged with Python

You can use signals and a context manager to acheive this. The idea is to register a function that will raise an exeption when the alarm signal is received and schedule the alarm signal for the timeout.

import signal
from contextlib import contextmanager


@contextmanager
def timeout(time):
    # Register a function …

Continue reading

Some tips for django

Posted on 2018-05-21 in Trucs et astuces • Tagged with Python, Web, Django


Continue reading

JavaScript tips

Posted on 2018-03-07 in Trucs et astuces • Tagged with JavaScript

Normalize an UTF-8 string

Rely on String.prototype.normalize().

const str = "Crème Brulée"
str.normalize('NFD').replace(/[\u0300-\u036f]/g, "")
> 'Creme Brulee'

Source: https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript/37511463#37511463


PostgreSQL tips

Posted on 2018-03-07 in Trucs et astuces • Tagged with PostgreSQL, Database, SQL

Update fields in JSONB format

Use the jsonb_set(COLUMN_NAME, PATH_TO_CHANGE, VALUE) function. The value must be a valid value in JSON (ie use '500' for numbers, 'true' for …


Continue reading

How to cache Python module in Gitlab CI

Posted on 2017-08-20 in Trucs et astuces • Tagged with gitlab, ci, Python

By default, pip cache will be in ~/.pip. However, this folder cannot be cached by Gitlab. The trick is to force pip to use a folder located in the build directory with the --cache-dir option. You can then cache this folder.

For instance, you can use .pip as in the …


Continue reading

nginx tips

Posted on 2017-08-19 in Trucs et astuces • Tagged with nginx

CORS for multiple domains

location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
    if ( $http_origin ~* (https?://(.+\.)?(domain1|domain2|domain3)\.(?:me|co|com)$) ) {
        add_header "Access-Control-Allow-Origin" "$http_origin";
    }
}

Source: https://stackoverflow.com/a/27879729/3900519


Chiffrer des fichiers avec GPG

Posted on 2017-08-08 in Trucs et astuces • Tagged with GPG, securité

Voici deux fonctions bash qui permettent de chiffrer et déchiffrer un fichier ou un dossier avec GPG.

Les dossiers sont compressés dans une archive ZIP avant le chiffrement.

Le chiffrement laisse les fichiers d'origine intacts. Le déchiffrement laisse le fichier chiffré intact.

Ces fonctions auront leur place dans votre .bashrc …


Continue reading

Extraire le HTML d'un email au format mbox

Posted on 2017-08-07 in Trucs et astuces • Tagged with mail, Python

Voici un petit script Python pour convertir un mail au format mbox en HTML. Pour que le script fonctionne, il faut soit que le corps du message soit du HTML (recommandé) soit que la première pièce jointe du message contienne le message en HTML.

Par défaut, le script traitera tous …


Continue reading

Docker compose tips

Posted on 2017-06-11 in Trucs et astuces • Tagged with Docker, Docker compose

For my tips about docker, go here.

Use docker-compose.override.yml

As describe here, if you create a docker-compose.override.yml next to your docker-compose.yml, you can override or add values to the docker file. This file is loaded by default. To ignore it …


Continue reading

Create python virtual enviroments on Windows

Posted on 2017-06-11 in Trucs et astuces • Tagged with Python

  1. Before creating the venv you will need to open a PowerShell terminal as root and run the commands below to allow the script that activates the virtual env to run:

    cd ..
    Set-ExecutionPolicy Unrestricted
    
  2. Create the venv. Run in a terminal as a normal user: python3 -m venv .venv If the …


Continue reading