Skip to content

Instantly share code, notes, and snippets.

@jaidevd
Created July 2, 2015 06:44
Show Gist options
  • Save jaidevd/b4c4cfb35b8762aeb8ce to your computer and use it in GitHub Desktop.
Save jaidevd/b4c4cfb35b8762aeb8ce to your computer and use it in GitHub Desktop.
from traits.api import HasTraits, Property, Dict, cached_property, Any, Str, Int
class MyTraits(HasTraits):
a = Any
b = Property(Dict(key_trait=Str, value_trait=Int), depends_on=['a'])
@cached_property
def _get_b(self):
return {'a': self.a}
if __name__ == '__main__':
mt = MyTraits(a='hello')
print mt.b
@pankajp
Copy link

pankajp commented Jul 2, 2015

Only trait setters are validated, getters are not:

from traits.api import HasTraits, Property, Dict, cached_property, Any, Str, Int, CInt


class MyTraits(HasTraits):

    a = Any

    b = Property(CInt, depends_on=['a'])

    #@cached_property
    def _get_b(self):
        return {'a': self.a}

    def _set_b(self, value):
        print value



if __name__ == '__main__':
    mt = MyTraits(a='hello')
    print mt.b
    mt.b = '12'
    mt.b = 'a12'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment