Skip to content

Instantly share code, notes, and snippets.

@leonsas
leonsas / grokking_the_gil.py
Created May 19, 2017 19:47
Grokking the GIL and atomic ops
# Tweaked from https://emptysqua.re/blog/grok-the-gil-fast-thread-safe-python/
import threading
for i in range(100):
n = 0
def foo():
global n
n += 1
threads = []
@leonsas
leonsas / custom_numpy_assertions.py
Created May 18, 2017 01:12
Custom Django/Python Assertions through Numpy
class NumpyAssertions:
def assertAlmostEqualNP(self, lval, rval, atol=None):
np.testing.assert_allclose(lval, rval, atol=atol)
class AlmostEqualityNumpyTest(TestCase, NumpyAssertions):
def test_numpy_almost_equal(self):
self.assertAlmostEqualNP(44.55, 44.75, atol=0.05)
@leonsas
leonsas / audit.py
Created August 9, 2016 18:35
Django AuditTrail
"""
From https://code.djangoproject.com/wiki/AuditTrail
Modified to allow `exclude_fields` options.
"""
from django.dispatch import dispatcher
from django.db import models
from django.core.exceptions import ImproperlyConfigured
from django.contrib import admin
import copy
@leonsas
leonsas / disable_signals.py
Created June 8, 2016 22:10 — forked from RobertKolner/disable_signals.py
Temporarily disable all signals in django.
from collections import defaultdict
from django.db.models.signals import *
class DisableSignals(object):
def __init__(self, disabled_signals=None):
self.stashed_signals = defaultdict(list)
self.disabled_signals = disabled_signals or [
pre_init, post_init,
pre_save, post_save,
import os
import arrow
from boto.s3.connection import S3Connection
S3_BACKUP_BUCKET = 'backup-bucket-name'
APP_NAME = 'myapp'
S3_REGION_HOSTNAME = 'e.g s3-us-west-1.amazonaws.com'
os.system('curl -o latest.dump `heroku pg:backups public-url --app %s`' % APP_NAME)
@leonsas
leonsas / crop_pictures_in_dir.py
Last active August 29, 2015 14:19
Cropping Pictures in Dir
from PIL import Image
import glob
filenames = glob.glob('*.png')
for f in filenames:
img = Image.open(open(f))
(w,h) = img.size
cropped_image = img.crop((0,0,w,w))
cropped_image.save(open('cropped_'+f,'w'), 'PNG')
Verifying that +leonsas is my openname (Bitcoin username). https://onename.com/leonsas
var linkranks = {"Paris": {"Rh%C3%B4ne-Alpes": 0.003025313292630801, "Women%27s_march_on_Versailles": 0.001960156560233164, "Tuileries_Palace": 0.001960156560233164, "Jacques_Chirac": 0.005880469680699492, "Metropolitan_area": 0.0002820987671835713, "Liverpool": 0.0006656336150812513, "Church_of_Saint-Sulpice,_Paris": 0.001960156560233164, "Luxembourg_in_the_Eurovision_Song_Contest": 0.0007010761311666402, "Basilica_of_St_Denis": 0.003920313120466328, "Eure": 0.0033556310996435625, "China": 0.0003702107970702168, "A86_autoroute": 0.003920313120466328, "Caen": 0.0033556310996435625, "Euro": 0.002460631271808035, "Azerbaijan_in_the_Eurovision_Song_Contest": 0.0007010761311666402, "Democratic_Republic_of_the_Congo": 0.0011675251349560507, "Paris_Match": 0.001960156560233164, "Mus%C3%A9e_d%27Art_Moderne_de_la_Ville_de_Paris": 0.001960156560233164, "List_of_films_set_in_Paris": 0.001960156560233164, "President_of_France": 0.003920313120466328, "Place_Dauphine": 0.001960156560233164, "Eurovision_Song_Contest_1967":
function forEachHTMLCollection(collection, fn){
for (var i = collection.length - 1; i >= 0; i--) {
fn(collection[i]);
};
}
ps = document.getElementsByTagName('p');
forEachHTMLCollection(ps, function(e){
e.style.color = 'white';
@leonsas
leonsas / gist:9c4387ff71dd0b564366
Last active August 29, 2015 14:09
Wiki to png
CITIES = [
('Chicago', 'chicago'),
('New_York_City', 'newyork'),
('Los_Angeles', 'losangeles'),
('Paris', 'paris'),
('Tokyo', 'tokyo'),
]
command_str ="""
/Users/leonsas/projects/temp/capturejs/capture.js --uri http://en.wikipedia.org/wiki/%s \