Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
stephenmcd / parallel_merge_sort.py
Last active April 16, 2024 02:56
Parallel Merge Sort
import math
import multiprocessing
import random
import sys
import time
def merge(*args):
# Support explicit left/right args, as well as a two-item
# tuple which works more cleanly with multiprocessing.
from rest_framework import serializers
class HyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
"""
This is a performance wrapper for HyperlinkedIdentityField.
We save a ton of time by not calling reverse potentially
thousands of times per request.
"""
def __init__(self, *args, **kwargs):
@stephenmcd
stephenmcd / richtextfield_clean.py
Created April 26, 2012 22:19
Patch Mezzanine's RichTextField without upgrading
"""
XSS privilege escalation by malicious non-superuser admin users.
Fixed in Mezzanine 1.0.9:
https://bitbucket.org/stephenmcd/mezzanine/changeset/40cbc47b8d8a
If an admin user was to create their own POST submit to any forms with a
RichTextField, they could include JavaScript that does the following:
@stephenmcd
stephenmcd / persistent_test_session.py
Created January 30, 2012 04:45
Persistent Sessions in Django TestCase
"""
The Django test client implements the session API but doesn't persist values in it:
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session
This Client subclass can be used to maintain a persistent session during test cases.
"""
from django.conf import settings
from django.test import TestCase
@stephenmcd
stephenmcd / metrics.py
Created January 23, 2014 05:20
Baseline system metrics daemon for statsd
import os
import time
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import NoArgsCommand
from django.db.models import Sum
import psutil
import redis
import statsd
@stephenmcd
stephenmcd / MergeSort.scala
Last active April 8, 2019 05:06
Parallel Merge Sort in Scala/Akka
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.Future
import scala.util.Random
import akka.actor.{ActorSystem, Actor, Props}
import akka.pattern.ask
import akka.util.Timeout
import akka.routing.RoundRobinPool
@stephenmcd
stephenmcd / patch_extend_node.py
Created May 8, 2012 12:00
Allow circular Django template inheritance, so projects can both override and extend a reusable app's templates.
from django.template.loader_tags import ExtendsNode
def get_extends_parent(self, context):
"""
Patched onto Django's ``ExtendsNode.get_parent`` which is
responsible for loading the template to extend from with
the ``extends`` template tag.
This patch allows the template foo/bar.html to extend
foo/bar.html, given that there is another version of it that
@stephenmcd
stephenmcd / rtl_field.py
Created December 11, 2013 19:54
Calculated RTL field handling in Django Rest Framework.
from unicodedata import bidirectional
from django.template.defaultfilters import striptags
from rest_framework import serializers
class RTLField(serializers.BooleanField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("read_only", True)
super(RTLField, self).__init__(*args, **kwargs)
@stephenmcd
stephenmcd / linkedin_viewers_rss.py
Created December 7, 2013 21:28
Convert linkedin profile viewers to RSS.
import cgi
import getpass
import mechanize
import json
import SimpleHTTPServer
import SocketServer
import uuid
@stephenmcd
stephenmcd / deep_force_unicode.py
Created August 5, 2012 00:37
Recursive force_unicode
from django.utils.encoding import force_unicode
from django.utils.functional import Promise
def deep_force_unicode(value):
"""
Recursively call force_unicode on value.
"""
if isinstance(value (list, tuple, set)):
value = type(value)(map(deep_force_unicode, value))
elif isinstance(value, dict):