Created
April 21, 2011 22:12
-
-
Save satra/935601 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [2]: from enthought.traits.api import HasTraits, Int | |
| In [3]: from cPickle import dumps, loads | |
| In [9]: a = HasTraits() | |
| In [10]: a.add_trait('foo', Int) | |
| In [11]: a.foo = 1 | |
| In [12]: a.foo = 'a' | |
| --------------------------------------------------------------------------- | |
| TraitError Traceback (most recent call last) | |
| TraitError: The 'foo' trait of a HasTraits instance must be an integer, but a value of 'a' <type 'str'> was specified. | |
| In [13]: pkld_a = dumps(a) | |
| In [14]: unpkld_a = loads(pkld_a) | |
| In [16]: unpkld_a.foo | |
| Out[16]: 1 | |
| In [17]: unpkld_a.foo = 'a' | |
| In [18]: unpkld_a.foo | |
| Out[18]: 'a' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def test_dynamic_traits(): | |
| from nose.tools import assert_raises | |
| from traits.api import HasTraits, Int | |
| from cPickle import dumps, loads | |
| a = HasTraits() | |
| a.add_trait('foo', Int) | |
| a.foo = 1 | |
| assign_a = lambda : setattr(a, 'foo', 'a') | |
| assert_raises(Exception, assign_a) | |
| pkld_a = dumps(a) | |
| unpkld_a = loads(pkld_a) | |
| assign_a_again = lambda : setattr(unpkld_a, 'foo', 'a') | |
| assert_raises(Exception, assign_a_again) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment