Skip to content

Instantly share code, notes, and snippets.

View saevarom's full-sized avatar

Sævar Öfjörð Magnússon saevarom

View GitHub Profile
@saevarom
saevarom / rails3
Created February 22, 2012 09:49
rvm shortcut
#!/bin/bash
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
@saevarom
saevarom / gist:2143022
Created March 21, 2012 00:34
md5sum for directory and check
find . -type f -print0 | xargs -0 md5sum > checksums_backup.md5
#check the data
md5sum -c checksums_backup.md5
@saevarom
saevarom / gist:3003464
Created June 27, 2012 11:24
Update mailchimp list by django group
from django.conf import settings
from mailsnake import MailSnake
ms = MailSnake(settings.MAILCHIMP_API_KEY)
from django.contrib.auth.models import Group
group_name = 'group-name'
list_id = 'list-id'
group = Group.objects.get(name=group_name)
for user in group.user_set.all():
@saevarom
saevarom / gist:3654490
Created September 6, 2012 10:22
To unicode or bust
# From "Unicode in Python, Completely Demystified" by Kumar McMillan.
# http://farmdev.com/talks/unicode/
def to_unicode_or_bust(
obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
@saevarom
saevarom / forms.py
Created October 15, 2012 15:17
PlaceholderAttributeMixin
class PlaceholderAttributeMixin(object):
def __init__(self, *args, **kwargs):
super(PlaceholderAttributeMixin, self).__init__(*args, **kwargs)
for field_name in self.fields:
field = self.fields.get(field_name)
if field:
if type(field.widget) in (forms.TextInput, forms.DateInput):
field.widget = forms.TextInput(attrs={'placeholder': field.label})
elif type(field.widget) in (forms.PasswordInput,):
field.widget = forms.PasswordInput(attrs={'placeholder': field.label})
@saevarom
saevarom / ajaxSetup.js
Created December 13, 2012 00:35
Django ajax setup for csrf
$.ajaxSetup({
beforeSend: function(xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
@saevarom
saevarom / index.html
Last active December 15, 2015 09:09
D3 example
<html>
<head>
<script src="http://nvd3.org/lib/d3.v2.js"></script>
<script src="http://nvd3.org/lib/fisheye.js"></script>
<script src="http://nvd3.org/nv.d3.js"></script>
<link href="http://nvd3.org/css/common.css" rel="stylesheet">
<link href="http://nvd3.org/src/nv.d3.css" rel="stylesheet">
<link href="http://nvd3.org/css/syntax.css" rel="stylesheet">
<script>
/*
decimal_sep: character used as deciaml separtor, it defaults to '.' when omitted
thousands_sep: char used as thousands separator, it defaults to ',' when omitted
*/
Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep)
{
var n = this,
c = isNaN(decimals) ? 2 : Math.abs(decimals), //if decimal is zero we must take it, it means user does not want to show any decimal
d = decimal_sep || '.', //if no decimal separator is passed we use the dot as default decimal separator (we MUST use a decimal separator)

Automatically use rvm from a file called .rvm within a folder. Sample contents for .rvm file:

2.0.0@mygemset

Automatically switch to virtualenv using a name from a file called .venv in a folder. Simply write the name of the virtual environment to the .venv file.

Automatically source env variables from a file called .exports Must have valid bash syntax:

@saevarom
saevarom / LICENSE
Created August 29, 2013 19:12 — forked from dfm/LICENSE
Copyright (c) 2012–2013 Daniel Foreman-Mackey
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all