def testNoOverwrite(self): mapping = data_structures.Mapping() original = data_structures.List() mapping["a"] = original with self.assertRaises(ValueError): mapping["a"] = data_structures.List() self.assertIs(original, mapping["a"]) with self.assertRaises(AttributeError): del mapping["a"] mapping.update(b=data_structures.Mapping()) with self.assertRaises(ValueError): mapping.update({"b": data_structures.Mapping()})
def testHashing(self): has_mappings = set([data_structures.Mapping(), data_structures.Mapping()]) self.assertEqual(2, len(has_mappings)) self.assertNotIn(data_structures.Mapping(), has_mappings) # In contrast to Mapping, dict wrappers are not hashable a = tracking.Checkpointable() a.d = {} self.assertEqual({}, a.d) self.assertFalse({} != a.d) # pylint: disable=g-explicit-bool-comparison self.assertNotEqual({1: 2}, a.d) with self.assertRaisesRegexp(TypeError, "unhashable"): set([a.d])
def __init__(self): super(HasMapping, self).__init__() self.layer_dict = data_structures.Mapping(output=core.Dense(7)) self.layer_dict["norm"] = data_structures.List() self.layer_dict["dense"] = data_structures.List() self.layer_dict["dense"].extend([ core.Dense(5), core.Dense(6, kernel_regularizer=math_ops.reduce_sum) ]) self.layer_dict["norm"].append(normalization.BatchNormalization()) self.layer_dict["norm"].append(normalization.BatchNormalization())
def testNonStringKeys(self): mapping = data_structures.Mapping() with self.assertRaises(TypeError): mapping[1] = data_structures.List()
def testHashing(self): has_mappings = set([data_structures.Mapping(), data_structures.Mapping()]) self.assertEqual(2, len(has_mappings)) self.assertNotIn(data_structures.Mapping(), has_mappings)