def test_main(self): class Klass1: pass class Klass2: pass class Klass3: pass ckm = ClassKeyedMap([(Klass1, 1), (Klass2, 2)]) self.assertEqual(1, ckm[Klass1]) self.assertEqual(2, ckm[Klass2]) self.assertEqual(2, len(ckm)) self.assertIsNone(ckm[Klass3]) self.assertEqual(3, len(ckm)) self.assertIsNone(ckm.default) self.assertTrue(hasattr(ckm, '__contains__')) self.assertIn(Klass1, ckm) empty = ClassKeyedMap() self.assertIs(True, bool(ckm)) self.assertIs(False, bool(empty)) self.assertEqual(0, len(empty)) self.assertNotIn(Klass1, empty) keys_set = {Klass1, Klass2, Klass3} self.assertEqual(keys_set, set(ckm)) self.assertEqual(keys_set, set(ckm.keys())) self.assertEqual({1, 2, None}, set(ckm.values())) self.assertEqual({(Klass1, 1), (Klass2, 2), (Klass3, None)}, set(ckm.items()) ) r = repr(ckm) self.assertTrue(r.startswith('ClassKeyedMap('))
def __init__(self, models_to_register: Iterable[Tuple[ Type[Model], Type[AbstractCellSorter]]] = DEFAULT_MODELS): self._sorters: ClassKeyedMap = ClassKeyedMap(default=None) for model, sorter_cls in models_to_register: self.register(model=model, sorter_cls=sorter_cls)
def __init__(self, default=lv_form.RegularRelatedField, models_to_register=DEFAULT_MODELS): self._builders_4_models = ClassKeyedMap(default=None) self.register_default(default) for model, builder in models_to_register: self.register_related_model(model=model, sfield_builder=builder)
def __init__(self, to_register=DEFAULT_SORTERS): # self._sorters_4_modelfields = {} # TODO: when order is kept (py3.6+) self._sorters_4_modelfields = OrderedDict() self._sorters_4_modelfieldtypes = ClassKeyedMap(default=None) for model_field_cls, sorter_cls in to_register: self.register_model_field_type(type=model_field_cls, sorter_cls=sorter_cls)
def __init__(self, to_register=DEFAULT_REGISTRATIONS, choice_sfield_builder=lv_form.RegularChoiceField): self._builders_4_modelfields = {} self._builders_4_modelfieldtypes = ClassKeyedMap(default=None) self.register_choice_builder(choice_sfield_builder) for model_field_cls, builder in to_register: self.register_model_field_type(type=model_field_cls, sfield_builder=builder)
def test_inheritage03(self): "Inheritance order must be kept when cache is filled too" class Klass1: pass class Klass2(Klass1): pass class Klass3(Klass2): pass class Klass4(Klass3): pass ckm = ClassKeyedMap([(Klass1, 1), (Klass3, 3)], # Not 2 & 4 default=0 ) self.assertEqual(1, ckm[Klass2]) self.assertEqual(3, ckm[Klass4])
def __init__(self, to_register: Iterable[ Tuple[Type[Field], Type[AbstractCellSorter]]] = DEFAULT_SORTERS): # self._sorters_4_modelfields = {} # TODO: when order is kept (py3.7) self._sorters_4_modelfields: Dict[Field, AbstractCellSorter] = OrderedDict() self._sorters_4_modelfieldtypes: ClassKeyedMap = ClassKeyedMap( default=None) for model_field_cls, sorter_cls in to_register: self.register_model_field_type(type=model_field_cls, sorter_cls=sorter_cls)
def test_inheritage02(self): "Inheriting values: more complex case (the nearest parent should be found)" class Klass1: pass class Klass2(Klass1): pass class Klass3(Klass2): pass class Klass4(Klass3): pass ckm = ClassKeyedMap([(Klass1, 1), (Klass2, 2), (Klass3, 3), (Klass4, 4)], default=0 ) class Klass5(Klass4): pass self.assertEqual(4, ckm[Klass5])
def test_inheritage01(self): "Inheriting values" class Klass1: pass class Klass2: pass class Klass3(Klass2): pass class Klass4(Klass3): pass ckm = ClassKeyedMap([(Klass1, 1), (Klass2, 2)]) self.assertEqual(2, ckm[Klass3]) self.assertEqual(2, ckm[Klass4]) ckm[Klass3] = 3 self.assertEqual(3, ckm[Klass3]) self.assertEqual(3, ckm[Klass4]) # Cache must be updated
def test_setitem(self): "Other default value + __setitem__" class Klass1: pass class Klass2: pass class Klass3: pass ckm = ClassKeyedMap(default=0) result = ckm[Klass1] = 1 ckm[Klass2] = 2 self.assertEqual(0, ckm[Klass3]) self.assertEqual(1, ckm[Klass1]) self.assertEqual(1, result) self.assertEqual(3, len(ckm)) self.assertEqual(0, ckm[Klass3]) # 2nd access should hit the cache self.assertEqual(3, len(ckm))
def __init__(self): self._enums_4_fields = {} self._enums_4_field_types = ClassKeyedMap() self._enums_4_models = {}
def __init__(self, models_to_register=DEFAULT_MODELS): self._sorters = ClassKeyedMap(default=None) for model, sorter_cls in models_to_register: self.register(model=model, sorter_cls=sorter_cls)
def __init__(self, default_field_explainer_class=FieldChangeExplainer): self._line_explainer_classes = {} self._field_explainer_classes = ClassKeyedMap( default=default_field_explainer_class)