Return a tuple of basic Python values by recursively converting a mapping and all its sub-mappings. For example:: >>> to_tuple({7: [1,2,3], 9: {1: [2,6,8]}}) ((7, (1, 2, 3)), (9, ((1, (2, 6, 8)),)))
(collection)
| 173 | |
| 174 | |
| 175 | def to_tuple(collection): |
| 176 | """ |
| 177 | Return a tuple of basic Python values by recursively converting a mapping |
| 178 | and all its sub-mappings. |
| 179 | For example:: |
| 180 | >>> to_tuple({7: [1,2,3], 9: {1: [2,6,8]}}) |
| 181 | ((7, (1, 2, 3)), (9, ((1, (2, 6, 8)),))) |
| 182 | """ |
| 183 | if isinstance(collection, dict): |
| 184 | collection = tuple(collection.items()) |
| 185 | assert isinstance(collection, (tuple, list)) |
| 186 | results = [] |
| 187 | for item in collection: |
| 188 | if isinstance(item, (list, tuple, dict)): |
| 189 | results.append(to_tuple(item)) |
| 190 | else: |
| 191 | results.append(item) |
| 192 | return tuple(results) |
| 193 | |
| 194 | |
| 195 | party_person = 'person' |
no test coverage detected