Skip to content

Instantly share code, notes, and snippets.

View pipermerriam's full-sized avatar

Piper Merriam pipermerriam

  • Ethereum Foundation
  • Boulder Colorado
View GitHub Profile
@pipermerriam
pipermerriam / monitor.py
Created May 8, 2014 16:42
Want to monitor the progress of a loop?
def monitor_progress(things, every=1000, message="Processing `{current}` of `{total}`"):
print "Beginning processing"
total = len(things)
for idx, thing in enumerate(things):
if not idx % every:
print message.format(current=idx, total=total)
yield thing
print "Finished processing"
if denom:
    x = total / denom
else:
    x = 0
try:
 x = total / denom
@pipermerriam
pipermerriam / bug.py
Last active January 1, 2016 16:49
Django Bug
from django.db import models
class Owner(models.Model):
pass
class Thing(models.Model):
owner = models.ForeignKey(Owner, related_name='things')
@pipermerriam
pipermerriam / tasks.py
Last active January 1, 2016 08:29
celery chaining issue
@celery.task
def first():
# generates an iterable
return 1, 2, 3
@celery.task
def second(thing):
"""
Takes a single item from the iterable from `first`, does some computation,
@pipermerriam
pipermerriam / emails.py
Created June 12, 2013 17:17
An example of sending a password reset email in django as a class-based email.
from urlparse import urljoin
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import int_to_base36
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from emailtools import MarkdownEmail
@pipermerriam
pipermerriam / pycon.py
Created March 13, 2013 02:55
Wanted to scrape some data about the pycon talks I was interested in.
import requests
import csv
from dateutil import parser
from BeautifulSoup import BeautifulSoup
CACHE = {}
"""
https://us.pycon.org/2013/schedule/presentation/35/
https://us.pycon.org/2013/schedule/presentation/68/
@pipermerriam
pipermerriam / fields.py
Created February 6, 2013 15:54
Various django objects and helpers.
class ClearableFileInput(widgets.ClearableFileInput):
"""
Changes the default file widget to be easier to target with CSS
"""
template_with_initial = u'<div class="fileField"><span class="currently">%(initial_text)s:</span> %(initial)s %(clear_template)s<span class="change">%(input_text)s:</span> %(input)s</div>'
template_with_clear = u'<div class="clear">%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label></div>'
initial_template = u'<a href="{{url}}">{{name}}</a>'
def render(self, name, value, attrs=None):

Pattern for email sending.

  • Easy to grep project for where emails get sent.
  • Easy to send the same email from multiple places.
  • Constructs urls in a reliable way such that you don't get ssl errors for missing www.
from django.utils.http import is_safe_url
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
class WithNextUrlMixin(object):
"""
This view will use whatever value was submitted as `next` as the success
url, as long as the url resolves correctly.
"""
@pipermerriam
pipermerriam / mixins.py
Created November 7, 2012 16:07 — forked from vdboor/mixins.py
Django view initialization ordering issues
class BaseViewMixin(object):
def dispatch(self, request, *args, **kwargs):
# Set earlier to ensure that other class methods which expect
# these values to be present can be called in `init`
self.request = request
self.args = args
self.kwargs = kwargs
# Run the complete request, returning a response, but allowing
# `init` to either raise exceptions, or hijack the response if