Skip to content

Instantly share code, notes, and snippets.

View mrchilds's full-sized avatar

Wesley Childs mrchilds

View GitHub Profile
@mrchilds
mrchilds / gist:1729289
Created February 3, 2012 09:30
Strip all HTML from string using BeautifulSoup
from BeautifulSoup import BeautifulSoup
body = "<p>Dear Everyone,</p><p>This is a test</p><h1>Test Complete</h1>"
plain_text = ' '.join(BeautifulSoup(body).findAll(text=True))
@mrchilds
mrchilds / gist:2020824
Created March 12, 2012 09:09
MAMP - MySQL server has gone away query
MAMP:
/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
set global net_buffer_length=1000000;
set global max_allowed_packet=1000000000;
/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot < ~/Downloads/DUMP
@mrchilds
mrchilds / gist:2399003
Created April 16, 2012 13:59
Python and Money
Work out total and then round up to 2 decimal places WITHOUT dropping the final zero....
from decimal import *
cost = "12.56896"
cost = Decimal(cost).quantize(Decimal('.01'), rounding=ROUND_UP)
@mrchilds
mrchilds / gist:3061165
Created July 6, 2012 16:25
Django - basic ajax
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson
def myajax(request):
#Some logic
html = render_to_string(TEMPLATE, context)
return HttpResponse(simplejson.dumps({"html": html}),
content_type="application/json; charset=utf-8")
@mrchilds
mrchilds / gist:3121438
Created July 16, 2012 07:58
Django South - Quick Help
# Convert Existing App
$ python manage.py convert_to_south APP_NAME --settings settings_debug
# Perform model change on development
$ python manage.py schemamigration APP_NAME --auto --settings settings_debug
$ python manage.py migrate APP_NAME --settings settings_debug.py
# Perform change on live server (assumes migration was applied and checked in on development)
@mrchilds
mrchilds / gist:3150241
Created July 20, 2012 11:25
Mysql CSV Import
Example File
AB101AA,394251,806376,57.1482995075,-2.09663094048
Import file from command line:
$ mysqlimport -u root -p --local --fields-terminated-by=',' DATABASE FILENAME
@mrchilds
mrchilds / gist:3164367
Created July 23, 2012 15:56
Mysql - Add New User With Specific DB Permissions
#Connect to mysql command line...
$ mysql -u root -p
#Create user
$ create user USERNAME;
#Assign Permissions
$ grant PERMISSIONS on DATABASE.* to 'USERNAME'@'localhost' identified by 'PASSWORD';
@mrchilds
mrchilds / gist:3394025
Created August 19, 2012 09:50
Django Unit Test - Ajax
from django.utils import simplejson
#Go to a url
response = self.client.get(self.url)
foobar = simplejson.loads(response.content)
print foorbar['item']
@mrchilds
mrchilds / gist:3776003
Created September 24, 2012 13:40
Django Performance Fact Finding
Very quick way to get some timing info...
import time
class Timer():
def __enter__(self): self.start = time.time()
def __exit__(self, *args): print time.time() - self.start
with Timer():
some_code()
@mrchilds
mrchilds / gist:5886088
Created June 28, 2013 16:39
Puppet - Templates - Join list
# Useful when creating lists in config files...
<%= my_list.join(',') %>