Skip to content

Instantly share code, notes, and snippets.


Mario César mariocesar

View GitHub Profile
mariocesar /
Created Jun 29, 2021
Sanitize html defining allowed tags and attributes. Using BeautifulSoup
from bs4 import BeautifulSoup, Comment
from typing import Dict, List, Optional, Tuple
AllowedTagsType = Dict[str, Optional[List[str]]]
# Example:
default_allow_tags = {
"p": None,
"b": None, # Allow no property
mariocesar /
Last active Apr 30, 2021
Useful shell function to load .env files in the terminal session

Load environment variables using .env files

You can add the following functions for your shell, loadenv will search for .env file if found will export all to the shell session with unloadenv will unset the variables so you can start again, reloadenv will reread the .env file.

function loadenv {
    test -f .env || echo "No .env file in the working directory"
    export $(grep -v '^#' .env | xargs -d '\n')
    diff <(echo "$oldenv") <(env | sort)
mariocesar /
Created Mar 30, 2021
Annoying things to remember about Node.js, npm, yarn, etc and the whole ecosystem

Most packages still fail due peer dependency version resolve. Packages like expo-cli. To be able to install it make sure you have the legacy mode for resolving versions is enabled

npm config --global set legacy-peer-deps true
mariocesar /
Last active Feb 16, 2021
Psycopg2 utils / PostgreSQL with Python

Create a connection using a database url from Heroku or Digital Ocean App platform

from urllib.parse import urlparse

import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

def create_connection():
    url = urlparse(os.environ["DATABASE_URL"])
mariocesar /
Created Dec 14, 2020
Some notes for to make a simple/secure password hasher
import base64
import hashlib
import secrets
from typing import NoReturn
def pbkdf2(password: str, salt: str, iterations: int, dklen: int=0, digest=None):
"""Return the hash of password using pbkdf2."""
if digest is None:
digest = hashlib.sha256
mariocesar / Makefile
Last active Nov 25, 2020
Build all icons and favicon needed for a webpage, using a svg input file
View Makefile
INKSCAPE_BIN ?= flatpak run org.inkscape.Inkscape
iconsizes = 16 32 48 57 60 72 76 96 120 144 152 180 192 256 300
iconnames = $(addsuffix .png, $(addprefix icons/icon-, $(iconsizes)))
all: icons icons/favicon.ico $(iconnames)
@echo = Done =
mkdir -p icons
from multiprocessing import Queue, Process
def do_something_really_slow(url):
result = Queue()
proc = Process(target=do_something_really_slow, args=(url,))
proc.join(timeout=5) # Wait 5 seconds, then timeout
mariocesar /
Created Sep 30, 2020
Python requests timeout retry
import requests
from requests.adapters import Retry, HTTPAdapter
class RetryTimeoutAdapter(HTTPAdapter):
def __init__(self):
max_retries = Retry(
status_forcelist=[500, 502, 503, 504],
method_whitelist=["GET", "HEAD", "INFO"],
mariocesar /
Last active Jul 1, 2020
Adding custom header with the deploy datetime of a Django app. Useful also for adding revision, and other deploy information
import os
import subprocess
from datetime import datetime
import django
from django.core.handlers.wsgi import WSGIHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings.develop")
DEPLOY_REVISION = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
mariocesar /
Created Jun 30, 2020
Run a celery task in a django project using a command #django #celery
import json
import logging
from logging import getLogger, DEBUG
from celery import signature
from import BaseCommand, CommandError
class Command(BaseCommand):
def add_arguments(self, parser):