Skip to content

Instantly share code, notes, and snippets.

@Glench
Glench / charCount.js
Last active December 11, 2015 06:58
Counting characters in a string for javascript
function charCount(str) {
if (!str || typeof str !== 'string') {
throw 'must provide a string';
}
var letter,
countObj = {},
alphabeticalLetters = [],
outputString = '',
letterCount;
@Glench
Glench / geometric_mean.py
Created January 24, 2013 18:59
Geometric mean function for python. Takes the nth root of all values in an iterable multiplied together. There might be some issues with this.
import operator
def geometric_mean(iterable):
return (reduce(operator.mul, iterable)) ** (1.0/len(iterable))
@Glench
Glench / tv_tropes_common_tropes.py
Last active January 8, 2021 08:32
A command-line script to find the common tropes of two or more media items. To use, you need to install the python libraries 'pattern' and 'pyquery'. Then use like this, passing in media names or links to tv tropes pages: > python tv_tropes_common_tropes.py 'My Little Pony Friendship is Magic' 'Hamlet'
#!/usr/bin/python
# a script to get all the common tropes for media from tv tropes
# usage:
# python tv_tropes_matcher.py name1 name2 [name3...nameN]
# please put names with spaces or special characters in quotes
# you can also pass in the urls if it won't automatch by name.
# pip install pattern
# pip install pyquery
@Glench
Glench / gui_raw_input.py
Created February 19, 2013 15:03
replace raw_input in python with GUI dialog popup text box
import easygui # easy_install easygui
some_input = easygui.enterbox(
msg="Enter some text below!",
title="Title of window",
strip=True, # will remove whitespace around whatever the user types in
default="This will show up in the box by default")
@Glench
Glench / percentChance.js
Last active December 14, 2015 17:59
calculate percentage chance, returning true or false. basically equivalent to spinning a roulette wheel and telling you if you won on that round or not.
var percentChance = function(integerPercent) {
return Math.random()*100 < integerPercent;
};
@Glench
Glench / watch_notebooks.py
Created March 23, 2013 15:46
a python script that watches the current directory for changes to ipython notebooks, compiles them to html, and uploads them via scp to a server. an experiment in real-time coding for teaching python to new programmers
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import envoy
import os
import datetime
class NotebookConverterHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path.endswith('.ipynb'):
@Glench
Glench / yo_mama.md
Last active December 15, 2015 08:09
Yo Mama's Driving Directions to Her House, From the East (from the amazing book you should totally buy "A Portrait of Yo Mama as a Young Man")

Yo Mama's Driving Directions to Her House

From the East

Take I-30 eastbound to Exit 52 or 59. Go left for about forty miles. Now make a left and you should see a man sellling newspapers on the south corner. Don't be alarmed if he's not there, though; sometimes a different guy fills in for him. Continue on Comstock for 3.2 miles, and when it hits Towerview (Road or Place--I forget which, but no matter, because those two streets are /not/ parallel), make a full ninety-degree turn at my friend Patty Ann's place. (Be careful, because the sign really jumps out at you.) Continue for about eighty or ninety minutes until you see a gravel road (if you see a stop sign that is slightly lighter than the usual hue you've gone about seventy miles too far). Turn left onto the access road (it'll look like you're heading off a cliff), then drive a considerable distance forward and take I-80 West (Scranton) to Exit 65B/I-195 South (Vancouver). You idiot! Make a narrow ri

@Glench
Glench / why_you_need_functions.py
Last active December 15, 2015 12:59
A new-to-programming student asked me why you would use functions. I gave this example and I feel like this explains it well, building up abstraction from a concrete example to a more powerful, general one. For the record, you shouldn't use '+' to put strings together and you shouldn't do language translation this way. This is just to illustrate…
def say_hello_glen():
return 'hello, glen'
def say_hello(name):
return 'hello, ' + name
def greet(name, language):
if language is 'spanish':
return 'hola, ' + name
if language is 'english':
@Glench
Glench / feelinglucky.xml
Created May 30, 2013 16:22
I'm Feeling Lucky Search in Chrome's Omnibar *with suggestions*. Open up javascript console ("developer tools") in chrome and run window.external.AddSearchProvider('URL TO RAW XML')
<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
<ShortName>Google I'm Feeling Lucky</ShortName>
<Description>Google I'm Feeling Lucky Instant</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:image/x-icon;base64,R0lGODlhEAAQAPfLAAATVikwdA8SnxUfgAsWpAAilholjxw4jBc7kwAlvQQ2sRMsoBUqqhMzuhY/vxw4tSgmiyM1mSUztiQ6sTE3sQ4qyxMxxRoyxiAuxR1CtBxJsBxasSJuuTFguBte0Rlf2xVc9h9W9xVjzxVr0gdj6BRh4R1o5yBcyiZbyydT1i9b2Ddb1iFY6CJg2Vpor1dzvEJu20Z0yi23QDy1REi2OUy0O1WzOVC4PU+tVUe5Sk2xQU2zRUO4UE21Ula2SmKEqWWF2HyPx2+a6X6e6Xqk1m+s78sUDs4UGdEQB9YfDdwaANEfHd0YEscjAM4mAM0qANIoD9IkGdslGswuItYgL4aP0ImP2YGZ36Opzaq2wq/S+rzX/7/e8MrS1MLO/sTb48rT8snX/83c89PZ+crq+cH1/9Dl/9Ln/93r/9fy/+Hf7P/42eDm/O7u/+T29uX2/eT2/+f4/+f5/+j/9u//8+3/9u7/9ur5/+j//+n//+v//u3//+7//e7//+////b66/T/6vX/6/f/7f/07fj/4fv/4Pj/5v/45v7/4/r+7/3/6fDw+Pfx//D/9/X/8fT/8/f/8ff/8/D///H///L8/fL///P///X7//b6/ff/+/T///b9//f///v19//w9v/09P/29v/x+f/y///z///1+v/1///2///3//j79P/58/z/8/z99/z/9v7/9P7/9vn7//v6//j9//n9//j///n//
@Glench
Glench / python-async-callback.py
Last active August 2, 2021 10:34
An example of how to vary a parameter for an async callback function in python. Hint: don't use lambda functions in loops!
"""
We want to run a function asychronously and run a
callback function with multiple parameters when it
returns!
In this example, we are pretending we're analyzing
the names and ages of some people. We want to print
out:
jack 0