Skip to content

Instantly share code, notes, and snippets.

@flags
Created October 27, 2011 01:35
Show Gist options
  • Save flags/1318549 to your computer and use it in GitHub Desktop.
Save flags/1318549 to your computer and use it in GitHub Desktop.
Keeps track of my notes.py file
import re, sys
class note:
def __init__(self,fname):
self.notes = []
self.fname = fname
def parse(self):
_f = open(self.fname,'r')
for line in _f.readlines():
_n = {'note':line[4:].replace('\n',''),'done':False}
if line[:3] == '[x]':
_n['done'] = True
self.notes.append(_n)
_f.close()
def rewrite(self):
_f = open(self.fname,'w')
for note in self.notes:
_str = ''
if note['done']: _str+='[x] '
else: _str+='[ ] '
_str+=note['note']+'\n'
_f.write(_str)
_f.close()
def complete(self,index,justkidding=False):
index -= 1
if index < 0 or index>len(self.notes)-1: print 'Invalid note.'
if justkidding:
self.notes[index]['done']=False
print '[ ] '+self.notes[index]['note']
else:
self.notes[index]['done']=True
print '[x] '+self.notes[index]['note']
def output(self):
i = 1
for note in self.notes:
if note['done']: print '[x]',
else: print '[ ]',
print '%s. %s' % (str(i),note['note'])
i+=1
_tn = note('test.txt')
_tn.parse()
if len(sys.argv)==3 and sys.argv[1]=='c':
_tn.complete(int(sys.argv[2]))
_tn.rewrite()
elif len(sys.argv)==3 and sys.argv[1]=='uc':
_tn.complete(int(sys.argv[2]),justkidding=True)
_tn.rewrite()
else:
_tn.output()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment