Skip to content

Instantly share code, notes, and snippets.

ON = 1
OFF = 0
def switch(setting=OFF):
def switchdec(function):
if not hasattr(function, 'enabled'):
function.enabled = setting
def wrapper(*args,**kwargs):
if getattr(function, 'enabled'):
return function(*args,**kwargs)
@cessor
cessor / gist:61a5462e8a0ac38648d1
Last active August 29, 2015 14:16
Keyboard Function
(function (window) {
var Keyboard = function() {
var keyCodes = {};
var keyNames = {
// Controls
"backspace": 8, "tab": 9, "return": 13, "shift": 16, "ctrl": 17, "alt": 18, "capslocks": 20,
"esc": 27, "space": 32, "left": 37, "up": 38, "right": 39, "down": 40,
"page-up": 33, "page-down": 34, "end": 35, "home": 36,
"insert": 45, "delete": 46,
@cessor
cessor / gist:9cca2de1930ab7d7368e
Created March 6, 2015 19:52
How to keep your sanity when working with files in python.
import sys
reload(sys)
ENCODING = 'utf-8-sig'
sys.setdefaultencoding(ENCODING)
def read_file(path):
with codecs.open(path, 'r', ENCODING) as file_:
return file_.read()
def write(string, path):
@cessor
cessor / gist:4621d0b202a2205e27ce
Created June 1, 2015 08:36
Blank Tornado App
import os
import logging
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, StaticFileHandler, url
from tornado.options import define, options
from tornado import gen
define('port', default=8888)
@cessor
cessor / gist:7a9174b1c4bc67d2feb5
Created July 7, 2015 18:39
A decorator that parallelizes operations on a list by partitioning the list and working on the partitions in separate threads.
from __future__ import division
import functools
import itertools
import math
import threading
def partition(collection):
size = len(collection)
part_count = int(math.ceil(math.sqrt(size)))
from scipy import stats
import numpy as np
import pandas as pd
sns.set_context('talk')
for df in [1, 6, 120]:
dist = stats.t(df, loc=0, scale=1)
range_ = np.linspace(dist.ppf(0.01), dist.ppf(0.99), num=100)
pdf = pd.Series(dist.pdf(range_))
@cessor
cessor / printstmt.py
Last active November 3, 2022 08:51
Make python look like cpp
# For when you are pissed off that python 3 has no print statement.
# Use echo+ Instead. No Brackets.
class Print(object):
def __add__(self, other):
print(other)
return self
def __lshift__(self, other):
return self.__add__(other)
@cessor
cessor / alert_magic.py
Last active November 2, 2015 10:42
Bootstrap Alert Blocks Cell Magic for IPython Notebooks
from IPython.core.magic import register_cell_magic
from IPython.display import HTML
@register_cell_magic
def alert(line_parameter, cell):
'''
Enables Bootstrap Alert Blocks as Cell magic, like so:
In [1]: %%alert info
This is a message
@cessor
cessor / rules.md
Last active January 6, 2017 17:52
Object Oriented Rules

Rules

  • Everything is an object
  • The Object must be instantiated
  • If it is not instantiated, it is not an object *(e.g. print, console, math)
  • If it is not an object, wrap it in an object (e.g. max = Max(3,2,1))
  • Constructors accept only objects
  • Constructors store their arguments
  • Constructors have no logic
  • Constructors only accept objects that do not change
@cessor
cessor / calculator.py
Created January 7, 2017 09:47
Dijkstras Algorithm Calculator
class UnexpectedSymbol(Exception): pass
operations = {
'+': lambda a,b: a+b,
'-': lambda a,b: a-b,
'*': lambda a,b: a*b,
'/': lambda a,b: a/b
}
precedence = {