Skip to content

Instantly share code, notes, and snippets.

@hjwp
hjwp / test_logging.py
Created January 19, 2014 18:33
test to get django's logging set up
import logging
import sys
from unittest.mock import patch # reqs. python 3. change to from mock if p2.
from django.conf.urls import url
from django.http import HttpResponse
from django.test import TestCase
from .urls import urlpatterns
## test assumes a bare django project created called "myproj"
@hjwp
hjwp / get_wordcount_history_from_git.py
Created November 19, 2013 09:23
Script to go back through every single git commit and extract the current wordcount, to make a pretty graph
#!/usr/bin/env python3
from collections import namedtuple
import csv
from datetime import datetime
import os
import re
import subprocess
Commit = namedtuple('Commit', ['hash', 'subject', 'date'])
WordCount = namedtuple('WordCount', ['filename', 'lines', 'words'])
@hjwp
hjwp / phantomjs-qunit-runner.js
Last active December 28, 2015 14:39
(yet another) phantomjs runner for qunit tests. This one produces text output that looks more like the web page.
var system = require('system');
if (!system.args[1]){
console.log('Pass path to test file as second arg');
phantom.exit();
}
var path = system.args[1];
if (path.indexOf('/') !== 0) {
path = system.env.PWD + '/' + path;
from selenium import webdriver
import sys
if 'phantom' in sys.argv:
browser = webdriver.PhantomJS()
else:
browser = webdriver.Firefox()
browser.implicitly_wait(3)
@hjwp
hjwp / selenium_phantomjs_bug_current_url.py
Last active December 26, 2015 21:29
selenium phantomjs current_url bug minimal repro
from selenium import webdriver
import sys
if 'phantom' in sys.argv:
browser = webdriver.PhantomJS()
else:
browser = webdriver.Firefox()
browser.implicitly_wait(3)
PythonAnywhere launches "Startup" Plan
== For immediate release ==
LONDON, United Kindgdom. PythonAnywhere LLP, the creators of the popular
browser-based Python development and hosting platform PythonAnywhere,
announced the launch of their higher-end "Startup" plan today.
CEO Giles Thomas said "We've been growing with our users, and many of
them have been hosting high-volume, high traffic sites with us for
@hjwp
hjwp / ch_14_extract.asciidoc
Last active December 25, 2015 21:09
Mocking getting out of control in chapter 14

Taking another look at our Mozilla Persona spike, we need something like this:

var loggedInUser = '{{ user.email }}' || null;
var csrf_token = '{{ csrf_token }}';
console.log(loggedInUser);

navigator.id.watch({
@hjwp
hjwp / fast_vs_slow_unit_tests.md
Last active December 23, 2015 06:19
Pros and cons of "fast" and "slow" unit tests

Fast and slow is really a misnomer. We're really talking about "pure" unit tests, which are highly isolated from each other, and will require the use of mocks (unless the code follows a functional paradigm), or "impure" unit tests, which involve more dependencies.

mocky/isolated tests Non-mocky/ high-dependency / integratey tests
@hjwp
hjwp / selenium_patcher.py
Created July 30, 2013 09:14
Selenium Python 3 patcher
#!/usr/bin/env python3
import difflib
import urllib.request
import os
import selenium
import imp
import shutil
target_dir = os.path.dirname(selenium.__file__)
@hjwp
hjwp / fix_dict_repr_order.py
Last active December 19, 2015 17:28
Fix Python dict repr so that they are alphabetically. currently handles 2-item dicts, probably very brittle
def fix_dict_repr_order(string):
dict_finder = r"({'\w+': .+, '\w+': .+})"
if not re.search(dict_finder, string):
return string
for dict_repr in re.findall(dict_finder, string):
items = re.search(
r"{('\w+': .+), ('\w+': .+)}",
dict_repr,
).groups()