Skip to content

Instantly share code, notes, and snippets.

@pygeek
pygeek / dividends.py
Created May 10, 2012 04:40
Outputs divisors for a specified range of numbers. Prints 'PRIME' if number is prime.
#! /usr/bin/env python
import timeit
class Divisors():
""" Outputs dividends a specified range of divisors.
Prints 'PRIME' if number is prime.
Outputs other interesting data.
"""
def __init__(self):
upper_bounds = self.upper_bounds()
@pygeek
pygeek / maintain_widescreen_aspect.js
Created July 3, 2012 16:52
Maintains aspect ratio of widescreen video. change 'video' to be the tag, class, id, or combination of the three for element.
$(function(){
var el = $('video'),
maintain_aspect = function(){
el.each(function(){
var $width = $(this).innerWidth(),
$new_height = $width * .562;
$(this).css({'height' : $new_height})
})
};
$(window).resize(function(){
@pygeek
pygeek / PickleFieldMixin.py
Created August 22, 2012 03:25
Django PickleField CRUD Mixin
class PickleFieldMixin(object):
"""Django PickleField CRUD Mixin.
Replace 'field' with PickleField field.
Replace 'model' with appropriate model containing \
this mixin.
"""
def attr_create(self, key, value=None):
key = str(key)
@pygeek
pygeek / gist:3439434
Created August 23, 2012 17:53
Retrieve items in a list where half have a higher idx of current idx, and half have lower idx of current idx
floor((i + (i-n)) / 2); n = total and i=1
@pygeek
pygeek / maintain_aspect.js
Created October 3, 2012 07:04
Maintains aspect ratio as (jQuery plugin)
$.fn.maintainAspect = function(cb){
var el = this,
_maintain_aspect = function(el){
var $width = el.parent().width();
var $height = $width / 2.39;
el.attr('width', $width)
el.attr('height', $height)
//trigger callback
$(function(){
$('ul.primary').hover(function(){
$(this).children('ul.secondary').stop(true, true).toggle(function(){
})
})
})
<script>
(function($){
//Adding inputFocus to namespace for the placeholder shim
$.fn.inputFocus = function(text){
$(this).focusin(function(){
if($(this).val() == text){
$(this).val('')
}
})
$(this).focusout(function(){
@pygeek
pygeek / gist:4338297
Created December 19, 2012 17:01
stick footer to bottom of page regardless of (window height : content height)
$(function(){
var $footer_position = function(){
if($(document).height() <= $(window).height()){
$('footer').css({'position' : 'absolute',
'bottom' : '0px'
})
}else{
$('footer').css({'position' : 'static'})
}
};
@pygeek
pygeek / google_analytics.py
Created December 20, 2012 18:09
# Usage: # Define GOOGLE_ANALYTICS_CODE in your settings. # In your base template put: {% google_analytics %} Python 3 compatible ;-]
rom django import template
from django.conf import settings
register = template.Library()
# Usage:
# Define GOOGLE_ANALYTICS_CODE in your settings.
# In your base template put: {% google_analytics %}
@register.simple_tag
def google_analytics():
#credits - http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs
lg1 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative
lg2 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(bold white)— %an%C(reset)' --abbrev-commit
lg = !"git lg1"