Learning Basics

Dan Bishop danb_83 at yahoo.com
Sun Jul 8 19:57:23 CEST 2007


On Jul 8, 12:10 pm, Brad <deviantbunnyl... at gmail.com> wrote:
> So I've been studying python for a few months (it is my first foray
> into computer programming) and decided to write my own little simple
> journaling program. It's all pretty basic stuff but I decided that I'd
> learn more from it if more experienced programmers could offer some
> thoughts on how I could do things better.
>
> #simple little journal
>
> from time import asctime
> myjournal=file('journal.txt','a+')
>
> formatter="*"*80+"\n"
> todaysdate=asctime()
> myjournal.write(formatter)
> myjournal.write(todaysdate + "\n\n")
>
> loop="set"
>
> while loop!=':a':
>     loop=raw_input(':')
>     if loop!=':a':
>         myjournal.write(loop+"\n")
>     if loopz==':a':
>         myjournal.close()
>
> #end of stuff
>
> So I'd appreciate some good feedback or ideas.

#!/usr/bin/env python

"""simple little journal"""

import sys
import time

EXIT_COMMAND = ':a'
HEADER = "*" * 80

def add_journal_entry(filename='journal.txt'):
    """
    Prompt the user to enter text, and write it into the journal file.
    """
    print "Enter your journal entry."
    print "When done, type %r." % EXIT_COMMAND
    journal_file = file(filename, 'a+')
    journal_file.write(HEADER + "\n")
    journal_file.write(time.asctime() + "\n\n")
    while True:
        line = raw_input(':')
        if line == EXIT_COMMAND:
            break
        journal_file.write(line + "\n")
    journal_file.close()

def _main(argv=None):
    """Executed when file is run as a script."""
    if argv is None:
        argv = sys.argv
    if len(argv) == 1: # No filename specified; use the default
        add_journal_entry()
    else:
        add_journal_entry(argv[1])

if __name__ == '__main__':
    _main()




More information about the Python-list mailing list