mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 13:19:24 +01:00
Added caching for __str__, __repr__, and __len__.
This commit is contained in:
parent
6e19d921c8
commit
a4b6ee2028
@ -44,10 +44,15 @@ class IrcMsg(object):
|
|||||||
"""Class to represent an IRC message.
|
"""Class to represent an IRC message.
|
||||||
"""
|
"""
|
||||||
__slots__ = ('_args', '_command', '_host', '_nick',
|
__slots__ = ('_args', '_command', '_host', '_nick',
|
||||||
'_prefix', '_user', '_hash')
|
'_prefix', '_user',
|
||||||
|
'_hash', '_str', '_repr', '_len')
|
||||||
def __init__(self, s='', command='', args=None, prefix='', msg=None):
|
def __init__(self, s='', command='', args=None, prefix='', msg=None):
|
||||||
if not s and not command and not msg:
|
if not s and not command and not msg:
|
||||||
raise ValueError, 'IRC messages require a command.'
|
raise ValueError, 'IRC messages require a command.'
|
||||||
|
self._str = None
|
||||||
|
self._repr = None
|
||||||
|
self._hash = None
|
||||||
|
self._len = None
|
||||||
if msg:
|
if msg:
|
||||||
prefix = msg.prefix
|
prefix = msg.prefix
|
||||||
command = msg.command
|
command = msg.command
|
||||||
@ -92,6 +97,8 @@ class IrcMsg(object):
|
|||||||
args = property(lambda self: self._args)
|
args = property(lambda self: self._args)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
if self._str is not None:
|
||||||
|
return self._str
|
||||||
ret = ''
|
ret = ''
|
||||||
if self.prefix:
|
if self.prefix:
|
||||||
ret = ':%s %s' % (self.prefix, self.command)
|
ret = ':%s %s' % (self.prefix, self.command)
|
||||||
@ -105,11 +112,14 @@ class IrcMsg(object):
|
|||||||
ret = '%s :%s\r\n' % (ret, self.args[0])
|
ret = '%s :%s\r\n' % (ret, self.args[0])
|
||||||
else:
|
else:
|
||||||
ret = ret + '\r\n'
|
ret = ret + '\r\n'
|
||||||
|
self._str = ret
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
# This might not take into account the length of the prefix, but leaves
|
# This might not take into account the length of the prefix, but leaves
|
||||||
# some room for variation.
|
# some room for variation.
|
||||||
|
if self._len is not None:
|
||||||
|
return self._len
|
||||||
ret = 0
|
ret = 0
|
||||||
if self.prefix:
|
if self.prefix:
|
||||||
ret += len(self.prefix)
|
ret += len(self.prefix)
|
||||||
@ -120,8 +130,10 @@ class IrcMsg(object):
|
|||||||
for arg in self.args:
|
for arg in self.args:
|
||||||
ret += len(arg) + 1 # Remember the space prior to the arg.
|
ret += len(arg) + 1 # Remember the space prior to the arg.
|
||||||
ret += 2 # For the colon before the prefix and before the last arg.
|
ret += 2 # For the colon before the prefix and before the last arg.
|
||||||
|
self._len = ret
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return hash(self) == hash(other) and \
|
return hash(self) == hash(other) and \
|
||||||
self.command == other.command and \
|
self.command == other.command and \
|
||||||
@ -132,17 +144,19 @@ class IrcMsg(object):
|
|||||||
return not (self == other)
|
return not (self == other)
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
try:
|
if self._hash is not None:
|
||||||
return self._hash
|
return self._hash
|
||||||
except AttributeError:
|
|
||||||
self._hash = hash(self.command) & \
|
self._hash = hash(self.command) & \
|
||||||
hash(self.prefix) & \
|
hash(self.prefix) & \
|
||||||
hash(self.args)
|
hash(self.args)
|
||||||
return self._hash
|
return self._hash
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(prefix=%r, command=%r, args=%r)' % \
|
if self._repr is not None:
|
||||||
(self.__class__.__name__, self.prefix, self.command, self.args)
|
return self._repr
|
||||||
|
self._repr = 'IrcMsg(prefix=%r, command=%r, args=%r)' % \
|
||||||
|
(self.prefix, self.command, self.args)
|
||||||
|
return self._repr
|
||||||
|
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
Loading…
Reference in New Issue
Block a user