コード例 #1
0
 def readContainerMap(self, spec):
     results = dict()
     key_ttype, key_spec = spec[0], spec[1]
     val_ttype, val_spec = spec[2], spec[3]
     is_immutable = spec[4]
     (map_ktype, map_vtype, map_len) = self.readMapBegin()
     # TODO: compare types we just decoded with thrift_spec and
     # abort/skip if types disagree
     key_reader = getattr(self,
                          self._ttype_handlers(key_ttype, key_spec)[0])
     val_reader = getattr(self,
                          self._ttype_handlers(val_ttype, val_spec)[0])
     # list values are simple types
     for idx in range(map_len):
         if key_spec is None:
             k_val = key_reader()
         else:
             k_val = self.readFieldByTType(key_ttype, key_spec)
         if val_spec is None:
             v_val = val_reader()
         else:
             v_val = self.readFieldByTType(val_ttype, val_spec)
         # this raises a TypeError with unhashable keys types
         # i.e. this fails: d=dict(); d[[0,1]] = 2
         results[k_val] = v_val
     self.readMapEnd()
     return TFrozenDict(results) if is_immutable else results
コード例 #2
0
 def test_map(self):
     """Test that annotated map field can be serialized and deserialized"""
     x = CompactProtoTestStruct(map_byte_map={
         TFrozenDict({42: 42, 100: -100}): 99,
         TFrozenDict({0: 0}): 100,
         TFrozenDict({}): 0,
     })
     x2 = self._roundtrip(x, CompactProtoTestStruct())
     self.assertEqual(x2.map_byte_map[TFrozenDict({42: 42, 100: -100})], 99)
     self.assertEqual(x2.map_byte_map[TFrozenDict({0: 0})], 100)
     self.assertEqual(x2.map_byte_map[TFrozenDict({})], 0)
コード例 #3
0
 def test_dict_is_hashable_only_after_frozen(self):
     d0 = {}
     self.assertFalse(isinstance(d0, collections.Hashable))
     d1 = TFrozenDict(d0)
     self.assertTrue(isinstance(d1, collections.Hashable))
コード例 #4
0
 },
 "set_byte_map": {
     frozenset((
         1,
         2,
         3,
     )): 1,
     frozenset((
         0,
         1,
     )): 2,
     frozenset(()): 0,
 },
 "map_byte_map": {
     TFrozenDict({
         1: 1,
     }): 1,
     TFrozenDict({
         2: 2,
     }): 2,
     TFrozenDict({}): 0,
 },
 "byte_map_map": {
     0: {},
     1: {
         1: 1,
     },
     2: {
         1: 1,
         2: 2,
     },