Python tricks

Posted on 2018-10-03 in Trucs et astuces • Tagged with Python

Inspect TLS certificates with requests

Use the snippet below to capture and inspect the certificates of a distant site and its chain with requests.

import requests

HTTPResponse = requests.packages.urllib3.response.HTTPResponse
orig_HTTPResponse__init__ = HTTPResponse.__init__
def new_HTTPResponse__init__(self, *args, **kwargs):
    orig_HTTPResponse__init__(self, *args, **kwargs …

Continue reading

Use dnsmasq with NetworkManager

Posted on 2018-09-08 in Trucs et astuces • Tagged with linux

This will, for instance, allow you to redirect all matching domains to a certain server: dnsmasq will intercept the DNS query and return the ip you specified.

To do this, edit NetworkManager configuration in /etc/NetworkManager/NetworkManager.conf and add in the main section dns=dnsmasq:

[main]
dns=dnsmasq

You …


Continue reading

VSCode tips

Posted on 2018-09-02 in Trucs et astuces • Tagged with VSCode

Sommaire

Python

venv

To use a venv with VSCode, add in the folder settings:

{
    "python.pythonPath": "~/.virtualenvs/bureauxlocaux--Uxk5jFn/bin/python",
    "python.unitTest.pyTestEnabled": true
}

Deploy your source maps to Rollbar with Ansible

Posted on 2018-07-16 in Trucs et astuces • Tagged with Rollbar, Python, Ansible

I recently had to integrate Rollbar (a service to track errors) with a JS application. One nice thing about Rollbar is that if you provide it with source maps, it will point to you where the error is in the original unminified code.

Since the application is deployed with Ansible …


Continue reading

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