Brad's Blog

web dev, django, running, food, or whatever

The little things 2013-12-29

I ran across an interesting line of code today, and thought I'd share some insights. First, though we need a little context. Imagine reading several lines of data from a csv file (using python's built-in csv module). You'll typically have some code that looks something like this: import csv with open('data.csv', 'rb') as csvfile: reader = csv.reader(csvfile) for row in reader: # Do some stuff with each row, # where the row is a list of ...

problems with django_extension's graph_models? 2013-11-15

I recently ran into an issue when trying to generate an image of my project's models using django_extension's graph_models command. Unfortunately, googling for the error didn't turn up any solutions, so I'm dumping some info here (just in case!). some background For the record, I was using django_extensions, version 1.2.5 (the latest release as of this post), and Django 1.4.2 (yeah... it's old) Running the following command:$ ./manage.py graph_models my_app ...

Getting the Most out of ack 2013-10-11

ack is an amazing tool. If you're not familiar with it, it's a lot like grep (the official site is at beyondgrep.com). It's a command-line tool that helps you find things in files, particularly source code. However, like a lot of command-line tools, it's all-too-easy to learn just the basics and forget that many of these tools can do so much more! Without further ado, here are a few of the ways I frequently use ...

An Attribute by any other name... 2013-09-11

Let's explore some python attributes, shall we? (note: this is python 2.7.x) Attributes Let's consider a simple class, N, with a single attribute, numbers containing values 0 - 9. class N(object): numbers = range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] We can create an instance of this class, then perform some operations on the attribute (like accessing or setting its values). >>> n = N() >>> n.numbers # get the value of the ...

Metrics with Django and Redis 2013-05-18

So, you've got a shiny new Django-powered site, and now you'd like to start recording metrics. Perhaps you've read The Lean Startup, and you know you've got to know how people are using your site in order to know what to improve. Perhaps you're just a data nerd and you like to count things. Either way, there are ton's of ways to measure things on your site. You could use third-party applications like Google ...

String Theory 2013-05-09

I'm not a physicist, but I've got some views on string theory that I'd like to share. Here goes: Brad's String Theory: Axiom I While in motion, strings will become untied, regardless of advanced techniques employed to fasten them. Brad's String Theory: Axiom II When at rest (i.e. upon completion of aforementioned motion), strings will be resistant to any attempt at unfastening. These statements are, to best of my knowledge, universally true, based on ...

Django Manager Testing Woes 2013-04-07

I've recently run into some strange behavior while testing some custom Django managers. While, I can't list all of the exact code (it's not open source), I'll try to list some simple examples that illustrate the problem so (hopefully), this post will be helpful for others. To get started, assume I have the following Model and Manager: class DefaultThingManager(models.Manager): def things(): # A custom method that retrieves some set of DefaultThing # objects. This doesn't ...

Python's zip, map, and lambda 2013-04-01

Many novice programmers (and even experienced programmers who are new to python) often get confused when they first see zip, map, and lambda. This post will provide a simple scenario that (hopefully) clarifies how these tools can be used. To start, assume that you've got two collections of values and you need to keep the largest (or smallest) from each. These could be metrics from two different systems, stock quotes from two different services, or just about anything. For ...

How in the world do you Mock a name attribute? 2013-03-14

Or... My adventures with Mock. Part 1. I've been working a lot with Mock lately (and by lately, I meand for the last three months). Though it takes a while to wrap your head around it, it's an amazing and powerful testing tool. To get started, let's look at some of the neat things you can do with Mock. Take this class, for example: class Thing(object): shape = 'square' color = 'blue' def calculate(self): # ... do some stuff ...

Why I deleted Mailbox after only an hour. 2013-03-13

I'll be honest; I was really excited about Mailbox. When I first heard about it, I'd become increasingly Sucky at Email. My inbox was a mess. I had five different kinds of flags/stars, multiple kinds of todo flags, and I was one of those read-an-email-and-mark-it-as-unread people. So, when Mailbox showed up, I really did have high hopes that it'd help me learn A New Way of managing email. I wanted to be productive. But then, I ...