Skip to content

Instantly share code, notes, and snippets.

Susensio /
Created Apr 27, 2020
Python static variable decorator for functions
# Source:
def static_vars(**kwargs):
def decorate(func):
for k in kwargs:
setattr(func, k, kwargs[k])
return func
return decorate
import shelve
from functools import wraps
def persistent_cache(cache_file='.cache'):
"""Using shelve module creates a cache that persists across executions.
Depends on repr giving a univocal representation of input arguments.
def decorator(func):
def wrapper(*args, **kwargs):
Susensio /
Last active Jul 8, 2020
Inherit property setter in python 3.7

Python @property inheritance the right way

Given a Parent class with value property, Child can inherit and overload the property while accessing Parent property getter and setter.

Although we could just reimplement the Child.value property logic completely without using Parent.value whatsover, this would violate the DRY principle and, more important, it wouldn't allow for proper multiple inheritance (as show in the example bellow).

Two options:

  • Child redefines value property completely, both getter and setter.
Susensio /
Created Sep 26, 2018 — forked from greydanus/
Dynamic plotting for matplotlib
"Dynamic plotting in matplotlib. Copy and paste into a Jupyter notebook."
# written October 2016 by Sam Greydanus
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import time
def plt_dynamic(x, y, ax, colors=['b']):
for color in colors:
ax.plot(x, y, color)
Susensio /
Last active May 3, 2020
Make function of numpy array cacheable

How to cache slow functions with numpy.array as function parameter on Python


from numpy_lru_cache_decorator import np_cache

def function(array):
Susensio /
Last active Sep 17, 2018 — forked from jsfenfen/output.png
Detecting rotation and line spacing of image of page of text using Radon transform
# -*- coding: utf-8 -*-
Inspired in
Automatically detect rotation and line spacing of an image of text using
Fourier transforms
If image is rotated by the inverse of the output, the lines will be
horizontal (though they may be upside-down depending on the original image)
Susensio / HumbleBundle
Last active Aug 29, 2018 — forked from santiagobasulto/
Download HumbleBundle books in batch with a simple Python script.
View HumbleBundle

Download HumbleBundle books

This is a quick Python script I wrote to download HumbleBundle books in batch. I bought the amazing Machine Learning by O'Reilly bundle. There were 15 books to download, with 3 different file formats per book. So I scratched a quick script to download all of them in batch.

(Final Result: books downloaded)

Susensio /
Last active Sep 17, 2018
Add open terminal here shortcut in Debian 9.5 Gnome
# Source:
echo "#! /bin/sh
gnome-terminal" > ~/.local/share/nautilus/scripts/Terminal
chmod +x ~/.local/share/nautilus/scripts/Terminal
# F12 or whatever
echo "F12 Terminal" > ~/.config/nautilus/scripts-accels
Susensio /
Last active Aug 2, 2018
Python logging boilerplate for packages
from .module import foo
import logging
from logging.handlers import RotatingFileHandler
import os
import os.path
# Logging directory
Susensio /
Created Jul 28, 2018
Syncs laptop backligh to LG HDMI monitor
#!/usr/bin/env bash
# Listen for changes in backlight and applies to external screen
acpi_listen | while IFS='/' read -ra line; do
if [ "$line" = "video" ]; then
# Read laptop backlight
sleep 0.2
You can’t perform that action at this time.