Skip to content

Instantly share code, notes, and snippets.

Avatar
Focusing

Mario César mariocesar

Focusing
View GitHub Profile
@mariocesar
mariocesar / utils.py
Created Jun 29, 2021
Sanitize html defining allowed tags and attributes. Using BeautifulSoup
View utils.py
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
mariocesar / README.md
Last active Apr 30, 2021
Useful shell function to load .env files in the terminal session
View README.md

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"
    oldenv=$(env|sort)
    export $(grep -v '^#' .env | xargs -d '\n')
    diff <(echo "$oldenv") <(env | sort)
@mariocesar
mariocesar / README.md
Created Mar 30, 2021
Annoying things to remember about Node.js, npm, yarn, etc and the whole ecosystem
View README.md

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
mariocesar / README.md
Last active Feb 16, 2021
Psycopg2 utils / PostgreSQL with Python
View README.md

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
mariocesar / password_hasher.py
Created Dec 14, 2020
Some notes for twitch.tv/melkeydev to make a simple/secure password hasher
View password_hasher.py
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
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 =
icons:
mkdir -p icons
View script.py
from multiprocessing import Queue, Process
def do_something_really_slow(url):
...
result = Queue()
proc = Process(target=do_something_really_slow, args=(url,))
proc.start()
proc.join(timeout=5) # Wait 5 seconds, then timeout
@mariocesar
mariocesar / script.py
Created Sep 30, 2020
Python requests timeout retry
View script.py
import requests
from requests.adapters import Retry, HTTPAdapter
class RetryTimeoutAdapter(HTTPAdapter):
def __init__(self):
max_retries = Retry(
total=3,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504],
method_whitelist=["GET", "HEAD", "INFO"],
@mariocesar
mariocesar / wsgi.py
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
View wsgi.py
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
mariocesar / runtasks.py
Created Jun 30, 2020
Run a celery task in a django project using a command #django #celery
View runtasks.py
import json
import logging
from logging import getLogger, DEBUG
from celery import signature
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def add_arguments(self, parser):