Skip to content

Instantly share code, notes, and snippets.

@hjwp
hjwp / toggle_touchpad.py
Created November 3, 2011 14:37
Toggle touchpad on/off (python/linux)
#!/usr/bin/python2.7
import subprocess
def touchpad_currently_off():
all_states = subprocess.check_output(['synclient']).split('\n')
off_state = next((s for s in all_states if 'touchpadoff' in s.lower()), None)
return '1' in off_state
def main():
if touchpad_currently_off():
@hjwp
hjwp / nginx-uwsgi-upgrade-post.md
Created August 26, 2012 17:22
Draft blog post on the switch from apache to nginx/uwsgi

Beloved users, and whomever else may find it of interest,

We recently made the switch from nginx to uwsgi. Well, I say recently, as far as I can tell from the commit logs we started work on it around July 10th, so that's fully six weeks ago. We just deployed it this week, and after a bumpy first few days it seems to be settling in well. We thought we'd share why we switched, and how it's going.

tl;dr:

@hjwp
hjwp / ctrlkey_minimal_repro.py
Last active December 17, 2015 02:59
Minimal repro for selenium control-key bug
import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
class TestCtrlKey(unittest.TestCase):
def tearDown(self):
self.browser.quit()
@hjwp
hjwp / rogue.py
Last active December 17, 2015 06:48
Roguelike tutorial step 1
import libtcodpy as libtcod
#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
LIMIT_FPS = 20 #20 frames-per-second maximum
def handle_keys():
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Acknowledgment:
#
# Python Koans is a port of Ruby Koans originally written by Jim Weirich
# and Joe O'brien of Edgecase. There are some differences and tweaks specific
# to the Python language, but a great deal of it has been copied wholesale.
# So thanks guys!
@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()
@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 / 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 / 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({
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