~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (C) 2004 Aaron Bentley
# <aaron.bentley@utoronto.ca>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

__docformat__ = "restructuredtext"
__doc__ = "A miscellany of stuff"


class rewind_iterator:
    """A rewindable iterator wrapper"""
    def __init__(self, base_iterator):
        """Initializer.
        
        :param base_iterator: The iterator to make rewindable
        :type base_iterator: Iterable of anything
        """
        self.base_iterator = base_iterator.__iter__()
        self.read_items = []
        self.list_iterator = self.read_items.__iter__()
        self.use_list = True

    def next(self):
        if self.use_list:
            try:
                return self.list_iterator.next()
            except StopIteration:
                self.use_list = False

        item = self.base_iterator.next()
        self.read_items.append(item)
        return item

    def rewind(self):
        """Rewind and start iterating again from the sequence beginning."""
        self.use_list = True
        self.list_iterator = self.read_items.__iter__()

    def __iter__(self):
        return self


def invert_dict(dict):
    newdict = {}
    for (key,value) in dict.iteritems():
        newdict[value] = key
    return newdict


# arch-tag: 9573f21a-15c4-4b9a-9f26-e744b3a58aa5