Skip to content

Instantly share code, notes, and snippets.

View EmilStenstrom's full-sized avatar

Emil Stenström EmilStenstrom

View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
html, body { height: 100%; }
body { font-size: 4em; color: rgba(0,0,0,0.1); }
body.noscroll {
overflow: hidden;
}
@EmilStenstrom
EmilStenstrom / Object.py
Last active August 31, 2018 10:53
Code found in a library I was considering using... NEVER write code like this :)
class Object:
attributes = ["myattr",
"anotherattr",
"attr1",
"someattr",
"attr5"]
code = "def __init__(self"
for att in attributes:
code = code + ", " + att + " = None"
code = code + "):\n"
@EmilStenstrom
EmilStenstrom / scrape_soc_statistics.py
Created March 16, 2015 12:49
Script to scrape data from Socialstyrelsens database over death statistics. You need to specify which codes to fetch via that codes parameter in main().
# -*- coding: utf-8 -*-
from lxml.html import fromstring
import os
import csv
import requests
def _get_payload_for_code(code):
parameters = {
"i_%s_3" % code: "on",
"visaAG": "on",
@EmilStenstrom
EmilStenstrom / generate_sha256_multicore.py
Created February 16, 2014 16:07
Competing in the hash|challenge to find the lowest SHA512 hash: http://www.h11e.com/
import hashlib
from random import random
from multiprocessing import Process, Array
from ctypes import c_char
CPU_CORES = 4
def new_candidate(prev_hash, seed):
return prev_hash[:32] + seed
@EmilStenstrom
EmilStenstrom / delete_orphan_users.py
Created June 25, 2013 13:16
Management command to delete users that no longer have a reference to them (Except UserProfile and SocialData in our case).
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from emailuser.models import UserProfile
from socialdata.models import SocialData
class Command(BaseCommand):
def _has_relations(self, related_objects, user, whitelist=(User, UserProfile, SocialData)):
for related in related_objects:
model = related.model
@EmilStenstrom
EmilStenstrom / num_to_key_mapper.py
Last active August 31, 2018 10:54
Convert a list of strings to a list of integers suitable for numerical processing. Easily convert back to the strings when done.
class KeyToNumMapper(object):
counter = 0
mapper_key_int = {}
mapper_int_key = {}
def __init__(self, initial):
map(self.add_key, initial)
def add_key(self, key):
if key in self.mapper_key_int:
@EmilStenstrom
EmilStenstrom / runserver.py
Last active December 12, 2015 10:49
Automatically watch Sass and CoffeeScript files when runserver command is run... Put this in /<your app>/management/commands/runserver.py
import atexit
import subprocess
from django.core.management.commands.runserver import BaseRunserverCommand
from django.core.servers.basehttp import AdminMediaHandler
from django.conf import settings
# Patch runserver to run the sass and coffeesscript compilers automatically
class Command(BaseRunserverCommand):
active_processes = []
@EmilStenstrom
EmilStenstrom / admin.py
Created February 12, 2013 10:32
Make values in raw_id_fields clickable...
# Example usage...
from admin_util import ImprovedModelForm
class DialogAdmin(ImprovedModelForm):
raw_id_fields = ("forum", "user", ...) # "forum" and "user" are ForeignKeys
...