Skip to content

Instantly share code, notes, and snippets.

Susensio

Block or report user

Report or block Susensio

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View presistent_cache.py
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):
@wraps(func)
def wrapper(*args, **kwargs):
@Susensio
Susensio / property_inheritance.md
Last active Sep 6, 2019
Inherit property setter in python 3.7
View property_inheritance.md

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 property_inheritance.py bellow).

Two options:

  • Child redefines value property completely, both getter and setter.
@Susensio
Susensio / dynamic_plotting.py
Created Sep 26, 2018 — forked from greydanus/dynamic_plotting.py
Dynamic plotting for matplotlib
View dynamic_plotting.py
"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
Susensio / numpy_lru_cache.md
Last active Nov 12, 2019
Make function of numpy array cacheable
View numpy_lru_cache.md

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

TL;DR

from numpy_lru_cache_decorator import np_cache

@np_cache()
def function(array):
    ...
@Susensio
Susensio / rotation_spacing.py
Last active Sep 17, 2018 — forked from jsfenfen/output.png
Detecting rotation and line spacing of image of page of text using Radon transform
View rotation_spacing.py
# -*- coding: utf-8 -*-
"""
Inspired in https://gist.github.com/jsfenfen/4c615775007b802117b7
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
Susensio / HumbleBundle books.md
Last active Aug 29, 2018 — forked from santiagobasulto/README.md
Download HumbleBundle books in batch with a simple Python script.
View HumbleBundle books.md

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
Susensio / open_terminal_shortcut.sh
Last active Sep 17, 2018
Add open terminal here shortcut in Debian 9.5 Gnome
View open_terminal_shortcut.sh
# Source: https://askubuntu.com/questions/68078/keyboard-shortcut-for-open-a-terminal-here
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
Susensio / __init__.py
Last active Aug 2, 2018
Python logging boilerplate for packages
View __init__.py
from .module import foo
import logging
from logging.handlers import RotatingFileHandler
import os
import os.path
# Logging directory
LOGGING_FOLDER = 'logs/'
LOG_PATH_TEMPLATE = LOGGING_FOLDER + "{}.log"
@Susensio
Susensio / sync_backlight.sh
Created Jul 28, 2018
Syncs laptop backligh to LG HDMI monitor
View sync_backlight.sh
#!/usr/bin/env bash
# Listen for changes in backlight and applies to external screen
MAX_BACKLIGHT=$(</sys/class/backlight/intel_backlight/max_brightness)
last_backlight=$(</sys/class/backlight/intel_backlight/brightness)
acpi_listen | while IFS='/' read -ra line; do
if [ "$line" = "video" ]; then
# Read laptop backlight
sleep 0.2
@Susensio
Susensio / profiling_qcachegrind.sh
Last active Sep 7, 2018
Python profiling with QCacheGrind
View profiling_qcachegrind.sh
# Profile with cProfile python module and save to output
python -m cProfile -o output.cprof script.py
# Transform into tree format for QCacheGrind
pyprof2calltree -i output.cprof -o callgrind.cprof
# Open QCacheGrind and load callgrind.cprof file
You can’t perform that action at this time.