~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_py.py

  • Committer: John Arbash Meinel
  • Date: 2009-10-02 20:32:50 UTC
  • mto: (4679.6.1 2.1-export-c-api)
  • mto: This revision was merged to the branch mainline in revision 4735.
  • Revision ID: john@arbash-meinel.com-20091002203250-q6iv6o2mwjqp4g53
Add __iter__ support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""The pure-python implementation of the StaticTuple type.
 
18
 
 
19
Note that it is generally just implemented as using tuples of tuples of
 
20
strings.
 
21
"""
 
22
 
 
23
 
 
24
class StaticTuple(object):
 
25
    """A static type, similar to a tuple of strings."""
 
26
 
 
27
    __slots__ = ('_tuple',)
 
28
 
 
29
    def __new__(cls, *args):
 
30
        if not args and _empty_tuple is not None:
 
31
            return _empty_tuple
 
32
        return object.__new__(cls)
 
33
 
 
34
    def __init__(self, *args):
 
35
        """Create a new 'StaticTuple'"""
 
36
        for bit in args:
 
37
            if not isinstance(bit, str) and not isinstance(bit, StaticTuple):
 
38
                raise TypeError('key bits must be strings or StaticTuple')
 
39
        num_keys = len(args)
 
40
        if num_keys < 0 or num_keys > 255:
 
41
            raise ValueError('must have 1 => 256 key bits')
 
42
        self._tuple = args
 
43
 
 
44
    def __repr__(self):
 
45
        return repr(self._tuple)
 
46
 
 
47
    def __hash__(self):
 
48
        return hash(self._tuple)
 
49
 
 
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
 
55
        return NotImplemented
 
56
 
 
57
    def __len__(self):
 
58
        return len(self._tuple)
 
59
 
 
60
    def __cmp__(self, other):
 
61
        return cmp(self._tuple, other)
 
62
 
 
63
    def __getitem__(self, idx):
 
64
        return self._tuple[idx]
 
65
 
 
66
    def as_tuple(self):
 
67
        return self._tuple
 
68
 
 
69
    def intern(self):
 
70
        return _interned_tuples.setdefault(self, self)
 
71
 
 
72
 
 
73
_empty_tuple = None
 
74
_empty_tuple = StaticTuple()
 
75
_interned_tuples = {}