/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-15 02:19:43 UTC
  • mfrom: (4634.85.12 cleanup-hof)
  • mto: This revision was merged to the branch mainline in revision 4775.
  • Revision ID: andrew.bennetts@canonical.com-20091015021943-l8tuonz6q30tc4t6
Merge do_with_cleanups from cleanup-hof, and drop (or at least make private) everything else from that branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
24
24
class StaticTuple(tuple):
25
25
    """A static type, similar to a tuple of strings."""
26
26
 
27
 
    __slots__ = ()
28
 
 
29
27
    def __new__(cls, *args):
30
28
        # Make the empty StaticTuple a singleton
31
29
        if not args and _empty_tuple is not None:
34
32
 
35
33
    def __init__(self, *args):
36
34
        """Create a new 'StaticTuple'"""
 
35
        for bit in args:
 
36
            if type(bit) not in (str, StaticTuple):
 
37
                raise TypeError('key bits must be strings or StaticTuple')
37
38
        num_keys = len(args)
38
39
        if num_keys < 0 or num_keys > 255:
39
 
            raise TypeError('StaticTuple(...) takes from 0 to 255 items')
40
 
        for bit in args:
41
 
            if type(bit) not in (str, StaticTuple, unicode, int, long, float,
42
 
                                 None.__class__, bool):
43
 
                raise TypeError('StaticTuple can only point to'
44
 
                    ' StaticTuple, str, unicode, int, long, float, bool, or'
45
 
                    ' None not %s' % (type(bit),))
 
40
            raise ValueError('must have 1 => 256 key bits')
46
41
        # We don't need to pass args to tuple.__init__, because that was
47
42
        # already handled in __new__.
48
43
        tuple.__init__(self)
50
45
    def __repr__(self):
51
46
        return '%s%s' % (self.__class__.__name__, tuple.__repr__(self))
52
47
 
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
 
 
60
48
    def as_tuple(self):
61
 
        return tuple(self)
 
49
        return self
62
50
 
63
51
    def intern(self):
64
52
        return _interned_tuples.setdefault(self, self)
65
53
 
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
 
 
75
54
 
76
55
# Have to set it to None first, so that __new__ can determine whether
77
56
# the _empty_tuple singleton has been created yet or not.