Skip to content

Instantly share code, notes, and snippets.

@daragh
daragh / module_mocking.py
Created September 10, 2012 15:45
Example code for mocking a module in Python
'''
Example code to show how it is possible to mock an entire module by patching
the sys.modules dict using mock ( http://www.voidspace.org.uk/python/mock/ ).
'''
from mock import patch, MagicMock
def test_import_patching():
module_mock = MagicMock()
with patch.dict('sys.modules', **{
@daragh
daragh / git-show-commit-for-current-patch
Last active December 17, 2015 06:28
A bash script to show the commit that git is trying to apply a patch for in a rebase/apply.
#!/bin/bash
set -e
git_dir=$(git rev-parse --git-dir)
rebase_dir=$git_dir/rebase-apply
if [ ! -d $rebase_dir ]
then
echo 'rebase/apply not in progress' >&2
exit 1
fi
patch_number=$(cat $rebase_dir/next)
@daragh
daragh / remove-trailing-whitespace-from-changed-lines-in-git-commit
Created October 14, 2013 10:15
A shell function to remove trailing whitespace from changed lines in a git commit. Intended to be used as part of a git pre-commit hook.
remove_trailing_whitespace_from_changed_lines() {
patch=$(mktemp --tmpdir remove-trailing-whitespace-patch-XXXXX)
git diff --cached --no-color --diff-filter=M > "$patch"
git apply --index --reverse "$patch"
git apply --index --whitespace=fix "$patch"
rm "$patch"
}
### Keybase proof
I hereby claim:
* I am daragh on github.
* I am daragh (https://keybase.io/daragh) on keybase.
* I have a public key whose fingerprint is 0770 6EE5 1893 13FE DB61 11C2 71AF 3537 99D5 412A
To claim this, I am signing this object:
#!/usr/bin/env python
import sys
(code,) = sys.argv[1:]
func = eval(code)
lines = [line[:-1] if line[-1] == '\n' else iine for line in sys.stdin]
lines.sort(key=func)
for line in lines:
print(line)
@daragh
daragh / pretty_table.py
Created April 22, 2017 16:39
Create a printable text table string in Python
def pretty_table(rows, sep=' | '):
rows = [[str(item) for item in row] for row in rows]
columns = zip(*rows)
widths = [max(len(item) for item in column) for column in columns]
padded = [
[item.ljust(width) for (item, width) in zip(row, widths)]
for row in rows
]
return '\n'.join(sep.join(row) for row in padded)
@daragh
daragh / external_merge_sort.py
Last active June 29, 2019 22:58
External Merge Sort in Python
#!/usr/bin/env python
import sys
import os
from tempfile import mkstemp
from argparse import ArgumentParser
def main(argv):
parser = ArgumentParser()
@daragh
daragh / progressive_sha1sum.py
Created June 29, 2019 23:30
Progressive sha1sum in Python
#!/usr/bin/env python
import sys
import os
import hashlib
from argparse import ArgumentParser
def main(args):
parser = ArgumentParser()
@daragh
daragh / index.html
Created December 7, 2019 20:01
HTML file to be edited with custom markup for use with elm-reactor
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="elm"></div>
@daragh
daragh / redis-dump-restore
Last active December 22, 2019 15:45
Copy keys & values from one Redis instance to another
#!/usr/bin/env python3
import sys
import asyncio
import argparse
from redis import StrictRedis
class Async: