Skip to content

Instantly share code, notes, and snippets.

View amontalenti's full-sized avatar

Andrew Montalenti amontalenti

View GitHub Profile
@amontalenti
amontalenti / strange_sorting.py
Last active September 27, 2015 11:48
strange Python sorting behavior
>>> a = [1, 2]
>>> b = (1, 2)
>>> a < b
True
>>> b > a
True
>>> b < a
False
>>> c = [
... (1, 2, 3),
@amontalenti
amontalenti / pypy_micro.py
Created January 15, 2012 01:14
better microbenchmark for pypy
def fib(n):
if n <= 1:
return n
else:
return fib(n-1) + fib(n-2)
def main():
for i in xrange(36):
print "n=%d => %d" % (i, fib(i))
renderRecommendations({
"data": [
{
"pub_date": "2012-02-17T17:31:33Z",
"link": "http://write.arstechnica.com/published/apple/news/2012/02/mt-preview-83bb1965c86a7f89c5e198f4f8c1582ff21e76c4.ars",
"title": "Week in Apple: Mountain Lion surprise attack",
"thumb_url_medium": "http://c0001566.cdn1.cloudfiles.rackspacecloud.com/medium_d84cc0f61eb70b89d6f65e9e5c790e321354e853.jpg",
"author": "Jacqui Cheng"
},
{
@amontalenti
amontalenti / shorten.py
Created March 26, 2012 12:06
basic shortener implementation
def to_base(q, alphabet):
if q < 0: raise ValueError, "must supply a positive integer"
l = len(alphabet)
converted = []
while q != 0:
q, r = divmod(q, l)
converted.insert(0, alphabet[r])
return "".join(converted) or '0'
def to62(q):
@amontalenti
amontalenti / newsblur_add.js
Created August 26, 2012 18:00
NewsBlur bookmarklet that adds domain of current article to NewsBlur
javascript:(function() { var u = location.href, a = document.createElement('a'); a.href = u; location.href = "http://www.newsblur.com/?url=" + encodeURIComponent(a.hostname); }())
@amontalenti
amontalenti / script-inject-http-proxy.js
Created November 21, 2012 17:16
script injecting proxy for Node.JS
var httpProxy = require('http-proxy');
var url = require('url');
httpProxy.createServer(function(req, res, proxy) {
var isHtml = false,
write = res.write,
writeHead = res.writeHead,
params = url.parse(req.url, true).query,
dest = params.dest || 'localhost',
@amontalenti
amontalenti / requirements.txt
Last active December 14, 2015 21:09
rapid web prototyping requirements
# ipython
ipython==0.13.1
# ipython notebook
pyzmq==13.0.0
tornado==2.4.1
# livereload
livereload==0.14
@amontalenti
amontalenti / hotclicks.py
Last active December 15, 2015 15:49
April Fools joke in 2013 :)
def calculate_hotclicks(item):
return item.clicks \
or \
((lambda n:\
reduce(\
lambda x,y:(x[0]+x[1],x[0]),[(1,1)]*(n-2))[0])\
(3)-1)
@amontalenti
amontalenti / papers2_sync.py
Created April 21, 2013 18:19
Synchronize my PDF store of ebooks with my Papers2 SQLite3 database on my Mac. Replaces the title of each book with the filename on-disk, since I have my own naming convention that works much more nicely.
import sqlite3
import sys
conn = sqlite3.Connection("Database.papersdb")
query = "SELECT pub.ROWID, pdf.path, pub.title FROM Publication pub, PDF pdf WHERE pdf.object_id=pub.ROWID"
for row in conn.execute(query):
rowid, path, title = row
newtitle = path.replace("Files/", "").replace(".pdf", "")
sql = ('UPDATE Publication SET title=? WHERE ROWID=?', (newtitle, rowid))
print sql
conn.execute(*sql)
@amontalenti
amontalenti / duckduckgo.txt
Created September 23, 2013 16:38
duckduckgo internal redirect to preserve referer domain while stripping keywords
http://r.duckduckgo.com/l/?kh=-1&uddg=http%3A%2F%2Fwww.journalism.co.uk%2Fnews%2F-dark-social-and-other-mysterious-analytics%2Fs2%2Fa554142%2F