Skip to content

Instantly share code, notes, and snippets.

import argparse
import contextlib
"""
Example usage:
python2.7 distinct_files.py --fileA=<fileA> --fileB=<fileB> > filenames_in_fileA_not_in_fileB.txt
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser()
@Newky
Newky / stdev_sample
Last active December 29, 2015 18:49
def stdev_sample(n):
diff = sum(n) / len(n)
diff_n_squared = map(lambda x: (x-diff) ** 2, n)
variance = sum(diff_n_squared) / (len(diff_n_squared) -1)
return math.sqrt(variance), variance
stdev_sample([1.23, 1.25, 1.21, 1.55])
@Newky
Newky / ls.sh
Created May 21, 2013 21:48
Another bit for a writeup
$ ls .git/hooks
applypatch-msg.sample
commit-msg.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
prepare-commit-msg.sample
pre-rebase.sample
update.sample
@Newky
Newky / json_demo.sh
Created May 21, 2013 21:44
Demo of creating and adding bad json for write up
$ echo "{" >> bad.json
$ git add bad.json
$ git commit
Invalid json in bad.json:
Expecting object: line 1 column 1 (char 1)
@Newky
Newky / json_hook.py
Created May 21, 2013 21:40
Json git hook for write up
import json
def precommit(git_state):
for fname in git_state["files"]:
if fname.endswith(".json"):
with open(fname, "r") as f:
try:
json.loads(f.read())
continue
@Newky
Newky / Hooked-usage.sh
Created May 21, 2013 21:02
Write-up for blog post.
$ hooked.py --help
Usage: hooked.py [options]
Options:
-h, --help show this help message and exit
--git-root=GITROOT the root directory of the git folder to inject hook
--clean Clean up after yourself
$ # lets make a new repo
@Newky
Newky / dictbeingadick.py
Created May 11, 2013 17:35
Python dict weirdness.
# in settings.py
USERS: {
"alice": {
"alice_specific_information" : True
},
"bob": {
"bob_specific_information": True
}
}
@Newky
Newky / NiceNumber.py
Created May 7, 2013 21:24
A fun little python class which allows you to write big numbers with commas for readability. Just for fun, not to be used for anything serious.
class NiceNumber(long):
def __new__(cls, *args):
x = 0
if long(args[0]) >= 0:
sign = 1
else:
sign = -1
for i in range(0, len(args)):
reverse_i = (len(args) - 1) - i
x += (1000 ** i) * abs(long(args[reverse_i]))
@Newky
Newky / roll.py
Created March 27, 2013 20:30
Probability rolls for gav guidance
import random
def roll_dice():
return random.randint(1, 6)
def sum_two_dice():
sum_one = roll_dice()
sum_two = roll_dice()
@Newky
Newky / gist:5151640
Created March 13, 2013 12:30
restoring cwd with a decorator.
def restore_cwd(func):
"""
decorator to restore the current working directory
"""
def wrapper(*args, **kwargs):
cwd = os.getcwd()
results = func(*args, **kwargs)
os.chdir(cwd)
return results