From dca88cadbd22f02cb355981841c7bf5175deff0c Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 21 Mar 2011 03:18:12 +0100 Subject: [PATCH] Use namedtuples instead of dict, this should GREATELY lower the memory footprint --- src/room.py | 16 ++++------------ src/text_buffer.py | 14 ++++++-------- src/windows.py | 37 +++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/room.py b/src/room.py index 080c91eb..4196ce5f 100644 --- a/src/room.py +++ b/src/room.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with Poezio. If not, see . -from text_buffer import TextBuffer +from text_buffer import TextBuffer, Message from datetime import datetime from random import randrange from config import config @@ -111,18 +111,10 @@ class Room(TextBuffer): color = self.do_highlight(txt, time, nickname) if time: # History messages are colored to be distinguished color = theme.COLOR_INFORMATION_TEXT - time = time if time is not None else datetime.now() + time = time or datetime.now() nick_color = nick_color or user.color if user else None - message = {'txt': txt, 'colorized':colorized, - 'time':time} - if nickname: - message['nickname'] = nickname - if nick_color: - message['nick_color'] = nick_color - if color: - message['color'] = color - if user: - message['user'] = user + message = Message(txt=txt, colorized=colorized, nick_color=nick_color, + time=time, nickname=nickname, color=color, user=user) # message = Message(txt, time, nickname, nick_color, color, colorized, user=user) while len(self.messages) > self.messages_nb_limit: self.messages.pop(0) diff --git a/src/text_buffer.py b/src/text_buffer.py index 832ce2f3..65da6975 100644 --- a/src/text_buffer.py +++ b/src/text_buffer.py @@ -21,10 +21,14 @@ Define the TextBuffer class import logging log = logging.getLogger(__name__) +import collections + from datetime import datetime import theme from config import config +Message = collections.namedtuple('Message', 'txt colorized nick_color time nickname color user') + class TextBuffer(object): """ This class just keep trace of messages, in a list with various @@ -43,14 +47,8 @@ class TextBuffer(object): def add_message(self, txt, time=None, nickname=None, colorized=False, nick_color=None): color = theme.COLOR_NORMAL_TEXT if nickname is not None else theme.COLOR_INFORMATION_TEXT nick_color = nick_color - msg = {'txt': txt, 'colorized':colorized, - 'time':time or datetime.now()} - if nickname: - msg['nickname'] = nickname - if nick_color: - msg['nick_color'] = nick_color - if color: - msg['color'] = color + msg = Message(txt=txt, colorized=colorized, nick_color=nick_color, + time=time or datetime.now(), nickname=nickname, color=color, user=None) self.messages.append(msg) while len(self.messages) > self.messages_nb_limit: self.messages.pop(0) diff --git a/src/windows.py b/src/windows.py index 2d62a35b..4300b240 100644 --- a/src/windows.py +++ b/src/windows.py @@ -48,6 +48,9 @@ import theme import common import wcwidth import singleton +import collections + +Line = collections.namedtuple('Line', 'text colorized text_offset text_color nickname_color time nickname') g_lock = Lock() @@ -515,14 +518,14 @@ class TextWin(Win): if message is None: # line separator self.built_lines.append(None) return 0 - txt = message.get('txt') + txt = message.txt if not txt: return 0 else: txt = txt.replace('\t', ' ') # length of the time offset = 9+len(theme.CHAR_TIME_LEFT[:1])+len(theme.CHAR_TIME_RIGHT[:1]) - nickname = message.get('nickname') + nickname = message.nickname if nickname and len(nickname) >= 25: nick = nickname[:25]+'…' else: @@ -533,18 +536,20 @@ class TextWin(Win): nb = 0 while txt != '': (txt, cutted_txt) = cut_text(txt, self.width-offset-1) - l = {'colorized': message.get('colorized'), - 'text_offset':offset, - 'text_color':message.get('color'), - 'text': cutted_txt - } - color = message.get('user').color if message.get('user') else message.get('nick_color') - if first and color: - l['nickname_color'] = color if first: - l['time'] = message.get('time').strftime("%H:%M:%S") - l['nickname'] = nick - self.built_lines.append(l) + color = message.user.color if message.user else message.nick_color + else: + color = None + if first: + time = message.time.strftime("%H:%M:%S") + nickname = nick + else: + time = None + nickname = None + self.built_lines.append(Line(text=cutted_txt, colorized=message.colorized, + text_offset=offset, text_color=message.color, + nickname_color=color, time=time, + nickname=nickname)) nb += 1 first = False while len(self.built_lines) > self.lines_nb_limit: @@ -569,9 +574,9 @@ class TextWin(Win): if line is None: self.write_line_separator() else: - self.write_time(line.get('time')) - self.write_nickname(line.get('nickname'), line.get('nickname_color')) - self.write_text(y, line.get('text_offset'), line.get('text'), line.get('text_color'), line.get('colorized')) + self.write_time(line.time) + self.write_nickname(line.nickname, line.nickname_color) + self.write_text(y, line.text_offset, line.text, line.text_color, line.colorized) if y != self.height-1: self.addstr('\n') self._refresh()