Skip to content

Instantly share code, notes, and snippets.

View ryanwilsonperkin's full-sized avatar
😅

Ryan Wilson-Perkin ryanwilsonperkin

😅
View GitHub Profile
@ryanwilsonperkin
ryanwilsonperkin / git_up_git_out.sh
Created June 3, 2014 17:18
Version control the way OutKast intended.
# http://www.youtube.com/watch?v=CssC-DY4lO8
git config alias.up commit
git config alias.out push
git config alias.something pull
@ryanwilsonperkin
ryanwilsonperkin / ldapuser.sh
Created September 13, 2014 02:06
Register a shell function called ldapuser to search the University of Guelph's LDAP server for a record with the given username.
# Use as `ldapuser username` to lookup ldap record for "username"
function ldapuser {
ldapsearch -x -H ldap://directory.uoguelph.ca -b "ou=People,o=uoguelph.ca" "uid=$1"
}
@ryanwilsonperkin
ryanwilsonperkin / deep_get.py
Last active March 8, 2016 17:22
Python method for fetching deeply nested dict keys à la ImmutableJS.getIn
def deep_get(o, keys):
return reduce(lambda prev, current: prev.get(current, {}), keys, o) or None
import random
class ShittyList(list):
def __len__(self):
min, max = 0, super(ShittyList, self).__len__()
return random.randrange(min, max)
@ryanwilsonperkin
ryanwilsonperkin / comprehension.js
Created June 8, 2016 15:05
Legibility vs Cleverness
// Clever
function comprehension(arr, fn) {
return arr.reduce((obj, key) => Object.assign(obj, {[key]: fn(key)}), {});
}
// Legible
function comprehension(arr, fn) {
const obj = {};
for (key of arr) {
obj[key] = fn(key);

Keybase proof

I hereby claim:

  • I am ryanwilsonperkin on github.
  • I am ryanwilsonperkin (https://keybase.io/ryanwilsonperkin) on keybase.
  • I have a public key whose fingerprint is 5664 AF96 D17B 16E8 F21B F431 FBA8 1CE4 75C6 7F0D

To claim this, I am signing this object:

# Having fun with context managers
from contextlib import contextmanager
@contextmanager
def deity():
print 'The Father'
yield
print 'The Holy Ghost'
god = deity()
@ryanwilsonperkin
ryanwilsonperkin / webpack.config.js
Created February 23, 2017 18:06
Webpack: chunkhash in production, hash in development
const PRODUCTION = process.env.NODE_ENV === 'production';
const HASH_MODE = PRODUCTION ? 'chunkhash' : 'hash';
module.exports = {
output: {
filename: `[name].[${HASH_MODE}].js`,
chunkFilename: `[name].[${HASH_MODE}].js`,
},
...
};
@ryanwilsonperkin
ryanwilsonperkin / decorators.py
Last active November 23, 2017 22:01
Different types of decorators
# Regular decorator
def decorator(f):
def wrapped(*args, **kwargs):
print('Before')
result = f(*args, **kwargs)
print('After')
return result
return wrapped
# Regular decorator usage

Profiling!

A simple way to get line profiling on your python functions.

  1. pip install line_profiler
  2. Decorate your function with @profile (no import needed)
  3. Launch a shell with kernprof -l manage.py shell
  4. Call the function you want to profile. Preferably several times! (%timeit is great for running it several times)
  5. Exit the shell. You'll see a dumped file created called manage.py.lprof