Skip to content

Instantly share code, notes, and snippets.

View judy2k's full-sized avatar

Mark Smith judy2k

View GitHub Profile
@judy2k
judy2k / double.py
Created March 26, 2013 15:55
The hackiest script in the world - anything that looks like an int in the input, replace with double its value and output to stdout. Use as follows (on OSX): 1. Copy the json data you want to double from your IDE 2. pbcopy | ./double.py | pbpaste 3. Paste the new json with all the numbers doubled back into your IDE
#!/usr/bin/env python
import re
import sys
def rep(sval):
return str(int(sval.group()) * 2)
if __name__ == '__main__':
@judy2k
judy2k / unicode.md
Last active December 31, 2015 02:09
These are some notes for a potential short talk on Python & Unicode.

Python & Unicode

text = open('a_unicode_file.txt', 'r').read()
print text
print 'type:', type(text)       # str is a container for binary data
print 'bytes:', len(text)       # The number of bytes, not characters!
print ' '.join(repr(b) for b in text)
print 'first byte:', text[:1] # Prints an invalid character!
@judy2k
judy2k / gitignore
Last active February 1, 2022 11:14
A script to manage a .gitignore file
#!/bin/bash
# To add two items to the current .gitignore file:
# gitignore '*.swp' bin
#
# To sort and de-dupe the current .gitignore file:
# gitignore
# Append each argument to its own line:
for item in "$@"; do
echo "$item" >> .gitignore;
@judy2k
judy2k / preloadImage.js
Last active August 29, 2015 13:58
Preload an image before calling a callback function
var preloadImage = function(url, callback) {
var loaded = false,
loadHandler = function() {
if (!loaded) {
callback(url);
}
loaded = true;
},
img = $('<img>')
.one('load', loadHandler)
@judy2k
judy2k / one_line_regex.py
Created May 12, 2014 10:22
Python regex matching and capturing in one line
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re, sys
def magic_match(pattern, target):
frame = sys._getframe(1)
result = re.match(pattern, target)
# This is properly, properly evil. Don't do this:

Keybase proof

I hereby claim:

  • I am judy2k on github.
  • I am judy (https://keybase.io/judy) on keybase.
  • I have a public key whose fingerprint is 1F8A C55A EC5F F23B 013A DDD2 F298 5834 FAE5 4325

To claim this, I am signing this object:

@judy2k
judy2k / writeable_closures.py
Last active August 29, 2015 14:07
Writeable Closures
#!/usr/bin/env python
def outer():
i = [0]
def inner():
i[0] += 1
print i[0]
return inner
f = outer()
@judy2k
judy2k / uc.py
Created October 15, 2014 14:09 — forked from anonymous/uc.py
Traceback (most recent call last):
File "./uc.py", line 7, in <module>
u += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 1: ordinal not in range(128)
@judy2k
judy2k / fizz_buzz_decorator_abomination.py
Last active August 29, 2015 14:13
A bunch of attempts to create a really horrible fizz buzz implementation in Python.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import wraps
def test(condition):
def test_decorator(f):
@wraps(f)
def test_wrapper(n):
def call_me(names=[]):
names.append('Stupid')
print ' '.join(names)
for _ in range(10):
call_me()