/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_py.py

  • Committer: Andrew Bennetts
  • Date: 2009-10-28 00:12:03 UTC
  • mfrom: (4774 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4775.
  • Revision ID: andrew.bennetts@canonical.com-20091028001203-m7lgs1wtnilgo3br
Merge lp:bzr, resolving NEWS conflict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
class StaticTuple(tuple):
25
25
    """A static type, similar to a tuple of strings."""
26
26
 
 
27
    __slots__ = ()
 
28
 
27
29
    def __new__(cls, *args):
28
30
        # Make the empty StaticTuple a singleton
29
31
        if not args and _empty_tuple is not None:
33
35
    def __init__(self, *args):
34
36
        """Create a new 'StaticTuple'"""
35
37
        for bit in args:
36
 
            if type(bit) not in (str, StaticTuple):
37
 
                raise TypeError('key bits must be strings or StaticTuple')
 
38
            if type(bit) not in (str, StaticTuple, unicode, int, long, float,
 
39
                                 None.__class__, bool):
 
40
                raise TypeError('StaticTuple can only point to'
 
41
                    ' StaticTuple, str, unicode, int, long, float, bool, or'
 
42
                    ' None not %s' % (type(bit),))
38
43
        num_keys = len(args)
39
44
        if num_keys < 0 or num_keys > 255:
40
45
            raise ValueError('must have 1 => 256 key bits')
45
50
    def __repr__(self):
46
51
        return '%s%s' % (self.__class__.__name__, tuple.__repr__(self))
47
52
 
 
53
    def __reduce__(self):
 
54
        return (StaticTuple, tuple(self))
 
55
 
 
56
    def __add__(self, other):
 
57
        """Concatenate self with other"""
 
58
        return StaticTuple.from_sequence(tuple.__add__(self,other))
 
59
 
48
60
    def as_tuple(self):
49
61
        return self
50
62
 
51
63
    def intern(self):
52
64
        return _interned_tuples.setdefault(self, self)
53
65
 
 
66
    @staticmethod
 
67
    def from_sequence(seq):
 
68
        """Convert a sequence object into a StaticTuple instance."""
 
69
        if isinstance(seq, StaticTuple):
 
70
            # it already is
 
71
            return seq
 
72
        return StaticTuple(*seq)
 
73
 
 
74
 
54
75
 
55
76
# Have to set it to None first, so that __new__ can determine whether
56
77
# the _empty_tuple singleton has been created yet or not.