1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""The pure-python implementation of the StaticTuple type.
19
Note that it is generally just implemented as using tuples of tuples of
24
class StaticTuple(object):
25
"""A static type, similar to a tuple of strings."""
27
__slots__ = ('_tuple',)
29
def __new__(cls, *args):
30
if not args and _empty_tuple is not None:
32
return object.__new__(cls)
34
def __init__(self, *args):
35
"""Create a new 'StaticTuple'"""
37
if not isinstance(bit, str) and not isinstance(bit, StaticTuple):
38
raise TypeError('key bits must be strings or StaticTuple')
40
if num_keys < 0 or num_keys > 255:
41
raise ValueError('must have 1 => 256 key bits')
45
return repr(self._tuple)
48
return hash(self._tuple)
50
def __eq__(self, other):
51
if isinstance(other, StaticTuple):
52
return self._tuple == other._tuple
53
if isinstance(other, tuple):
54
return other == self._tuple
58
return len(self._tuple)
60
def __cmp__(self, other):
61
return cmp(self._tuple, other)
63
def __getitem__(self, idx):
64
return self._tuple[idx]
70
return _interned_tuples.setdefault(self, self)
74
_empty_tuple = StaticTuple()