Brad's Blog

What's in my requirements.txt 2017-05-14

It's Sunday, and tomorrow is our scheduled monthly python meetup in Memphis, and it's one of those month's where I've been busy and I haven't done a good job of finding a speaker. So, that mean's I've got to pull something together at the last minute. While racking my brain for a quick-and-easy topic, I thought, "I wonder what python packages I'm using most?" So, I ran this nifty monstrosity of a ...

Sending SMS messages with Amazon SNS and Python 2017-02-01

There are many services out there that will let you programmatically send SMS messages. One of the more popular is Twilio, and they have a great API and a python client that's easy to use. There's an interesting quora thread with several other suggestions as well. Another option is to use Amazon's Simple Notification Service (SNS), which also supports sending SMS messages. I recently incorporated this into a project, and thought I'd share. Step 1: API ...

Let's convert a Word Doc to HTML 2016-04-29

tl;dr I wrote a python script to convert Word documents to mostly-clean html. Get it at https://github.com/bradmontgomery/word2html. Ah, Microsoft Word... That glorious business-class software used all-around the world. It's perfect for those long, legal documents consisting of nothing but headers, paragraphs, and bulleted lists. All of which we an easily convert into simple HTML, right. Right? File > Save As > Web Page (.htm). Easy as... No wait, was that supposed to be File ...

A custom __date lookup for Django 2015-10-18

⚠ Django 1.9 now includes a built-in __date lookup. If possible, you should use that instead of the code below, which doesn't support timezones. In my post last week on date lookups, I ended with a promise to take a look at building a custom django lookup (namely, a __date lookup). Django includes a basic Lookup class, and to build your own lookup expressions, all you really need to do is: Subclass django.db.models.Lookup define a ...

Date lookups in Django 2015-10-10

A while ago I tweeted out something that I've wanted to see in Django for a very long time, yet have never really taken the time to investigate or implement it: I wish #django had this: M.objects.filter(datetimefield__date=http://t.co/MVFXsN4Ivk(2015, 6, 29)) Has that ever been attempted?— Brad Montgomery (@bkmontgomery) June 29, 2015 Django's ORM has a very rich set of field lookups, but at present, it doesn't support an exact ...

Disabling the Forms in Django Rest Framework's Browsable API 2015-09-03

If you're building a RESTful api using django, then you're probably aware of Django Rest Framework. It's a great project that will do a lot of the heavy lifting for you. It's also got this really really nice featur: the browsable api. The browsable api gives you out-of-the box access to view your api, and even to interact with it using some auto-generated forms. This is great during development, because you can quickly see exaclty how ...

Webucator: zip, map, and lambda 2015-08-30

Recently, the folks from Webucator, who have a series of python tutorials got in touch with me about one of my blog posts: Python's zip, map, and lambda. They wanted to turn that article into a tutorial video. I thought that sounded like a pretty cool idea, so if you're into video tutorials, give this a view. And check out some of their other tutorials as well.

A django iconbool filter 2015-08-26

Django's template laguage includes a lot of really useful built-in tags and filters, but sometimes you just need to build your own. There are many reasons why you might want to do this, but I'm lazy, and I like to build filters and tags that let me take shortcuts in the template. Here's one example of a simple filter that let's me be lazy: an iconbool filter. Motivation I really like Font-Awesome, and any time I ...

Nice ArrayField widgets with choices and chosen.js 2015-04-25

