Skip to content

Instantly share code, notes, and snippets.

View eloraburns's full-sized avatar

Elora Burns eloraburns

View GitHub Profile
@eloraburns
eloraburns / gist:786091
Created January 19, 2011 12:18
Random bit flipping
# Assuming Python >=2.5, <3.0
import os
from random import randint
num_random_bits = 9
with open('f', 'r+') as f:
f.seek(0, os.SEEK_END)
length = f.tell()-1
for _ in range(num_random_bits):
place = randint(0, length)
f.seek(place, os.SEEK_SET)
@eloraburns
eloraburns / highlight.py
Created October 3, 2011 19:45
Highlight the provided (regexp) arg in the output
#!/usr/bin/env python
import sys
import re
r = re.compile("(?P<subject>" + sys.argv[1] + ")")
for line in sys.stdin:
print r.sub("\x1B[1;32m\g<subject>\x1B[0m", line)
@eloraburns
eloraburns / gist:1507806
Created December 21, 2011 21:32
The 1-liner (whitespace is unnecessary) extensible way to "increment" a string
INCREMENTED_MAP = {'1': ('2', False), '0': ('1', False), '3': ('4', False), '2': ('3', False), '5': ('6', False), '4': ('5', False), '7': ('8', False), '6': ('7', False), '9': ('0', True), '8': ('9', False)}
# Can extend INCREMENTED_MAP with 'a': ('b', False), ... 'z': ('a', True) to get lowercase alphabetics, etc
def incremented(number):
return ''.join(
reversed(
reduce(
lambda (so_far, carry_in), (old_digit, (new_digit, carry_out)):
(
so_far + (new_digit if carry_in else old_digit,),
@eloraburns
eloraburns / gist:1672006
Created January 24, 2012 19:20
Confluence 4 "edit wiki page source" bookmarklets
// This one swaps OUT the tinyMCE editor in favour of the underlying textarea:
javascript:(function(){tinymce.editors[0].hide();jQuery('#wysiwygTextarea').show().removeClass('hidden').css({width:'100%',height:parseInt(jQuery('#main').css('height'))-100+"px"});})()
// This one puts the tinyMCE editor back in place, before you save:
javascript:(function(){jQuery('#wysiwygTextarea').hide().addClass('hidden');tinymce.editors[0].show();})()
// They should probably also disable/enable the "Save" button, so you don't accidentally try to save from the wrong view (your changes get backed out!).
diff --git a/src/kill.c b/src/kill.c
index ad09321..b0fe633 100644
--- a/src/kill.c
+++ b/src/kill.c
@@ -217,12 +217,29 @@ send_signals (int signum, char *const *argv)
return status;
}
+/* Send ALL THE SIGNALS to all the processes or process groups specified
+ by ARGV. Return a ridiculous exit status. */
@eloraburns
eloraburns / gist:2030832
Created March 13, 2012 19:09
Getting -1 from a nan (that should have been positive...)
diff -r e90378882863 pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py Tue Mar 13 11:15:10 2012 -0700
+++ b/pypy/module/micronumpy/__init__.py Tue Mar 13 12:08:16 2012 -0700
@@ -98,6 +98,7 @@
("rad2deg", "degrees"),
("reciprocal", "reciprocal"),
("sign", "sign"),
+ ("signbit", "signbit"),
("sin", "sin"),
("sinh", "sinh"),
@eloraburns
eloraburns / gist:2570145
Created May 1, 2012 18:09
Frozenset that caches state transitions
# Modeled after the idea used in dynamic language runtimes, where "expensive" mutable object transitions are cached, when you can safely assume that there will be a finite number of transitions.
# For example, on my iMac:
# $ python -m timeit -n 10000 -r 3 -s "from caching_frozenset import CachingFrozenset" "s = CachingFrozenset()
# for x in range(100):
# s = s.with_value(x)"
# 10000 loops, best of 3: 66.1 usec per loop
# $ python -m timeit -n 10000 -r 3 "s = frozenset()
@eloraburns
eloraburns / gist:2757885
Created May 20, 2012 12:16
Function overloading in Python
#!/usr/bin/python
import inspect
from functools import wraps
from collections import defaultdict
# This time, we'll avoid using a class to hold the namespace. We'll just use
# the class that the functions are being defined in.
# AS A BONUS
# This means that it works for module-level functions, not just methods!
@eloraburns
eloraburns / runwhen.py
Created July 10, 2012 18:00
Run a command when the filesystem changes
#!/usr/bin/env python
import os
import sys
from time import sleep
usable_files = (
# Customize this as you see fit
'.py',
'.php',
@eloraburns
eloraburns / gist:4062594
Created November 12, 2012 22:50
nose plugin for ipython notebook (early alpha), me and Greg Ward!
{
"metadata": {
"name": "nose experiment"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{