~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to examples/demo/Advanced/Settable_cached_property.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  Copyright (c) 2007, Enthought, Inc.
 
2
#  License: BSD Style.
 
3
 
 
4
"""
 
5
The example demonstrates how to create a 'settable cached' property. The example
 
6
itself is nonsensical, and is provided mainly to show how to set up such a
 
7
property, and how it differs from a standard 'cached' property.
 
8
 
 
9
A normal 'cached' property does not have a 'setter' method, meaning that the
 
10
property is read-only. It usually represents a value which depends upon the
 
11
state of other traits for its value. The cached property provides both a
 
12
mechanism for remembering (i.e. caching) the current value of the property as
 
13
well as a means of automatically detecting when the value of the property
 
14
changes (which causes the cache to be flushed), and notifying any associated
 
15
trait listeners that the value of the property has changed.
 
16
 
 
17
Normally there is no 'setter' for such a property because the value is derived
 
18
from the value of other traits.
 
19
 
 
20
However, it is possible to define a 'settable cached' property which in addition
 
21
to the capabilities of a normal 'cached' property, also allows the property's
 
22
value to be explicitly set.
 
23
 
 
24
To accomplish this, simply set the 'settable' argument to the
 
25
'property_depends_on' decorator to True (see the '_get_c' method in the
 
26
example code). When set this way, an appropriate 'setter' method is
 
27
automatically generated for the associated property.
 
28
 
 
29
This allows code to set the value of the property directly if desired, subject
 
30
to any constraints specified in the property declaration. For example, in the
 
31
example, the 'c' trait is a 'settable cached' property whose value must be an
 
32
integer. Attempting to set a non-integer value of the property will raise an
 
33
exception, just like any other trait would.
 
34
 
 
35
If any of the traits which the property depends upon change value, the current
 
36
value of the property will be flushed from the cache and a change notification
 
37
for the property will be generated. Any code that then attempts to read the
 
38
value of the property will result in the cache being reloaded with the new value
 
39
returned by the property's 'getter' method.
 
40
 
 
41
In the example, trait 'c' is a 'settable cached' property which returns the
 
42
product of 'a' times 'b', and trait 'd' is a normal 'cached' property that
 
43
returns double the value of 'c'.
 
44
 
 
45
To see the effect of these traits in action, try moving the 'a' and 'b' sliders
 
46
and watching the 'c' and 'd' traits update. This demonstrates how 'c' and 'd'
 
47
are properties that depend upon the values of other traits.
 
48
 
 
49
Now try changing the value of the 'c' trait by moving the slider or typing a
 
50
new value into the text entry field. You will see that the 'd' trait updates
 
51
as well, illustrating that the 'c' trait can be set directly, as well as
 
52
indirectly by changes to 'a' and 'b'.
 
53
 
 
54
Also, try typing non-numeric values into the 'c' field and you will see that
 
55
any values set are being type checked as well (i.e. they must be integer
 
56
values).
 
57
 
 
58
Now try typing a value into the 'd' trait and you will see that an error
 
59
results (indicated by the text entry field turning red), because this is a
 
60
normal 'cached' trait that has no 'setter' method defined.
 
61
"""
 
62
 
 
63
#-- Imports --------------------------------------------------------------------
 
64
 
 
65
from traits.api \
 
66
    import HasTraits, Int, Range, Property, property_depends_on
 
67
 
 
68
from traitsui.api \
 
69
    import View, Item, RangeEditor
 
70
 
 
71
#-- Demo Class -----------------------------------------------------------------
 
72
 
 
73
class SettableCachedProperty ( HasTraits ):
 
74
 
 
75
    a = Range( 1, 10 )
 
76
    b = Range( 1, 10 )
 
77
    c = Property( Int )
 
78
    d = Property
 
79
 
 
80
    view = View(
 
81
        Item( 'a' ),
 
82
        Item( 'b' ),
 
83
        '_',
 
84
        Item( 'c',
 
85
              editor = RangeEditor( low = 1, high = 100, mode = 'slider' ) ),
 
86
        Item( 'c' ),
 
87
        '_',
 
88
        Item( 'd',
 
89
              editor = RangeEditor( low = 1, high = 400, mode = 'slider' ) ),
 
90
        Item( 'd' ),
 
91
        width = 0.3
 
92
    )
 
93
 
 
94
    @property_depends_on( 'a,b', settable = True )
 
95
    def _get_c(self):
 
96
        return (self.a * self.b)
 
97
 
 
98
    @property_depends_on( 'c' )
 
99
    def _get_d(self):
 
100
        return (self.c + self.c)
 
101
 
 
102
#-- Run the demo ---------------------------------------------------------------
 
103
 
 
104
# Create the demo:
 
105
demo = SettableCachedProperty()
 
106
 
 
107
# Run the demo (if invoked from the command line):
 
108
if __name__ == '__main__':
 
109
    demo.configure_traits()