Welcome, guest | Sign In | My Account | Store | Cart

Simple recipe for representing a list of messages with their responses like in a forum.

Python, 70 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
messages = dict()  #   {index : [niveau,index_parent,message]
filiation = dict() #   {index_parent:[liste_des_fils]}

class message:
    def __init__(self,idx,idx_parent,message):
	self.idx = idx
	self.idx_parent = idx_parent
	self.message = message
	self.level = 0
	self.ajout_message()

    def ajout_message(self):
	if self.idx_parent:
	    self.level = messages[self.idx_parent][0] + 1
	messages[self.idx] = [self.level,self.idx_parent,self.message]
	self.update_filiation()
		    
    def update_filiation(self):
	if self.idx_parent:
	    parent = self.idx_parent
	    if filiation.has_key(parent):
		filiation[parent].append(self.idx)
	    else:
		filiation[parent]=[self.idx]
	else:
	    filiation[self.idx]=[]

def liste_rep(num):
    sep = ' |-'
    try:
	mess = messages[num][2]
	level = messages[num][0]
	if level :
	    print '  |'*(level),sep,mess,'('+ `num`+')'
	else :
	    print num,mess
    except:
	pass
    if filiation.has_key(num):
	for i in filiation[num]:
	    liste_rep(i)

def liste_all():
    for i in filiation.keys():
	if messages[i][1] == None: #pas de parent
	    liste_rep(i)

	    
					   
if __name__ == '__main__':
    import pprint
    m = message(1,None,'first message')
    m = message(2,1,'response to the first message')
    m = message(3,1,'response to the first message')
    m = message(4,2,'response to the second message')
    m = message(5,None,'fifth message')
    m = message(6,2,'response to the second message')
    m = message(7,1,'response to the first message')
    m = message(8,6,'response to the sixth message')
    m = message(9,5,'response to the fifth message')
    m = message(10,None,'tenth message')
    m = message(11,None,'eleventh message')
    print "Filiation Dictionnary{index_parent:[list_of_childs]}:"
    pprint.pprint(filiation)
    print
    print "Messages Dictionary {message_number : [level,index_parent,content]}:"
    pprint.pprint(messages)
    print
    print "Displaying the tree messages"
    liste_all()

This is my try to implement a list of posts with their responses. I would be very happy if you could improve it for more efficiency. Here the output of this script:

Filiation Dictionnary{index_parent:[list_of_childs]}: {1: [2, 3, 7], 2: [4, 6], 5: [9], 6: [8], 10: [], 11: []}

Messages Dictionary {message_number : [level,index_parent,content]}: {1: [0, None, 'first message'], 2: [1, 1, 'response to the first message'], 3: [1, 1, 'response to the first message'], 4: [2, 2, 'response to the second message'], 5: [0, None, 'fifth message'], 6: [2, 2, 'response to the second message'], 7: [1, 1, 'response to the first message'], 8: [3, 6, 'response to the sixth message'], 9: [1, 5, 'response to the fifth message'], 10: [0, None, 'tenth message'], 11: [0, None, 'eleventh message']}

Displaying the tree messages 1 first message | |- response to the first message (2) | | |- response to the second message (4) | | |- response to the second message (6) | | | |- response to the sixth message (8) | |- response to the first message (3) | |- response to the first message (7) 5 fifth message | |- response to the fifth message (9) 10 tenth message 11 eleventh message

1 comment

Salvatore Di Dio (author) 17 years, 1 month ago  # | flag

bad translation. 'ajout_message' can be translated by 'add_message', excuse me for my poor english

Reagards

Created by Salvatore Di Dio on Fri, 29 Oct 2004 (PSF)
Python recipes (4591)
Salvatore Di Dio's recipes (1)

Required Modules

Other Information and Tasks