Coverage for python/lsst/verify/blobset.py: 22%

76 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 09:25 +0000

1# This file is part of verify. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21__all__ = ['BlobSet'] 

22 

23from .blob import Blob 

24from .jsonmixin import JsonSerializationMixin 

25 

26 

27class BlobSet(JsonSerializationMixin): 

28 r"""A collection of `~lsst.verify.Blob`\ s. 

29 

30 Parameters 

31 ---------- 

32 blobs : `list` of `lsst.verify.Blob`\ s 

33 Blobs to include in the set. 

34 """ 

35 

36 def __init__(self, blobs=None): 

37 # internal dict of Blobs 

38 self._items = {} 

39 # internal mapping of blob names to identifiers 

40 self._name_map = {} 

41 

42 if blobs is not None: 

43 for blob in blobs: 

44 self.insert(blob) 

45 

46 @classmethod 

47 def deserialize(cls, blobs=None): 

48 """Create a BlobSet from a parsed JSON dataset. 

49 

50 Parameters 

51 ---------- 

52 blobs : `list`, optional 

53 A list of blob JSON serializations. 

54 

55 Returns 

56 ------- 

57 instance : `BlobSet` 

58 A `BlobSet` instance. 

59 """ 

60 instance = cls() 

61 if blobs is None: 

62 blobs = [] 

63 for blob_doc in blobs: 

64 blob = Blob.deserialize(**blob_doc) 

65 instance.insert(blob) 

66 return instance 

67 

68 def __getitem__(self, key): 

69 try: 

70 # key may be a blob's name, rather than identifier 

71 key = self._name_map[key] 

72 except KeyError: 

73 pass 

74 

75 return self._items[key] 

76 

77 def __setitem__(self, key, value): 

78 if not isinstance(value, Blob): 

79 message = ('Blob {0} is not a ' 

80 'lsst.verify.Blob-type') 

81 raise TypeError(message.format(value)) 

82 

83 if key != value.identifier: 

84 message = ("Key {0} is inconsistent with the blob's " 

85 "identifier, {1}") 

86 raise KeyError(message.format(key, value.identifier)) 

87 

88 self._items[key] = value 

89 self._name_map[value.name] = key 

90 

91 def __len__(self): 

92 return len(self._items) 

93 

94 def __contains__(self, key): 

95 try: 

96 # key may be a blob's name, rather than identifier 

97 key = self._name_map[key] 

98 except KeyError: 

99 pass 

100 

101 return key in self._items 

102 

103 def __delitem__(self, key): 

104 try: 

105 # key may be a blob's name, rather than identifier 

106 key = self._name_map[key] 

107 except KeyError: 

108 pass 

109 

110 del self._items[key] 

111 

112 def __iter__(self): 

113 for key in self._items: 

114 yield key 

115 

116 def __eq__(self, other): 

117 return self._items == other._items 

118 

119 def __ne__(self, other): 

120 return not self.__eq__(other) 

121 

122 def __str__(self): 

123 count = len(self) 

124 if count == 0: 

125 count_str = 'empty' 

126 elif count == 1: 

127 count_str = '1 Blob' 

128 else: 

129 count_str = '{count:d} Blobs'.format(count=count) 

130 return '<BlobSet: {0}>'.format(count_str) 

131 

132 def keys(self): 

133 """Get a sequence of blob identifiers, which are keys to the BlobSet. 

134 

135 Returns 

136 ------- 

137 keys : sequence of `str` 

138 Sequence of `Blob.identifier`. 

139 """ 

140 return self._items.keys() 

141 

142 def values(self): 

143 """Get a new view of the BlobSet's values. 

144 

145 Returns 

146 ------- 

147 blob : iterable of `lsst.verify.Blob` 

148 A collection of `~lsst.verify.Blob` objects that is updated when 

149 this object changes. 

150 """ 

151 return self._items.values() 

152 

153 def items(self): 

154 """Iterate over (identifier, `Blob`) pairs in the set. 

155 

156 Yields 

157 ------ 

158 item : `tuple` 

159 Tuple containing: 

160 

161 - `Blob.identifier` of the `Blob` (`str`) 

162 - `Blob` instance 

163 """ 

164 for item in self._items.items(): 

165 yield item 

166 

167 def insert(self, blob): 

168 """Insert a blob into the set. 

169 

170 Parameters 

171 ---------- 

172 blob : `Blob` 

173 `Blob` to insert into the set. It can be accessed by key from 

174 the BlobSet through its `Blob.identifier` string. 

175 """ 

176 self[blob.identifier] = blob 

177 

178 @property 

179 def json(self): 

180 """A `dict` that can be serialized as JSON.""" 

181 json_doc = JsonSerializationMixin._jsonify_list( 

182 [blob for identifier, blob in self.items()] 

183 ) 

184 return json_doc