Skip to content

Instantly share code, notes, and snippets.

@jsatt
jsatt / gist:5853801
Created June 24, 2013 21:33
running average in python
def running_sum_generator(iterable):
sum = 0
for i in iterable:
if not i:
continue
sum += i
yield sum
def running_avg(value_list):
@jsatt
jsatt / selenium_speed.py
Created June 25, 2013 14:43
Script for running page speed tests with selenium
from datetime import datetime
from math import sqrt
from time import sleep
import csv
import logging
import urlparse
from selenium import selenium
SELENIUM_BROWSERS = ['firefox']
@jsatt
jsatt / gist:6042104
Last active December 20, 2015 00:29
Class for dynamically generating a Django QuerySet from a list of Django objects of the same type.
from django.db.models.query import QuerySet
class QuerySetFromIter(QuerySet):
def __init__(self, objects=[], model=None, query=None, *args, **kwargs):
if model is None:
if len(objects):
model = type(objects[0])
else:
# TODO: find a better way to handle this
@jsatt
jsatt / venv_node
Last active December 21, 2015 07:38
A script for managing and installing nodejs within a Python virtualenv.
#!/bin/bash
TASK=$1
node_checksum() {
if [ "$1" = "$2" ]; then
return
elif [ -z $2 ]; then
echo 'Checksums empty' #missing in raspberry pi binary
return
@jsatt
jsatt / gist:6391723
Last active April 13, 2016 13:31
stderr and stdout to file
# stdout to file
echo test > file.txt
# stdout append to file
echo test >> file.txt
# stderr to file
cmd 2> file.txt
# stderr to stdout
@jsatt
jsatt / gist:6391778
Created August 30, 2013 16:36
Build and upload Python package to pypi
python setup.py sdist bdist_egg upload
@jsatt
jsatt / gist:6391787
Created August 30, 2013 16:36
Access Redis from pyshell
from redis import Redis
r = Redis('host', db=1, password='pass')
r.keys(':1:cachekey*')
@jsatt
jsatt / rand_string.py
Created August 30, 2013 16:38
Generate a random string in python
import random
import string
rand_string = ''.join(random.choice(string.ascii_letters + string.digits)
for i in xrange(random.randint(6, 20)))
date = now().replace(hour=0, minute=0, second=0, microsecond=0)
@jsatt
jsatt / gist:7414231
Created November 11, 2013 14:47
Select by row
SELECT * FROM [table name] LIMIT [row id], 1;