Skip to content

Instantly share code, notes, and snippets.

View bradmontgomery's full-sized avatar

Brad Montgomery bradmontgomery

View GitHub Profile
@bradmontgomery
bradmontgomery / chmown.sh
Created August 14, 2010 03:00
A combination of chmod & chown
#!/bin/bash
# Usage: chmown [options] <chmod-args> <chown-user>[:<chown-group>]
chmown () {
options=''
while [ -n "$(echo $1 | grep '-')" ]; do
options="$options $1"
shift
done
@bradmontgomery
bradmontgomery / using-jQuery-to-add-forms-to-a-Django-formset.js
Last active May 14, 2023 01:24
Use jQuery to add forms to a django formset
/*
Given the following HTML (a form generated by a django formset), I want to be able to
clone the form elements inside the div#book-form-container, omitting any values
<form action="/books/add/" method="post">
<div id="book-form-wrapper"> <input type="hidden" name="form-TOTAL_FORMS" value="1" id="id_form-TOTAL_FORMS" /> <input type="hidden" name="form-INITIAL_FORMS" value="0" id="id_form-INITIAL_FORMS" />
<div class="book-form-container">
<input type="hidden" name="form-0-id" id="id_form-0-id" />
@bradmontgomery
bradmontgomery / colors.py
Last active October 1, 2019 16:08
Use PIL to find a list of colors in an image
import Image
def get_color_list(path_to_image):
"""
Returns a list of tuples for RGB colors: (R, G, B)
This is gonna be slow and memory-intensive. Use at your own risk.
Requires PIL
"""
def _get_color(t): return t[1]
@bradmontgomery
bradmontgomery / crontab
Created October 18, 2010 19:18
A handy reminder
#X-------------------- minute (0 - 59)
#--X------------------ hour (0 - 23)
#-----X--------------- day of month (1 -31)
#-------X------------- month (1-12)
#----------X---------- day of week (0-7) (sunday = 0 or 7)
@bradmontgomery
bradmontgomery / kill_attrs.py
Created November 11, 2010 23:12
A way to remove all HTML attributes with BeautifulSoup
from BeautifulSoup import BeautifulSoup
def _remove_attrs(soup):
for tag in soup.findAll(True):
tag.attrs = None
return soup
def example():
doc = '<html><head><title>test</title></head><body id="foo" onload="whatever"><p class="whatever">junk</p><div style="background: yellow;" id="foo" class="blah">blah</div></body></html>'
@bradmontgomery
bradmontgomery / float_to_decimal_bad.py
Created December 11, 2010 17:57
So, converting a float to a decimal yields a TypeError. You first have to convert to a string, then a decimal.
In [14]: Decimal(2.0 * 3.0)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/oldbrad/Downloads/carmenproject/<ipython console> in <module>()
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/decimal.py in __new__(cls, value, context)
650 if isinstance(value, float):
651 raise TypeError("Cannot convert float to Decimal. " +
--> 652 "First convert the float to a string")
@bradmontgomery
bradmontgomery / bleach_bug.py
Created January 16, 2011 02:26
Playing with bleach, and not sure if I'm doing it wrong.
>>> from bleach import Bleach
>>> from janitor import html_tags # my stuff
>>>
>>> html_tags.basic_document_tags
['a', 'abbr', 'acronym', 'blockquote', 'cite', 'code', 'dd', 'del', 'dfn', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img', 'ins', 'kbd', 'li', 'ol', 'p', 'pre', 'q', 'samp', 'strong', 'ul', 'body', 'DOCTYPE', 'head', 'html', 'link', 'meta', 'style', 'title']
>>>
>>> s = '<!DOCTYPE html><html><head><title>t</title></head><body><p><em>Some</em> stuff</p></body></html>'
>>>
>>> bl = Bleach()
>>> bl.clean(s, tags=html_tags.basic_document_tags)
@bradmontgomery
bradmontgomery / countdown.html
Created March 29, 2011 04:07
Stupid, simple countdown clock
<!DOCYTYPE html>
<html>
<head><title>countdown</title>
<style type="text/css">
body { padding: 2em; margin: 0px auto; background-color: white; color: #222; font-family: "Helvetica", "Arial", sans-serif;}
h1 {border-bottom: 1px solid #aaa; text-align: center; }
p#countdown { font-size: 150px; text-align: center; margin: 0; padding: 0;}
table { width: 50%; margin: 0 auto; }
thead > tr {background-color: #aaa; }
td, th { text-align: center; padding: 1em; border: 1px solid #ccc; }
class MyModel(models.Model):
status = models.IntegerField()
stuff = models.ForeignKey()
def save(self, force_insert=False, force_update=False):
#if something has happened with stuff:
# update status
super(MyModel, self).save(force_insert, force_update)
@bradmontgomery
bradmontgomery / count-mercurial-file-changes.txt
Created April 8, 2011 19:21
Counting the frequency with which files change in a mercurial repo
# Step 1: Create a style for the output of "hg log"
# See: http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html
# I put this in: ~/.hg/templates/fileonly
changeset = '{files}'
file = '{file}\n'
# Step 2: Use the above style and sort/count your output
hg log --style ~/.hg/templates/fileonly --date="2011-01-01 to 2011-04-01" | sort | uniq -c | sort -r