Skip to content

Instantly share code, notes, and snippets.

@tomviner
tomviner / spottylotty.py
Created September 9, 2011 15:43
Spotify Ticket Lottery
from __future__ import division
import sys
import math
import random
# http://www.spotify.com/us/jobs/tech/ticket-lottery/
def bc(n, k, f=math.factorial):
"""Binomial coefficient in terms of Factorials
(n) = ____n!____
@tomviner
tomviner / q1.py
Created September 16, 2011 08:47
Anagram of Palindrome
# -*- coding: utf-8 -*-
"""
1) A string is a palindrome if it reads the same from left-to-right as it does right-to-left.
e.g “nolemonnomelon”, “racecar” & “neveroddoreven” are all palindromes.
A string is an anagram of another string if it consists of exactly the same characters but in another order.
e.g The string “carrace” is an anagram of “racecar”.
Write a function `def is_anagram_of_palindrome(str):`
such that it returns True if the string str is an anagram of a palindrome, and otherwise returns False.
You may assume that str contains only lowercase characters a-z and is not empty.
e.g Given str = “carrace” the function should return True as it is an anagram of the palindrome “racecar”.
@tomviner
tomviner / fabfile.py
Created September 17, 2011 22:51
VirtualEnvWrappers with Fabric
def once(s):
"command_prefixes is a list of prefixes"
if s not in env.command_prefixes:
return s
return 'true'
@contextlib.contextmanager
def mycd(dir):
with prefix(once('cd %s' % dir)):
yield
@tomviner
tomviner / guardiancartoons.ics
Created September 18, 2011 19:35
Guardian Cartoons as Google Calendar Widgets
BEGIN:VCALENDAR
X-WR-CALNAME;VALUE=TEXT:guardiancartoons
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTART;VALUE=DATE:20110917T00:00:00Z
DTEND;VALUE=DATE:20110918
SUMMARY:Martin Rowson on the arrest of UBS 'rogue trader' - cartoon
X-GOOGLE-CALENDAR-CONTENT-TITLE:Martin Rowson on the arrest of UBS 'rogue trader' - cartoon
X-GOOGLE-CALENDAR-CONTENT-ICON:http://www.guardian.co.uk/favicon.ico
@tomviner
tomviner / formatted_django_sql.py
Created January 4, 2012 17:21
Print django database query with a little formatting
# eg qs = Product.objects.exclude(is_prebuy=True)
print re.sub(r'\b([A-Z]+)\b', r'\n\1\n\t', str(qs.query))
@tomviner
tomviner / gist:3175280
Created July 25, 2012 09:29
sublime settings
{
"auto_complete_commit_on_tab": true,
"detect_slow_plugins setting": false,
"dictionary": "Packages/Language - English/en_GB.dic",
"find_selected_text": true,
"fold_buttons": false,
"folder_exclude_patterns":
[
".svn",
".git",
@tomviner
tomviner / pretty.py
Last active November 29, 2021 12:42
Parse ps output to show top CPU, memory and process count, summed over identically named processes
import string
def pretty_size(size):
"size in bytes"
size = int(size)
units = ' KMGTPE'+string.lowercase[::-1]
bytes = 'B'
i = 0
while size>1024:
@tomviner
tomviner / Install-instructions-for-mac.txt
Created August 28, 2012 09:34
Homemade 2012 ticket notifier
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
curl -O http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz
tar xzf pip-0.7.2.tar.gz
cd pip-0.7.2
python setup.py install
easy_install pip
pip install requests
@tomviner
tomviner / s3_test.py
Created September 11, 2012 14:30
Simple example of an S3 django storage backend saving a file from local and url
import os
import urllib2
import contextlib
import StringIO
from django.core.files.storage import get_storage_class, FileSystemStorage
from django.core.files import File
from django.conf import settings
from galleries.models import GalleryImage
@tomviner
tomviner / gist:4285213
Created December 14, 2012 12:41 — forked from seb-thomas/gist:4285197
with if?
$.getJSON('media/js/markers.json', function(data){
$.each(data.markers, function(i, marker){
if (...) {
map.addMarker({
id: i,
tags: marker.tag,
lat: marker.latitude,
lng: marker.longitude,
infoWindow: {
content: '<h2>' + marker.name + '</h2><p>' + marker.address + '</p>'