One of the really cool new features in Django 1.8 is the support for Postgres-specific fields. I'm very excited to be able to use things like PostgreSQL arrays or hstore without 3rd-party add-ons. Unfortnately, the default form inputs for ArrayFields are less than stellar. So, in this post I want to explore a few things: a Model who's ArrayField only accepts items from a set of predefined choices a ModelForm that makes use of chosen.js (which ...

Old-school Skitch and CloudFiles with ftp-cloudfs 2014-01-28

I'm an unabashed fan of Skitch. The old one (pre-Evernote acquisition). A lot of folks have written about how to acquire this and how they use it. Until recently, I just used the built-in SFTP support to upload files to a Rackspace CloudServer. Lately however, I've been trying to limit the amount of stuff I have on servers that I have to maintain. One way I've been doing that is to push everything that looks like a ...

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 ...

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 ...

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 ...

Calculate a Week Range for a Date 2013-03-07

Math with dates and date ranges is often fun & enlightening! As a testament to the fun of calculating dates (particularly ranges of dates), I present the following: Given a date, how would you find the range of dates that describe the week during which your original date lies? In other words, assume today is March 7, 2013 (and it is... for now anyway). Can you answer these two questions: What was last Sunday's date? What will be the ...

Customizing Django's password_change view 2012-10-30

If you have a site where users have the traditional username/password combination, you've got to provide some way to let users change their password. Luckily, this is fairly easy to do with Django. The auth app comes with a password_change view that does what you'd probably expect. It's also fairly easy to set up. You add a line similar to the following to your root URLConf: url(r'^accounts/', include('django.contrib.auth.urls')), You also ...

Thoughts on PyArkansas 2012-10-28

I make a decent effort to keep up with what's happening in the Python comunity, but—gosh darnit—it's hard! I think that's why conferences exist. They give you a day or two to focus solely on meeting people and catching up with what's going on in the community. I particularly like the smaller, regional events like the one I attended this weekend: PyArkansas. Here's a few reasons why: Cost: Smaller, regional conferences tend to ...

Django Models & Mixins for cleaner code 2012-09-26

I've been using Mixins lately to DRY-ly make certain behavior available to several different Django models. If you're not familiar with mixins, there's a great discussion over on StackOverflow. Here's a simple example to illustrate what I've been doing. In building Work for Pie, we've got a UserProfile model that looks something like this: class UserProfile(models.Model): user = models.OneToOneField(User) tagline = models.CharField(max_length=140) biography = models.TextField() avatar_url = models.URLField(max_length ...

PostgreSQL 9.1.2 via homebrew on OS X 10.7.2 2011-12-22

I just picked up a snazzy new Macbook Air, and I'm working on setting up my development environment(s). For the most part this has been fairly easy. I pull in my repos from github and bitbucket, and I use virtualenv and pip to organize all my python packages (mostly installing from requirements files). Most of the other command-line tools get intalled with homebrew, and this time around I decided to install PostgreSQL with homebrew. I didn't keep ...

Chosen.js in the Django admin 2011-12-13

Update Nov 23, 2013: I've written a little app (django-chosenadmin) that'll automatically add this to every app. Quite some time ago, I ran across the chosen.js plugin for jQuery and Prototype (I'm using the jQuery flavor). My first thought upon seeing this was, "This would rock in Django's admin app." Yet for some reason, I didn't make that happen.Until recently. I maintain a project where about 10 people use the admin app extensively ...

Python: stray commas cause tuples? 2010-10-15

As I try to debug a strange problem in a Django view, I notice a stray comma after a dictionary definition. So I jump over to a python shell, and guess what? Ending a literal dict with a comma creates a tuple. >>> d = {1:'foo'},>>> type(d)<type 'tuple'>>>> d({1: 'foo'},)>>>

A case for values_list 2010-07-19

Here's the Scenario: I have a model (lets call it Contact) with two Foreign Keys, one of which is related to User in Django's contrib.auth app. I need to build a form that lets me select an existing object, and a new user. class ContactType(Model): name = CharField(max_length=128)class Contact(Model): user = ForeignKey(User) contact_type = ForeignKey(ContactType) # possibly more fields...I need to select from existing models, so my first thought might be to build ...

PyGraphviz on OS X (SL) with virtualenv 2010-07-14

There's this cool project called django-extensions that (among other things) adds a lot of commands to django's manage.py offerings. One of which is ./manage.py graph_models [appname] which will generate a nice graph displaying the relationships among all of your Models. This comand needs pyGraphViz, though, and I was a little disappointed when i discovered I couldn't install pyGraphViz with pip install pygraphviz. (ok, a lot disappointed). I eventually got this working, and here's how ...

Convert Tables to Unordered Lists 2010-05-28

If you've ever had the pleasure of working with old HTML content, you've surely seen some <table>'s where they don't belong. Lately, that's the sort of thing I've been dealing with on a regular basis, and for some reason, I often see a list of information in a table.Wouldn't it be nice if there were an easy way to turn these tables into unordered lists? Thanks to BeautifulSoup, this is really not ...

Pretty options for Django's auth.User 2010-04-22

Several of my Django Apps have Foreign Key relationships to django.contrib.auth.model.User. In Django's admin app, these show up a select elements displaying the username attribute. For some people, that may be OK, but for most of the people with which I work, it's not. We want to see prettier options, i.e. each User's full name as the options in that select element.So, here's how it works. We override the ModelChoiceField ...

Dealing with Unicode and ASCII using Python 2010-03-25

Dealing with Character Encodings is (sometimes) hard. It's especially confusing for those who've never done it before. Converting text from unicode to ascii can be tricky. A lot of times, I'll import some data from a text file, and I just want to convert everything to ASCII and ignore anything that's not ascii (like MS Word's smart quotes). Luckily, this is fairly easy:mystring = mystring.decode('ascii', 'ignore')There's tons of great Python resources ...

On select_related() 2010-03-23

If you use Django, and your models have relationships that span across multiple tables, you need to read this: http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4select_related() is awesome.That is all.

Building PIL on OS X: Snow Leopard 2010-02-25

There are several places online that discuss problems installing PIL on Mac OS X Snow LeopardThis is how I got it to work.Install lib jpeg using homebrew (which is super-aweseome!) brew intall jpeg.This installs the library into /usr/local/Cellar/jpeg/7Install libfreetype the old-fashioned way (./configure, make, sudo make install). I used freetype-2.1.10.pre-20050511.Download, unpack PIL (I used Imaging-1.1.6). I had to make the following changes to setup.pyFREETYPE_ROOT = "/usr/local"JPEG_ROOT ...

Mercurial installation woes on Mac OS X 2009-09-30

I started using mercurial around version 1.2, and I'm pretty sure I used the Mac OS X installer (from http://mercurial.berkwood.com/) to install 1.2.1. This placed hg in /Library/Frameworks/Python.framework/Versions/Current/bin/.Now, I've decided to upgrade to 1.3.1, and I again grab the Mac OS X installer (again from http://mercurial.berkwood.com/), which installs hg in /usr/local/bin/.Ok, but my path is set up ...

My PYTHONPATH bit me. 2009-06-30

I'd just finished the first version of a new django app (myapp), and so I pushed it out to my development server. All the new code was in place, so I ran python manage.py syncdb. The result?Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/usr/local/lib/python2.6/site-packages/django/core/management/__init__.py", line 340, in execute_manager File "/usr/local/lib/python2.6/site-packages/django/core/management/__init__ ...

"Adding" Q objects in Django 2009-06-26

I've got a Django app with the following Model:class Story(models.Model): title = models.CharField(max_length=255) content = models.TextField()The Problem: I wanted to build a simple search feature that OR'ed all the search terms. Essentially, I wanted SQL resembling the following:SELECT * from myapp_stories where title LIKE '%term1%' OR content LIKE '%term1%' OR title LIKE '%term2%' OR content LIKE '%term2%'; The Solution: You can add django's ...

Data Truncated Errors 2009-05-06

I recently ran into some of the Data truncated for column ... errors in my django apps. After a little digging, I've discovered that my particular problem lie in the structure of the underlying MySQL tables. Particularly with varchar columns. I have a model that contains a FileField:class MyModel(models.Model): file = models.FileField(upload_to="files/%Y/%m/%d")Note that the MySQL table generated by this model will look something like the following:+-------------+--------------+------+-----+---------------------+----------------+| Field | Type | Null | Key | Default ...

Push Your SSH Public keys using Fabric 2009-04-30

This came across my twitter radar today from @bitprophet (aka: Jeff Forcier), who just happens to be the new maintainer for Fabric:def push_key(): keyfile = '/tmp/%s.pub' % env.user run('mkdir -p ~/.ssh && chmod 700 ~/.ssh') put('~/.ssh/id_rsa.pub', keyfile) run('cat %s >> ~/.ssh/authorized_keys' % keyfile) run('rm %s' % keyfile)Everything you need to push your public key to an external server using Fabric.

Restricting Access by Group in Django 2009-04-24

Django's authentication system provides built-in support for Groups. When developing an app, you may want to prevent users in a particular group from accessing part of your app. For example, if you were building a tool to be used by Faculty and Students, it's quite possible that there would be parts of the app you wouldn't want Students to access (like the part that allows a User to change grades!). Luckily, there's a decorator called user_passes_test ...

mod_python checks your blood pressure 2009-03-24

I'm deploying a django project using mod_python... now, usually I will just use my package management tools to install a binary version, but this time I need to build it from source.As I get ready to go through the whole configure/make/make install process, I peruse the output of configure just to make sure everything is ok...checking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether ...

Scheduled Tasks (or cron jobs) with Django 2009-02-20

This is my take on setting up cron jobs for the apps in a Django project. It is based on my own convention, and it solves my initial problems where I want to perform some action on all of my Django apps at a periodic interval (currently this is a once-a-day task).In order for this to work, I create a cron.py module for all of my INSTALLED_APPS. This module must contain a run method. Other than that, it ...

A Custom form for Django's Automatic Admin. 2009-01-14

A huge selling-point for Django (at least for developers) is its Automatic Admin. However, the ease at which the Admin can be set up, might make one second-guess an attempt to customize what is provided by default. Of course, the default admin site may not be without its drawbacks...Many of the django Apps that I have built, tap into Django's User Authentication System. Simply put, when I build a model, it has a Foreign Key to django's ...

Add a Context Processor for your Django app using Sites 2009-01-05

I've recently refactored a significant number of my Django Apps so that they include the "sites" framework. Essentially, this allows me to use the same code (and database) for multiple sites. For Example, if I was was building a CMS (and I am!), I might have a model that defines a "page":from django.db import modelsfrom django.contrib.auth.models import Userfrom django.contrib.sites.models import Siteclass Page(models.Model): title = models.CharField('title', max_length=255) content ...

How to convert HTML to PDF using Python. 2008-12-17

I'm building web-based, data-driven apps using Django. Eventually (or unfortunately), I will need to generate some reports that are printer-friendly. Logically, PDF is the format for such files... so how am I going to convert my xHTML and CSS to a nice-looking PDF document?The Django Book has a whole chapter dedicated to Generating Non-HTML Content. They seem to to be fond of ReportLab ToolKit. The caveat here, though, is that you need to know a bit about the ...

Extending Django's MultiWidget: SplitSelectDateTimeWidget 2008-11-19

This entry is an update to SelectTimeWidget: A custom Django Widget. The Problem: I want to use a Single widget object for a DateTimeField, but I want it to consist of select elements with appropriate options for month, day, year, hour, minute, and second. Additionally, I want to be able to specify a 12-hour format, so I would then need options for "a.m." and "p.m."Fortunately, Django's SelectDateWidget (from django.forms.extras.widgets) takes care of the ...

SelectTimeWidget: A custom Django Widget 2008-11-18

I've been meaning to write this blog post for quite some time...Django Models provide a way to create a definitive source of data for web applications. Written as a python class, a Django Model consists of Fields that (among other things) define a type for your data. Django's Forms provide a mechanism for generating HTML form elements and validating user input. A sublcass of Django's Form class is the ModelForm which essentially creates a Form based ...

Soup's On! And it IS Beautiful! 2008-11-17

Here's the problem: There's a BAJILLIION static html pages sitting out on a server, and I need to migrate all that content to a new Database-driven CMS. Additionally, I need to get rid of a lot of non-essential hard-coded presentational markup (like align="center" or font="whatever") and any inline styles that may exist... (you know, because external CSS is the way to go).I could spend hours and hours just copy-/pasting stuff... but meh. Enter BeautifulSoup ...

A topic for BarCampMemphis 2008-10-22

I've recently watched the What is BarCamp video, and I've been thinking about a Django-related topic. I'm sure there are plenty of web developers out there looking for a better way...Now, I've only been using Django for about 5 months, but I'm pretty sure I could give any interested kindred souls a good jump-start. And so that's what I'd like to do... show by example.The Example? Why not a wiki... It ...

The Structure of a Django App 2008-07-15

Previously, I'd lamented the difficultly present in choosing an web development framework. I'd worked through several symfony tutorials, and though I could see the benefits down the road, it just didn't feel right to me (yes... "feel" is a technical drawback).So, I checked out a copy of Django, and I haven't looked back. If you're the least bit proficient with python, and you need to build a database-driven web site, USE DJANGO! They have ...

Games for Kids: dodger 2008-07-11

I've always thought it would be fun to build video games, and when my daughters were born, I knew I would eventually want to build some simple games that they could enjoy while they're young. My original attempt at this lead me to pygame, and the result was a simple alphabet/number game that plopped letters and numbers on the screen (very similar to AlphaBaby, but not nearly as good!). Unfortunately, I never continued to develop that one ...

Mac OS X, Python, and Fink: Playing Nicely Together 2008-06-25

Since upgrading to Leopard, I've been using Mac's default install of Python (which is 2.5.1). For the most part it's worked well for me, namely because I've installed additionaly packages either using Mac installers or through easy_install. I recently needed to install python-ldap which didn't work using easy_install. So, I turned to Fink.Unfortunately, Fink wanted to install it's own version of python (2.5.2) as well. (I say unfortunately only ...

Lions, Tigers, and Web Development Frameworks, oh my! 2008-06-17

Apparently I've stumbled upon a problem that has recently faced many web developers. That is, I would like to adopt an open-source web development framework for mid-sized project. Well... searching that phrase only yields 200,000+ results. So how does one choose?I guess Ruby on Rails sparked the whole "Web Development Framework" movement (among other things). I've typically used PHP for my web-based projects in the past, but over the last 2 years, I've also become ...

pyCropper 2008-05-20

Occasionally, I need to crop some images. I suspect this happens to a lot of people, and they simply use their favorite photo editor to perform the task. For a few images, this works just fine. Plenty of people have Photoshop, the Gimp, or even iPhoto. This past weekend, however, I found myself needing to crop about 1000 images, and I need to do it quickly. This was more than my machine could load into the Gimp at once. Even ...

Shrinking Images with Python 2008-02-15

My wife had a very large image, and she needed some smaller versions of it. Well, I thought I'd just fire up The GIMP and create a few shrunken versions, but by the time The GIMP loaded on my G4 Mac mini, I'd almost finished writing the python coded needed to do the task for me!Writing code is good!import Imagefrom sys import argvdef shrink(filename): im = Image.open(filename) w,h = im.size # Create images that ...

Tracking a Laser pointer with Python and OpenCV 2008-01-24

A recent thread on the OpenCV mailing list (entitled: "Tracking laser dots") discussed techniques that could be used to track the dot from a laser pointer. This sounded like something fun, so I finally got around to trying it out. Essentially this could be done acheived by the following algorithm: Grab the video frame. Convert the video frame to the HSV color space. Split the frame into individual components (separate images for H, S, and V). Apply a threshold to ...

Computing Correlation Coefficients in Python 2007-12-05

A useful technique for matching objects in images is to compute the images' Correlation Coefficients. Essentially, you take any image and compute the correlation between it and another, smaller image containing ONLY the object that you want to identify. The resulting correlation image should contain bright spots where there is a high correlation (or match) between the two images. Here's a simple python script to compute the correlation between two images: https://github.com/bradmontgomery/correlation It requires PIL ...

Announcing pgSlideShow 2007-10-31

I've just released a simple little application called pgSlideShow. It's a free image slideshow application written in Python using pygame. It recursively searches a given directory for image files, and then displays them on a computer screen in Fullscreen mode. You can get it here: http://bradmontgomery.net/pgSlideShow/https://github.com/bradmontgomery/pgSlideShow

PyOpenGL on OS X 2007-10-16

Update: July 7, 2010: For the past year or so I've been using virtualenv, virtualenvwrapper, and pip to manage and install python packages, and I highly encourage everyone to use these tools! It may take a bit to learn how to use these tools, but once you do, the rewards are great! (especially when using 3rd party python libraries on Mac OS X).To install PyOpenGL using the aforementioned tools, you'd simply do the following.Create a virtual ...

Pygame on OS X with python 2.5 2007-09-23

I've used Pygame on Mac OS X in the past, but my installation recently stopped working for some reason, so I decided to grab the binaries and re-install. After checking out the Pythonmac list, I was a little disappointed to see that there were only Pygame binaries for Python 2.4. Below is a list of software that I installed (in the necessary order) to get pygame working with Python 2.5 on OS X. All of these (except ...

The Never-ending "To-Learn" List 2007-09-10

I'm officially a student! August marked the beginning of a new semester for me, and this time I'm a full-time student. After being on the faculty side of things for a while, I'm actually having a great time being a student again. One of the major benefits of being a student is that I'm actually getting to dive into that never-ending list of technology tools that I've been wanting to learn how to use!Just ...