newbie help needed factoring methods and params in class defs

Jason Cunliffe jasonic at nomadicsltd.com
Sun Oct 1 20:54:20 EDT 2000


I am writing a module 'caldict.py' with classes to manage 'timeline'
dictionaries. Timestamps (as strings) are lookup keys and the event data
they fetch is any string.
I am using Marc-André Lemburg's  mxDateTime package
http://starship.python.net/~lemburg/mxDateTime.html
plus 'seqdict' and 'mseqdict' modules by Wolfgang Grafen
http://home.germany.net/100-366919/Python/Modules/Modules.html
A great combination - thanks to both authors.

So far so good.
I have a class CalDict() which has various methods: init(), append(),
sort(), findnth(), etc..
I want to use key arguments for various options for all my functions in a
consistent manner.

import caldict
cd = caldict.CalDict()
cd.append(year=1955, month=4, day=5, hour=14, minute=30, 'birthday')
cd.append(year= -2500, 'Bronze age')
cd.append(year = 1789, 'French Revolution')
cd.append(str(now()), 'here and now')
#now put everything in order beautifully thanks to seqdict and mxDateTime
cd.sort()
#Timeline events can be returned by date...
cd.getevent('1955-04-05 14:30:00.00')
#or by index...
cd.findnth(index='last')
cd.findnth(index=1)
cd.findnth(index= -2)  #returns penultimate event ie: 'French Revolution'
#cool!
#now I need the option to have other arguments also for example:
cd.findnth(index='last', display=1)
#or
cd.findnth(index='last', showdate=1)

These will overide defaults and display the result, or show the date as well
as the event string for whatever the last item in the timeline is.

At present I do this very simply as follows inside class Caldict:

   def reverse(self, display = 0):
        s = self.eventdict
        if display:
            self.display()

But as I look down my list of growing class methods, I see the same code
repeated again and again.
There must be a much more elegant pythonic way to factor the "if display"
condition out. But not sure how..
I have a method display () in the top of my Class.but when I  try to detect
there to see if the passed parameter  display =1 it does not work.

I seem to be not undersrtanding something basic about passing parameters
and reusing methods within a class definition.

Another perhaps related problem also concernt best way to factor:

1 class Caldict:
2   def __init__(self, size=12, months=1, days=0, hours=0):
3    #default creates 12 months from the present day and time
4        self.eventdict = seqdict.seqdict()
5        self.indexdict = {}
6        s = self.eventdict
7        for x in range (size):
8            #initialize our delta values
9            dmonths = months * x
10            ddays = days * x
11           dhours = hours * x
12           ddate = now()+RelativeDateTime( months=+dmonths, days=+ddays,
hours=+dhours)
13            s.append(str(ddate), "entry" + str(x))
14           s.sort()
15   #showme
16   def display(self):
17      s = self.eventdict
18      if display:
19         for x in range(len(s)):
20                print x, s.keys()[x]

Why can do I have to redefine in line 17 's = self.eventdict' when I alrady
did so in  line 6. Same for all teh followng methods?

This must be Python101..
I someone can help me get to grips with Python classes better..

Thanks
- Jason

________________________________________________________________
Jason CUNLIFFE = NOMADICS.(Interactive Art and Technology).Design Director






More information about the Python-list mailing list