Five for Friyay: Useful Python and Django Libraries

Every day is a new adventure in a new job. I came into my job at REVSYS with not much production Python experience and my colleagues have been kind enough to share some time-saving and frustration-reducing libraries with me as I've been learning. This Friday, I'm sharing five libraries (technically, four libraries and a repo) that I've learned about in the last three months and fallen pretty much in love with. Enjoy!

1. python-dateutil

I shared python-dateutil with a Slack channel of software engineers whose first programming language is not Python and the response was 🙌 and exclamations of "You mean I didn't need to spend hours fussing with strptime?!?"

This library does a lot of handy things, but the most important thing it does it take a string that contains some sort of date/time data and just poof make it into a DateTime object.

Using strptime to parse a DateTime string from an API, I had to slice the string because there was data I couldn't figure out how to get strptime to account for. Just getting this far took me more than an hour, and it still wasn't perfect:

>> from datetime import datetime
>> date = datetime.strptime('2013-08-28T23:59:00-06:00'[:19], '%Y-%m-%dT%H:%M:%S')
>> date
datetime.datetime(2013, 8, 28, 23, 59)

Using python-dateutil for the same thing:

>> from dateutil.parser import parse
>> date = parse('2013-08-28T23:59:00-06:00')
>> date
datetime.datetime(2013, 8, 28, 23, 59, tzinfo=tzoffset(None, -21600))

I get a much more accurate DateTime object! I wish I'd known about it a month ago, because strptime isn't that fun to use.

2. django-test-plus

I'm pitching a REVSYS product here, but I really like it. django-test-plus makes writing Django REST Framework tests a little bit easier. I turned this:

class MyModelTestCase(TestCase):
   def test_list(self):        
       url = reverse('mymodel-list')

       # Non-logged-in users should not be able to see models
       response = self.client.get(url)
       self.assertEqual(response.status_code, 401)

       # Superusers can view models
       superuser = SuperUserFactory()
       with self.login(superuser):
           response = self.client.get(url)
           self.assertEqual(response.status_code, 200)

into this:

class MyModelTestCase(TestCase):
   def test_list(self):
       # Non-logged-in users should not be able to see models

       # Superusers can view models
       superuser = SuperUserFactory()
       with self.login(superuser):

The library contains built-in methods for checking the major HTTP status codes using the standard HTTP methods (GET, POST, PUT, DELETE, etc.) and can save you a lot of keystrokes. Frank Wiles's blog post about using django-test-plus is pretty helpful, too.

3. django-rest-swagger

django-rest-swagger puts a prettier UI on your Django REST Framework APIs. The project ships with an example based on the Django REST Framework tutorial so you can see it in action right out of the box. It integrates your docstrings into the UI so your API's documentation is right there in the browser.

Screeshot of django-rest-swagger in the browser

4. django-click

Write management commands for fun and profit with django-click! The documentation for this library is solid and it makes writing management commands really easy. I wind up using it a lot to generate and mess with test data in development. Here's a silly example management command that takes in your name and greets you:

import djclick as click

@click.option('--name', help="Pass in your name", default='')
def command(name):
    print('Hi there', name)

Now I can run python greeting and see "Hi there" in my console. Or, I can run python greeting --name=Lacey and see "Hi there Lacey." Let your imagination run wild with possibilities!

Thanks to Jeff Triplett for letting me know this library existed!

5. styleguide-git-commit-message

I'm cheating. The Git Commit Message StyleGuide isn't a library and it isn't Django. It IS a style guide for writing commit messages that use semantic emoji. I've been integrating this style guide into my own Git workflow and not only do my commits feel more whimsical, I can also tell at a glance what I was doing in my commit history.

Thanks to Jeff Triplett for his advice on this post.