Skip to content

Instantly share code, notes, and snippets.

View ashchristopher's full-sized avatar

Ash Christopher ashchristopher

  • Shopify
  • Toronto, Ontario
View GitHub Profile
@ashchristopher
ashchristopher / simple_read_file
Created April 28, 2012 17:58
Simple reading of file with python
with open('data.txt', 'r') as f:
for line in f.readlines():
print line
@ashchristopher
ashchristopher / mem_report.sh
Created April 28, 2012 04:20 — forked from epicserve/mem_report.sh
A quick script for printing out memory usage of various services
CELERY=`ps -A -o pid,rss,command | grep celeryd | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
GUNICORN=`ps -A -o pid,rss,command | grep gunicorn | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
REDIS=`ps -A -o pid,rss,command | grep redis | grep -v grep | awk '{total+=$2}END{printf("%d", total)}'`
NGINX=`ps -A -o pid,rss,command | grep nginx | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
OTHER=`ps -A -o pid,rss,command | grep -v nginx | grep -v celeryd | grep -v gunicorn | grep -v redis | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
websites=`ps -A -o user,pid,rss,command | grep gunicorn | egrep -o "[a-z_]+\.py$" | sort | uniq | perl -wpe 's|\.py$||;' | xargs`
printf "%-10s %3s MB\n" "Celery:" $CELERY
printf "%-10s %3s MB\n" "Gunicorn:" $GUNICORN
printf "%-10s %3s MB\n" "Nginx:" $NGINX
printf "%-10s %3s KB\n" "Redis:" $REDIS
@ashchristopher
ashchristopher / test.py
Created April 6, 2012 20:52 — forked from joestump/test.py
A drop-in replacement for Django's test mange.py command that uses coverage.
from django.conf import settings
from django.core.management.base import CommandError
from optparse import make_option
import re
import sys
try:
from south.management.commands.test import Command as BaseCommand
except ImportError:
@ashchristopher
ashchristopher / python-lock.py
Created March 22, 2012 19:42
Python Lock (Disqus)
class UnableToGetLock(Exception):
pass
class Lock(object):
"""
Uses the defined cache backend to create a lock.
>>> with Lock('key name'):
>>> # do something
@ashchristopher
ashchristopher / test-coverage.py
Created January 22, 2012 02:40 — forked from acdha/test-coverage.py
A custom Django test runner with support for coverage.py and graceful handling for app selection and various testing gotchas
#!/usr/bin/env python
"""
Run Django Tests with full test coverage
This starts coverage early enough to get all of the model loading &
other startup code. It also allows you to change the output location
from $PROJECT_ROOT/coverage by setting the $TEST_COVERAGE_OUTPUT_DIR
environmental variable.
"""
@ashchristopher
ashchristopher / django_raw_update.py
Created January 10, 2012 14:26
Django RAW Update
def raw_update(self, *args, **kwargs):
"""Use the manager 'update' method for a given set of fields.
Values in args are the names of the model fields which should be updated db-side
Kwargs indicate new values for model fields that should be updated on the model *and* in the db.
The following are equivalent:
>>> self.raw_update('foo', bar='baz')
and
>>> self.bar = 'baz'
@ashchristopher
ashchristopher / README.rst
Created December 15, 2011 19:22 — forked from wolever/README.md
EMPMYMIP: Easily Maintain a PyPI Mirror of Your Important Packages!

EMPMYMIP: Easily Maintain a PyPI Mirror of Your Important Packages!

Status

EMPMYMIP seems to mostly kind of work for me. It might even do the same for you!

The implementation is kind of a huge hack, though, and it might very well

@ashchristopher
ashchristopher / jquery.500frame.js
Created August 12, 2011 19:35 — forked from defrex/jquery.500frame.js
pop-up any 500s in an iframe using jQuery. Especially useful for Django errors.
$(document).bind('ajaxError', function(e, jqXHR){
if (jqXHR.status == 500){
var erframe = document.createElement('iframe');
$('body').append(erframe);
$(erframe).css({
'position': 'absolute',
'top': '5%', 'left': '50%',
'width': '90%', 'height': '90%',
'marginLeft': '-45%',
'z-index' : '9999999'
@ashchristopher
ashchristopher / WriteUnicodeToFileInPython
Created May 26, 2011 14:00
Write unicode data to file in python
import codecs
f = codecs.open('/tmp/myfile.csv', 'w', "utf-8")
# now use f as you would any file object
@ashchristopher
ashchristopher / test.py
Created May 26, 2011 03:18 — forked from joestump/test.py
A drop-in replacement for Django's test mange.py command that uses coverage.
from django.conf import settings
from django.core.management.base import CommandError
from optparse import make_option
import re
import sys
try:
from south.management.commands.test import Command as BaseCommand
except ImportError: