Programming exercises/challenges

Michele Simionato michele.simionato at gmail.com
Sat Nov 22 06:16:31 EST 2008


On Nov 20, 1:28 pm, s... at pobox.com wrote:
>     >> a diary manager compatible with my Emacs diary file (sometimes I
>     >> don't want to open Emacs for a quick note)
>
>     Arnaud> You mean that you sometimes don't have emacs open?
>
> I am constantly amazed at work that people open a separate emacs for each
> file they want to edit.  Most of them seem not to even know that find-file
> exists.
>
> Skip

There is a colleague of mine who keeps open a single
Emacs for weeks, with up to fifty buffers open at
the same time; he thinks using multiple
instances of Emacs is an aberration.
I myself usually keep open 3 or 4 instances of Emacs at
the same time. One instance could keep the
bunch of Python files I am editing at the moment, another
instance a bunch of SQL files, another instance
my journal, another the documentation files, etc. I feel
uncomfortable with more than three or four buffer per
Emacs, so I tend to close and reopen them often, or
to use a different Emacs instance.
I mantain my personal journal as a Python script opening
a ReST file with Emacs according to today's date.
Since the code is short, I can as well post it here:

echo $diario.py
# -*- coding: utf-8 -*-
"""
usage: %prog [options]
-p, --publish: publish the journal as a web page
"""

import os, sys, time, datetime, calendar, re, locale
try: # set italian locale
    locale.setlocale(locale.LC_ALL, 'it_IT')
except:
    locale.setlocale(locale.LC_ALL, 'it_IT.UTF8')
from ms.optionparser import OptionParser

if sys.platform == 'darwin':
    EMACS = '/Applications/Aquamacs\ Emacs.app/Contents/MacOS/Aquamacs
\ Emacs'
else:
    EMACS = 'emacs'
ROOT = os.path.join(os.environ['HOME'], 'md/diario')
if not os.path.exists(ROOT): os.mkdir(ROOT)
OUTPUT = '/tmp/diario'
DATE = re.compile('\d\d\d\d-\d\d-\d\d.txt$')
today =  datetime.date.today()
current_year = today.isoformat()[:4]
templ = """\
%s
---------------------------
"""

def open_today_page():
    today_page = os.path.join(ROOT, str(today)) + '.txt'
    if not os.path.exists(today_page):
        print >> file(today_page, 'w'), templ % today.strftime('%A %d-
%b-%Y')
    os.system('%s +4 %s &' % (EMACS, today_page))

def filter_pages_per_month_year(pages, theyear):
    yield '.. contents::'
    oldmonth = None
    for pagename in sorted(pages):
        year = pagename[:4]
        if year != theyear:
            continue
        month = pagename[5:7]
        if month != oldmonth:
            yield '%s %s\n============================' % (
                  calendar.month_name[int(month)], year)
        yield file(os.path.join(ROOT, pagename)).read()
        oldmonth = month

def publish_journal():
    import docutils.core, webbrowser
    print >> file(OUTPUT + '.txt', 'w'), '\n\n'.join(
        filter_pages_per_month_year(
        [f for f in os.listdir(ROOT) if DATE.match(f)], current_year))
    docutils.core.publish_cmdline(
        writer_name='html',
        argv=[OUTPUT + '.txt', OUTPUT + '.html'])
    webbrowser.open('file://%s.html' % OUTPUT)

if __name__ == '__main__':
    option, args = OptionParser(__doc__).parse_args()
    if option.publish:
        publish_journal()
    else:
        open_today_page()


(the OptionParser comes from this recipe: http://code.activestate.com/recipes/278844/)



More information about the Python-list mailing list