Skip to content

Instantly share code, notes, and snippets.

View ionelmc's full-sized avatar
🙃
wat

Ionel Cristian Mărieș ionelmc

🙃
wat
View GitHub Profile
administrator@ubuntu:~/projects/sandbox$ ve-cpython/bin/python json-bench.py && ve-pypy/bin/pypy json-bench.py
Running: json-bench.py
test_cpickle
test_cpickle warmup: 3.89885687828
test_cpickle RUN: 4.03457689285
test_demjson
test_demjson warmup: 143.255517006
test_demjson RUN: 156.608139992
test_cjson
test_cjson warmup: 3.51447200775
@ionelmc
ionelmc / gist:2047741
Last active October 1, 2015 19:57
webserver in a line
python -c "import SimpleHTTPServer, SocketServer, BaseHTTPServer; SimpleHTTPServer.test(SimpleHTTPServer.SimpleHTTPRequestHandler, type('Server', (BaseHTTPServer.HTTPServer, SocketServer.ThreadingMixIn, object), {}))" 9090
# short
python -c "from SimpleHTTPServer import test, BaseHTTPServer as b, SimpleHTTPRequestHandler as h; from SocketServer import ThreadingMixIn as t; test(h, type('Server', (b.HTTPServer, t, object), {}))" 9090
# shorter
python -c "import SimpleHTTPServer as h, SocketServer as s, BaseHTTPServer as b; h.test(h.SimpleHTTPRequestHandler, type('S', (b.HTTPServer, s.ThreadingMixIn, object), {}))" 9090
# this variant disables dns reverse lookups
python -c "import SimpleHTTPServer as h, SocketServer as s, BaseHTTPServer as b; h.test(type('H', (h.SimpleHTTPRequestHandler, object), {'address_string': lambda self: str(self.client_address[0])}), type('S', (b.HTTPServer, s.ThreadingMixIn, object), {}))" 9090
@ionelmc
ionelmc / cmd-proxy.py
Created August 14, 2012 08:44
Command line http proxy (for benchmarking)
#!/usr/bin/env python
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
import subprocess
import shlex
import fcntl
import errno
from cStringIO import StringIO
@ionelmc
ionelmc / gist:3754670
Created September 20, 2012 08:28
Test the damn imports
find src -name \*.py | sed 's/\//./g;s/\.py$//g;s/\.__init__$//;s/^src\.//' | while read -r line; do
echo Importing $line '\033[1;31m'
PYTHONPATH=src DJANGO_SETTINGS_MODULE=xxx.settings python -c "import $line"
echo -n '\033[0m'
done
@ionelmc
ionelmc / admin.py
Created October 16, 2012 12:37
terse admin.py
from django.contrib import admin
from .models import SomeModel, OtherModelWithFKToSomeModel
def register(model):
def decorator(klass):
admin.site.register(model, klass)
return klass
return decorator
def inline(model, klass=admin.TabularInline, **options):
return type(
@ionelmc
ionelmc / create-mysql-ramdisk-instance.sh
Last active August 21, 2017 07:02
Mysql instances (ramdisk, linux)
#!/bin/bash -xe
# source: http://www.ducea.com/2009/01/19/running-multiple-instances-of-mysql-on-the-same-machine/
i=$1
port=$[3306+$i]
#mysqladmin -S /var/run/mysqld/mysqld$i.sock shutdown || true
kill -9 `cat /var/run/mysqld/mysqld$i.pid` || true
umount /var/lib/mysql$i || true
rm -rf /var/lib/mysql$i
mkdir /var/lib/mysql$i
class RegistrationBackend(SimpleBackend):
def register(self, request, **kwargs):
name, email, password = kwargs['name'], kwargs['email'], kwargs['password1']
username = generate_id(name, '', email)
UserProfile.objects.create(
user=User.objects.create_user(username, email, password),
name=name
)
from django.template import Library
register = Library()
@register.simple_tag(takes_context=True)
def active_page(context, page_name):
active = active_class(context, page_name)
return ' class="%s" ' % active if active else active
@register.simple_tag(takes_context=True)
def active_class(context, page_name):
@ionelmc
ionelmc / windows-network-type-fixup.py
Created November 17, 2012 23:12
windows-network-type-fixup.py
import win32com.client
NETWORK_CATEGORIES = {
1: "PRIVATE",
0: "PUBLIC",
2: "DOMAIN"
}
m = win32com.client.Dispatch("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")
more = 1
pos = 1
connections = m.GetNetworkConnections()
python -c "while 1:import sys,random;sys.stdout.write(random.choice('\/'))"