Package buildbot :: Package util :: Module bbcollections
[frames] | no frames]

Source Code for Module buildbot.util.bbcollections

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  try: 
17      from collections import defaultdict 
18      assert defaultdict 
19  except ImportError: 
20      # collections.defaultdict only appeared in py2.5, but buildbot supports 2.4 
21 - class defaultdict(dict):
22 - def __init__(self, default_factory=None, *args, **kwargs):
23 self._default_factory = default_factory 24 dict.__init__(self, *args, **kwargs)
25 - def __getitem__(self, key):
26 if key not in self and self._default_factory: 27 self[key] = self._default_factory() 28 return dict.__getitem__(self, key)
29
30 -class KeyedSets:
31 """ 32 This is a collection of named sets. In principal, it contains an empty set 33 for every name, and you can add things to sets, discard things from sets, 34 and so on. 35 36 >>> ks = KeyedSets() 37 >>> ks['tim'] # get a named set 38 set([]) 39 >>> ks.add('tim', 'friendly') # add an element to a set 40 >>> ks.add('tim', 'dexterous') 41 >>> ks['tim'] 42 set(['friendly', 'dexterous']) 43 >>> 'tim' in ks # membership testing 44 True 45 >>> 'ron' in ks 46 False 47 >>> ks.discard('tim', 'friendly')# discard set element 48 >>> ks.pop('tim') # return set and reset to empty 49 set(['dexterous']) 50 >>> ks['tim'] 51 set([]) 52 53 This class is careful to conserve memory space - empty sets do not occupy 54 any space. 55 """
56 - def __init__(self):
57 self.d = dict()
58 - def add(self, key, value):
59 if key not in self.d: 60 self.d[key] = set() 61 self.d[key].add(value)
62 - def discard(self, key, value):
63 if key in self.d: 64 self.d[key].discard(value) 65 if not self.d[key]: 66 del self.d[key]
67 - def __contains__(self, key):
68 return key in self.d
69 - def __getitem__(self, key):
70 return self.d.get(key, set())
71 - def pop(self, key):
72 if key in self.d: 73 return self.d.pop(key) 74 return set()
75