Created
May 18, 2010 03:30
-
-
Save jmhobbs/404570 to your computer and use it in GitHub Desktop.
A Simple ToDo App Using Python and MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import time | |
from datetime import datetime | |
from pymongo.connection import Connection | |
import pymongo | |
class ToDoApplication: | |
def __init__ ( self, argv ): | |
self.argv = argv | |
try: | |
self.mongo_connection = Connection() | |
self.mongo_db = self.mongo_connection.todo | |
except Exception, e: | |
raise Exception ( "Oops! Error connecting to MongoDB: %s" % e ) | |
def help ( self, error=None ): | |
if None != error: | |
print error | |
print "usage: %s <method>" % self.argv[0] | |
print "== Methods ==" | |
print "<none> list all incomplete tasks sorted by priority then chronologically" | |
print "help show this help" | |
print "next list all incomplete tasks that are high priority" | |
print "done list all complete tasks chronologically" | |
print "high <argument> add high priority task called <argument>" | |
print "low <argument> add low priority task called <argument>" | |
print "finish <argument> complete task called <argument>" | |
print "dont <argument> delete unfinished task called <argument>" | |
def run ( self ): | |
try: | |
if 1 >= len( self.argv ): | |
self.show( | |
{ "complete": False }, | |
[ ( "level", pymongo.ASCENDING ), ( "added", pymongo.ASCENDING ) ] | |
) | |
else: | |
if "help" == self.argv[1].lower(): | |
self.help() | |
elif "next" == self.argv[1].lower(): | |
self.show( | |
{ "level": "high", "complete": False }, | |
[ ( "level", pymongo.ASCENDING ), ( "added", pymongo.ASCENDING ) ] | |
) | |
elif "done" == self.argv[1].lower(): | |
self.show( | |
{ "complete": { "$ne": False } }, | |
[ ( "completed", pymongo.ASCENDING ) ] | |
) | |
elif "high" == self.argv[1].lower(): | |
self.add( "high" ) | |
elif "low" == self.argv[1].lower(): | |
self.add( "low" ) | |
elif "finish" == self.argv[1].lower(): | |
self.complete( True ) | |
elif "dont" == self.argv[1].lower(): | |
self.complete( False ) | |
else: | |
raise Exception( "Bad Method" ) | |
except Exception, e: | |
self.help( str( e ) ) | |
def show ( self, params = None, sort = None ): | |
cursor = self.mongo_db.todos.find( params ).sort( sort ) | |
for row in cursor: | |
print row['task'] | |
def add ( self, level ): | |
if 2 >= len( self.argv ): | |
raise Exception( "missing argument" ) | |
self.mongo_db.todos.insert( | |
{ | |
"task": sys.argv[2], | |
"level": level, | |
"complete": False, | |
"added": int( time.mktime( datetime.utcnow().timetuple() ) ) | |
} | |
) | |
def complete ( self, finish ): | |
if 2 >= len( self.argv ): | |
raise Exception( "missing argument" ) | |
document = self.mongo_db.todos.find_one( { "complete": False, "task": sys.argv[2] } ) | |
if not document: | |
print "No Matching ToDo Found" | |
else: | |
if finish: | |
document['complete'] = int( time.mktime( datetime.utcnow().timetuple() ) ) | |
self.mongo_db.todos.save( document ) | |
else: | |
self.mongo_db.todos.remove( document ) | |
if __name__ == "__main__": | |
import sys | |
try: | |
app = ToDoApplication( sys.argv ) | |
app.run() | |
except Exception, e: | |
print e |
This was written for Python 2, and you’d need a MongoDB instance running on your local machine. To be honest, it’s 11 years old, I would be shocked if it still worked.
You can give it a shot though, python mongo.todo.py help
and see what happens.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i run this