Skip to content

Instantly share code, notes, and snippets.

Emil Stenström EmilStenstrom

View GitHub Profile
@EmilStenstrom
EmilStenstrom / generate_sha256_multicore.py
Created Feb 16, 2014
Competing in the hash|challenge to find the lowest SHA512 hash: http://www.h11e.com/
View generate_sha256_multicore.py
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 / admin.py
Created Feb 12, 2013
Make values in raw_id_fields clickable...
View admin.py
# Example usage...
from admin_util import ImprovedModelForm
class DialogAdmin(ImprovedModelForm):
raw_id_fields = ("forum", "user", ...) # "forum" and "user" are ForeignKeys
...
@EmilStenstrom
EmilStenstrom / runserver.py
Last active Dec 12, 2015
Automatically watch Sass and CoffeeScript files when runserver command is run... Put this in /<your app>/management/commands/runserver.py
View 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 / delete_orphan_users.py
Created Jun 25, 2013
Management command to delete users that no longer have a reference to them (Except UserProfile and SocialData in our case).
View delete_orphan_users.py
# -*- 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 / scrape_soc_statistics.py
Created Mar 16, 2015
Script to scrape data from Socialstyrelsens database over death statistics. You need to specify which codes to fetch via that codes parameter in main().
View scrape_soc_statistics.py
# -*- 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",
View nonscrolling_layer.html
<!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;
}
View kundo-events.js
/*
DOCUMENTATION:
This script can be embedded on any page with Kundo Chat, and will change the contents
of an element on the page, depending on if the customer can start a new chat or not.
To change what's shown, change CHAT_AVAILABLE_HTML and CHAT_UNAVAILABLE_HTML to
anything you want. To add a button that starts the chat, add a call to
window.$kundo_chat.start_chat()
View gensim_tfidf.py
from gensim.models import TfidfModel
from gensim.corpora import Dictionary
class MyCorpus:
def __init__(self, documents):
self.documents = documents
self.dictionary = Dictionary(documents)
def __iter__(self):
View run_tokenize_stats.py
import bz2
import os
import re
from collections import defaultdict
from enum import Enum, auto
from conllu import parse
from spacy.lang.sv import Swedish
@EmilStenstrom
EmilStenstrom / ud_helper.py
Created Aug 31, 2018
Python wrapper for the (very raw) python interface for ufal.udpipe. Saving it here for posterity.
View ud_helper.py
import re
from ufal.udpipe import Model, Pipeline, ProcessingError
class Parser:
MODELS = {
"swe": "data/swedish-ud-2.0-170801.udpipe",
}
def __init__(self, language):
model_path = self.MODELS.get(language, None)
You can’t perform that action at this time.