Archive for category python
Campy: the Python Campfire Bot
We use a Campfire at Needle for our day-to-day communication between our tech team members and other business members. We really like Campfire’s ability to store files, conference call and keep transcripts even when you are not logged in. We get GitHub commit messages, Pivotal Tracker story updates and more scattered in our conversations.
These one-way updates are very nice, but I thought it would be nice to be able to have a bot that sat in the channel and could take commands from anybody and do more complex tasks for us without us ever having to leave Campfire. To solve this problem, I wrote Campy, a pure Python Campfire bot with an extensible plugin system.
The first plugin I wrote as a proof of concept is a Google image search plugin. You tell the bot some search parameters that you would like it to search for, and you get back an image inline in your chat transcript. Don’t worry, safe search is on by default so you don’t get any NSFW images back.
The second plugin I wrote was the all-important Pivotal Tracker plugin. This plugin lets you do the following:
- pt story create “Title” “Description” bug|feature|chore|release — Create a story
- pt getmine started|finished|delivered|accepted|rejected|unstarted|unscheduled — Get a list of all stories that belong to you
- pt start #story_id — Start a particular story.
- pt start|tell next bug|feature|chore [mine] — Start or tell the next story of a given type. Optionally supply ‘mine’ at the end of the message to only work on your own stories.
The plugins system is very extensible, so you can create a plugin to do pretty much anything!
You can get Campy here: https://github.com/bbelchak/campy
Unit Testing Django with a NoSQL Backend
Posted by Ben in django, NoSQL, python, Technology on February 7, 2011
In my previous post about unit testing for django, I laid the groundwork for how to unit test any django application. One nice feature that django includes with its test framework is the test database syncing. Even better is if you are using South to do database migrations – it will run the migrations in your test environment for you.
However, what if you are using a NoSQL database backend like MongoDB, Cassandra, CouchDB or something similar and you aren’t using the Django ORM? How do you handle setting up and tearing down the database environments?
The good news is that Python’s unittest framework makes this easy. You can override the setUp() and tearDown() on each TestCase that you build. Here is a snippet to get you started:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import pymongo from django.test import TestCase # It would be best to define this in a utility class somewhere def get_db(db_name=None): """ GetDB - simple function to wrap getting a database connection from the connection pool. """ return pymongo.Connection( host=settings.MONGO_HOST, port=settings.MONGO_PORT)[getattr( settings, "MONGO_DBNAME_PREFIX", "") + (db_name or settings.MONGO_DBNAME)] class MyTestCase(TestCase): fixtures = ['test_data.json'] def setUp(self): self.db = get_db('test') self.db.create_collection('mytestcollection') def test_doc_published(self): # Set up a document to save doc = dict(text="test", user_id=1) self.db.mytestcollection.save(doc) self.assertEqual(self.db.mytestcollection.find_one( {'user_id':1})['text'], 'test') def tearDown(self): self.db.drop_collection('mytestcollection') |
What this does is setUp() a collection in the mytestcollection collection, runs the my_doc_published test and then tears down the test database environment by dropping the mytestcollection collection.
Things to remember for setUp() and tearDown():
setUp()is called before every test method in your TestCase class.tearDown()is called after every test method in your TestCase class.tearDown()is called even if your test methods fail or error out.
And there you have it! Django makes testing even non-ORM datasources a snap, if you know how to wire it up.
UPDATE: Some would say that database fixtures and setting up/tearing down database environments as part of your unit tests is not “unit testing”. This is not entirely accurate, because in order to do unit tests that rely on backend data, you must instantiate and tear down pristine database environments.
Unit Testing Your Django Application
Posted by Ben in django, python, Technology, Uncategorized on February 5, 2011
Unit testing is a very important part of any software project. It helps you know that the new code you are deploying works, and isn’t going to blow up in your face. It also helps you feel good about changing large chunks of code without destroying everything you’ve done for the last 3 years.
Unit testing with django is as simple as pie. The documentation is very good, and you can learn a lot about more advanced testing methods from the python documentation. In this blog post, I aim to show a quick way to get up and running with testing your django application.
First, if you are just starting out, make sure you put a high emphasis on testing your application, otherwise you are going to end up with a bunch of code that has never been tested and you will find yourself writing code for weeks just to get partial coverage on the code you’ve already written. Starting off on the right foot is a much better approach, and you will find life much more enjoyable.
Let’s get started…
Read the rest of this entry »
Code Completion (IntelliSense) in VIM
Posted by Ben in django, python, Technology, vim on January 31, 2011
VIM has been my editor of choice for at least 15 years. I love how fast I can edit files, perform menial tasks, and wreak general havoc on any code project I am working on at any given moment. One of the things that I have missed about VIM from an IDE perspective has been code completion (a.k.a. “IntelliSense”). I have spent a lot of time on websites and man pages trying to figure out syntax and function names for several types of languages, and just recently discovered a long-included feature of VIM called omni completion, or Omnicomplete.
Since my life is mostly centered around django these days, I will discuss how I’ve benefited from omnicomplete and how I’ve set it up in my own environment.
First, since django is a web development framework, I want to make sure that I can get omnicompletion for HTML, Python, JavaScript and CSS. Omnicompletion works for almost any programming language that VIM has syntax highlighting support for, and these languages are no exception.
Django database migrations with South
Posted by Ben in django, python, Technology on January 27, 2011
I have been using django for web development for almost a year now, and I just recently started using South to do database migrations. To be fair, most of the work that I have been doing with databases has centered around MongoDB and schema-less document stores instead of a traditional RDBMS. Since Django does not come with any database migration tools, my standard approach was to make sure that my models are completely thought out before running the manage.py syncdb command. The lack of a good database migration tool was one of the things that originally had turned me off to django.
Enter South. South lets you manage your database in a way very similar to how Ruby on Rails works.