Show More
@@ -356,10 +356,26 b' class TraitType(object):' | |||||
356 | """Create a new instance of the default value.""" |
|
356 | """Create a new instance of the default value.""" | |
357 | return self.default_value |
|
357 | return self.default_value | |
358 |
|
358 | |||
359 |
def |
|
359 | def instance_init(self): | |
|
360 | """Part of the initialization which may depends on the underlying | |||
|
361 | HasTraits instance. | |||
|
362 | ||||
|
363 | It is typically overloaded for specific trait types. | |||
|
364 | ||||
|
365 | This method is called by :meth:`HasTraits.__new__` and in the | |||
|
366 | :meth:`TraitType.instance_init` method of trait types holding | |||
|
367 | other trait types. | |||
|
368 | """ | |||
360 | pass |
|
369 | pass | |
361 |
|
370 | |||
362 |
def |
|
371 | def init_default_value(self, obj): | |
|
372 | """Instantiate the default value for the trait type. | |||
|
373 | ||||
|
374 | This method is called by :meth:`TraitType.set_default_value` in the | |||
|
375 | case a default value is provided at construction time or later when | |||
|
376 | accessing the trait value for the first time in | |||
|
377 | :meth:`HasTraits.__get__`. | |||
|
378 | """ | |||
363 | value = self.get_default_value() |
|
379 | value = self.get_default_value() | |
364 | value = self._validate(obj, value) |
|
380 | value = self._validate(obj, value) | |
365 | obj._trait_values[self.name] = value |
|
381 | obj._trait_values[self.name] = value | |
@@ -368,11 +384,10 b' class TraitType(object):' | |||||
368 | def set_default_value(self, obj): |
|
384 | def set_default_value(self, obj): | |
369 | """Set the default value on a per instance basis. |
|
385 | """Set the default value on a per instance basis. | |
370 |
|
386 | |||
371 |
This method is called by :meth:`HasTraits.__new__` to |
|
387 | This method is called by :meth:`HasTraits.__new__` to instantiate and | |
372 |
validate the default value. |
|
388 | validate the default value. The creation and validation of | |
373 | default values must be delayed until the parent :class:`HasTraits` |
|
389 | default values must be delayed until the parent :class:`HasTraits` | |
374 | class has been instantiated. |
|
390 | class has been instantiated. | |
375 |
|
||||
376 | Parameters |
|
391 | Parameters | |
377 | ---------- |
|
392 | ---------- | |
378 | obj : :class:`HasTraits` instance |
|
393 | obj : :class:`HasTraits` instance | |
@@ -388,11 +403,11 b' class TraitType(object):' | |||||
388 | break |
|
403 | break | |
389 | else: |
|
404 | else: | |
390 | # We didn't find one. Do static initialization. |
|
405 | # We didn't find one. Do static initialization. | |
391 |
self. |
|
406 | self.init_default_value(obj) | |
392 | return |
|
407 | return | |
393 | # Complete the dynamic initialization. |
|
408 | # Complete the dynamic initialization. | |
394 | obj._trait_dyn_inits[self.name] = meth_name |
|
409 | obj._trait_dyn_inits[self.name] = meth_name | |
395 |
|
410 | |||
396 | def __get__(self, obj, cls=None): |
|
411 | def __get__(self, obj, cls=None): | |
397 | """Get the value of the trait by self.name for the instance. |
|
412 | """Get the value of the trait by self.name for the instance. | |
398 |
|
413 | |||
@@ -416,12 +431,12 b' class TraitType(object):' | |||||
416 | obj._trait_values[self.name] = value |
|
431 | obj._trait_values[self.name] = value | |
417 | return value |
|
432 | return value | |
418 | else: |
|
433 | else: | |
419 |
return self. |
|
434 | return self.init_default_value(obj) | |
420 | except Exception: |
|
435 | except Exception: | |
421 | # HasTraits should call set_default_value to populate |
|
436 | # HasTraits should call set_default_value to populate | |
422 | # this. So this should never be reached. |
|
437 | # this. So this should never be reached. | |
423 | raise TraitError('Unexpected error in TraitType: ' |
|
438 | raise TraitError('Unexpected error in TraitType: ' | |
424 |
|
|
439 | 'default value not set properly') | |
425 | else: |
|
440 | else: | |
426 | return value |
|
441 | return value | |
427 |
|
442 | |||
@@ -550,7 +565,7 b' class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):' | |||||
550 | pass |
|
565 | pass | |
551 | else: |
|
566 | else: | |
552 | if isinstance(value, TraitType): |
|
567 | if isinstance(value, TraitType): | |
553 |
value. |
|
568 | value.instance_init() | |
554 | if key not in kw: |
|
569 | if key not in kw: | |
555 | value.set_default_value(inst) |
|
570 | value.set_default_value(inst) | |
556 |
|
571 | |||
@@ -865,9 +880,9 b' class Type(ClassBasedTraitType):' | |||||
865 | return result + ' or None' |
|
880 | return result + ' or None' | |
866 | return result |
|
881 | return result | |
867 |
|
882 | |||
868 |
def |
|
883 | def instance_init(self): | |
869 | self._resolve_classes() |
|
884 | self._resolve_classes() | |
870 |
super(Type, self). |
|
885 | super(Type, self).instance_init() | |
871 |
|
886 | |||
872 | def _resolve_classes(self): |
|
887 | def _resolve_classes(self): | |
873 | if isinstance(self.klass, py3compat.string_types): |
|
888 | if isinstance(self.klass, py3compat.string_types): | |
@@ -974,9 +989,9 b' class Instance(ClassBasedTraitType):' | |||||
974 |
|
989 | |||
975 | return result |
|
990 | return result | |
976 |
|
991 | |||
977 |
def |
|
992 | def instance_init(self): | |
978 | self._resolve_classes() |
|
993 | self._resolve_classes() | |
979 |
super(Instance, self). |
|
994 | super(Instance, self).instance_init() | |
980 |
|
995 | |||
981 | def _resolve_classes(self): |
|
996 | def _resolve_classes(self): | |
982 | if isinstance(self.klass, py3compat.string_types): |
|
997 | if isinstance(self.klass, py3compat.string_types): | |
@@ -1071,12 +1086,12 b' class Union(TraitType):' | |||||
1071 | self.default_value = self.trait_types[0].get_default_value() |
|
1086 | self.default_value = self.trait_types[0].get_default_value() | |
1072 | super(Union, self).__init__(**metadata) |
|
1087 | super(Union, self).__init__(**metadata) | |
1073 |
|
1088 | |||
1074 |
def |
|
1089 | def instance_init(self): | |
1075 | for trait_type in self.trait_types: |
|
1090 | for trait_type in self.trait_types: | |
1076 | trait_type.name = self.name |
|
1091 | trait_type.name = self.name | |
1077 | trait_type.this_class = self.this_class |
|
1092 | trait_type.this_class = self.this_class | |
1078 |
trait_type. |
|
1093 | trait_type.instance_init() | |
1079 |
super(Union, self). |
|
1094 | super(Union, self).instance_init() | |
1080 |
|
1095 | |||
1081 | def validate(self, obj, value): |
|
1096 | def validate(self, obj, value): | |
1082 | for trait_type in self.trait_types: |
|
1097 | for trait_type in self.trait_types: | |
@@ -1457,11 +1472,11 b' class Container(Instance):' | |||||
1457 | validated.append(v) |
|
1472 | validated.append(v) | |
1458 | return self.klass(validated) |
|
1473 | return self.klass(validated) | |
1459 |
|
1474 | |||
1460 |
def |
|
1475 | def instance_init(self): | |
1461 | if isinstance(self._trait, TraitType): |
|
1476 | if isinstance(self._trait, TraitType): | |
1462 | self._trait.this_class = self.this_class |
|
1477 | self._trait.this_class = self.this_class | |
1463 |
self._trait. |
|
1478 | self._trait.instance_init() | |
1464 |
super(Container, self). |
|
1479 | super(Container, self).instance_init() | |
1465 |
|
1480 | |||
1466 |
|
1481 | |||
1467 | class List(Container): |
|
1482 | class List(Container): | |
@@ -1616,7 +1631,7 b' class Tuple(Container):' | |||||
1616 | raise TraitError(e) |
|
1631 | raise TraitError(e) | |
1617 |
|
1632 | |||
1618 | validated = [] |
|
1633 | validated = [] | |
1619 | for t,v in zip(self._traits, value): |
|
1634 | for t, v in zip(self._traits, value): | |
1620 | try: |
|
1635 | try: | |
1621 | v = t._validate(obj, v) |
|
1636 | v = t._validate(obj, v) | |
1622 | except TraitError: |
|
1637 | except TraitError: | |
@@ -1625,12 +1640,12 b' class Tuple(Container):' | |||||
1625 | validated.append(v) |
|
1640 | validated.append(v) | |
1626 | return tuple(validated) |
|
1641 | return tuple(validated) | |
1627 |
|
1642 | |||
1628 |
def |
|
1643 | def instance_init(self): | |
1629 | for trait in self._traits: |
|
1644 | for trait in self._traits: | |
1630 | if isinstance(trait, TraitType): |
|
1645 | if isinstance(trait, TraitType): | |
1631 | trait.this_class = self.this_class |
|
1646 | trait.this_class = self.this_class | |
1632 |
trait. |
|
1647 | trait.instance_init() | |
1633 |
super(Container, self). |
|
1648 | super(Container, self).instance_init() | |
1634 |
|
1649 | |||
1635 |
|
1650 | |||
1636 | class Dict(Instance): |
|
1651 | class Dict(Instance): | |
@@ -1706,12 +1721,11 b' class Dict(Instance):' | |||||
1706 | validated[key] = v |
|
1721 | validated[key] = v | |
1707 | return self.klass(validated) |
|
1722 | return self.klass(validated) | |
1708 |
|
1723 | |||
1709 |
def instance_init(self |
|
1724 | def instance_init(self): | |
1710 | if isinstance(self._trait, TraitType): |
|
1725 | if isinstance(self._trait, TraitType): | |
1711 | self._trait.this_class = self.this_class |
|
1726 | self._trait.this_class = self.this_class | |
1712 | if hasattr(self._trait, '_resolve_classes'): |
|
1727 | self._trait.instance_init() | |
1713 | self._trait._resolve_classes(obj) |
|
1728 | super(Dict, self).instance_init() | |
1714 | super(Dict, self).instance_init(obj) |
|
|||
1715 |
|
1729 | |||
1716 |
|
1730 | |||
1717 | class EventfulDict(Instance): |
|
1731 | class EventfulDict(Instance): |
General Comments 0
You need to be logged in to leave comments.
Login now