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!
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
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.
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)
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.
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)
class MyModelTestCase(TestCase): def test_list(self): # Non-logged-in users should not be able to see models self.get('my-model-list') self.response_401() # Superusers can view models superuser = SuperUserFactory() with self.login(superuser): self.get_check_200('my-model-list')
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.
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.
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:
# greeting.py import djclick as click @click.command() @click.option('--name', help="Pass in your name", default='') def command(name): print('Hi there', name)
Now I can run
python manage.py greeting and see "Hi there" in my console. Or, I can run
python manage.py 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!
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.