Needs advise for good structure for discussion class (Long)

Max Møller Rasmussen maxm at
Tue Aug 21 16:07:23 CEST 2001

Although this message is about Zope it is mainly about Python, so I send it
here instead of on the Zope list.

I belive that the general problem is interresting enough to get discussed in, so here goes.

I am trying to build the "ultimate threaded discussion module" for Zope. I
belive that the monolithic frameworks that exists today are _evil_ :-) and
that what is needed is more flexible modules.

My main design criteria is:

    1) It must easy to "skin". By this I mean that it should be simple to
write new dtml that gives it a new functionality and look.

    2) It should be as flexible as possible.

    3) it should be easy to write/change dtml script for a beginner.

In python a simple code snippet for listing the messages would be::

    for msg in sortedMessages:
        print msg.indentLevel*'   ' + msg.content

In dtml it would be::

    <dtml-in sortedMessages>
        <dtml-var "indentLevel*'   '"><dtml-var content><br>

'sortedMessages' is a method that takes two optional arguments.
'threadOrder' and 'messageOrder'. Either can be 'forward' or 'reverse'.

getSortedMessages(self, threadOrder='reverse',

This is to support all the ways a discussion can list the messages according
to user preferences.


    Thread 1
        Re: Thread 1
            Re: Re: Thread 1
    Thread 2
        Re: Thread 2
        Re: Thread 2

(Messages that has no parents are threads, messages that has parents are
just messages.)

So the idea is to get the class to return a sorted list of message objects.
Each message has it's 'indentLevel' property assigned dynamically in
accordance to how the messages are sorted. This makes it very simple to
script it in dtml and Python. No ugly and evil recursion ;-)

Naturally this will cause me problems because Python use referenced objects,
så if two users are trying to get the list with different sorts, all hell is

I haven't figured out just yet how to solve this. Probably the 'indentLevel'
should not be a property of the message class.

The short of the long is that I have written the below class to play around
with different solutions. And I have realised that I really am in need for
some ideas as to how I should structure the program.

Some qustions I have:

    - Will it be easier/faster to sort the messages by iteration or

    - Should the message class have a 'child' parameter too, so it would be
easier to sort it in a recursive manner.

    - Should I just put the parent and child objects into the messages?

    - Should I sort the list in the 4 possible combinations on insert insted
of what I do now?

    - Does anybody has any ideas as to the smartes way to structure this

    - Is this to stupid to ask in the newsgroup?

ANY ideas, comments and solutions will be welcome.

Sometimes the fluidity of Python makes solutions much simpler than other
approaches that are inspired by other tools and languages. They can just be
hard to discover sometimes. What I really hope for is some "Blinding flash
of the obvious". But anything else will be appreciated.

Meanwhile I will continue to work on the problem myself.

regards Max M


class message:
    def __init__(self, id, content, parent): = id
        self.content = content
        self.parent = parent
        self.indentLevel = 0

class messages:
    def __init__(self):
        self.mesgDict = {}

    def addMsg(self, id, content='No content', parent=0):
        self.mesgDict[id] = message(id, content, parent)

    def getSortedMessages(self, threadOrder='reverse',
        # find the threads (parent messages)
        threads = [msg for msg in self.mesgDict.values() if msg.parent==0]
        # so far i only returns the first message in all threads
        return threads

if __name__ == '__main__':
    Thread 1
        Re: Thread 1
            Re: Re: Thread 1
    Thread 2
        Re: Thread 2
        Re: Thread 2
    diskus = messages()
    diskus.addMsg(0, 'Thread 1')
    diskus.addMsg(1, 'Thread 2')
    diskus.addMsg(2, 'Re: Thread 1', 1)
    diskus.addMsg(3, 'Re: Thread 2', 1)
    diskus.addMsg(4, 'Re: Thread 2', 1)
    diskus.addMsg(4, 'Re: Re: Thread 1', 2)

    sortedMessages = diskus.getSortedMessages()
    for msg in sortedMessages:
        print msg.indentLevel*'   ' + msg.content

                         - Nørgård Mikkelsen a/s -
    - Vandværksvej 18 - DK 5000 Odense C
               Tlf (+45) 66 14 14 80 - Fax (+45) 66 14 19 43
                     Max M - Direkte (+45) 63 14 47 15

More information about the Python-list mailing list