Skip to content

Instantly share code, notes, and snippets.

@mshuffett
mshuffett / metaclass.py
Created February 24, 2014 01:21
Python metaclass demonstration
class MetaCommand(type):
def __new__(meta, name, bases, dct):
print '-----------------------------------'
print "Allocating memory for class", name
print meta
print bases
print dct
return super(MetaCommand, meta).__new__(meta, name, bases, dct)
def __init__(cls, name, bases, dct):
print '-----------------------------------'
@mshuffett
mshuffett / singleton.py
Created February 24, 2014 01:43
Singleton metaclass
class _Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Singleton(_Singleton('SingletonMeta', (object,), {})): pass
@mshuffett
mshuffett / mechanize_browser.py
Created March 30, 2014 13:12
Emulate a browser using mechanize
import mechanize
import cookielib
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
@mshuffett
mshuffett / email.py
Created March 30, 2014 14:54
Send email using Gmail
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def sendmail(message, subject='subject', username='username', password='password', address='reciever'):
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = address
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
@mshuffett
mshuffett / Wikipedia.ipynb
Created May 23, 2014 23:21
IPython Notebook Using Wikipedia API
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mshuffett
mshuffett / README.md
Last active August 29, 2015 14:05 — forked from agnoster/README.md
Fork of Agnoster

agnoster.zsh-theme

A ZSH theme optimized for people who use:

  • Solarized
  • Git
  • Unicode-compatible fonts and terminals (I use iTerm2 + Menlo)

For Mac users, I highly recommend iTerm 2 + Solarized Dark

@mshuffett
mshuffett / flatten_timeit.py
Created April 5, 2013 02:03
Python timeit app for different methods of flattening a shallow list. list.extend is fastest across board. https://www.dropbox.com/s/lb7yojmcckndojm/flatten_graph.png
#!/usr/bin/env python2.6
#http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python/408281#408281
"""Usage: %prog item_count"""
from __future__ import print_function
import collections
import itertools
import operator
from timeit import Timer
@mshuffett
mshuffett / console.log.js
Created July 2, 2013 08:22
Make console.log object accessible.
console.oldLog = console.log;
console.log = function(value) {
console.oldLog(value);
window.$log = value;
};
@mshuffett
mshuffett / Object Flatten
Last active December 20, 2015 01:49 — forked from penguinboy/Object Flatten
flattens object in javascript and gives nice names
var flattenObject = function(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object') {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
@mshuffett
mshuffett / user_dir.sh
Created July 30, 2013 23:15
Create user directory for preexisting user.
#!/bin/bash
mkdir -p /home/mshuff
chown mshuff:mshuff /home/mshuff
usermod -d /home/mshuff mshuff