1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
Using Traitlets
===============
In short, traitlets let the user define classes that have
1. Attributes (traits) with type checking and dynamically computed
default values
2. Traits emit change events when attributes are modified
3. Traitlets perform some validation and allow coercion of new trait
values on assignment. They also allow the user to define custom
validation logic for attributes based on the value of other
attributes.
Default values, and checking type and value
-------------------------------------------
At its most basic, traitlets provides type checking, and dynamic default
value generation of attributes on :class:`traitlets.HasTraits`
subclasses:
.. code:: python
from traitlets import HasTraits, Int, Unicode, default
import getpass
class Identity(HasTraits):
username = Unicode()
@default("username")
def _default_username(self):
return getpass.getuser()
.. code:: python
class Foo(HasTraits):
bar = Int()
foo = Foo(bar="3") # raises a TraitError
::
TraitError: The 'bar' trait of a Foo instance must be an int,
but a value of '3' <class 'str'> was specified
observe
-------
Traitlets implement the observer pattern
.. code:: python
class Foo(HasTraits):
bar = Int()
baz = Unicode()
foo = Foo()
def func(change):
print(change["old"])
print(change["new"]) # as of traitlets 4.3, one should be able to
# write print(change.new) instead
foo.observe(func, names=["bar"])
foo.bar = 1 # prints '0\n 1'
foo.baz = "abc" # prints nothing
When observers are methods of the class, a decorator syntax can be used.
.. code:: python
class Foo(HasTraits):
bar = Int()
baz = Unicode()
@observe("bar")
def _observe_bar(self, change):
print(change["old"])
print(change["new"])
Validation and Coercion
-----------------------
Custom Cross-Validation
^^^^^^^^^^^^^^^^^^^^^^^
Each trait type (``Int``, ``Unicode``, ``Dict`` etc.) may have its own
validation or coercion logic. In addition, we can register custom
cross-validators that may depend on the state of other attributes.
Basic Example: Validating the Parity of a Trait
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
from traitlets import HasTraits, TraitError, Int, Bool, validate
class Parity(HasTraits):
data = Int()
parity = Int()
@validate("data")
def _valid_data(self, proposal):
if proposal["value"] % 2 != self.parity:
raise TraitError("data and parity should be consistent")
return proposal["value"]
@validate("parity")
def _valid_parity(self, proposal):
parity = proposal["value"]
if parity not in [0, 1]:
raise TraitError("parity should be 0 or 1")
if self.data % 2 != parity:
raise TraitError("data and parity should be consistent")
return proposal["value"]
parity_check = Parity(data=2)
# Changing required parity and value together while holding cross validation
with parity_check.hold_trait_notifications():
parity_check.data = 1
parity_check.parity = 1
Notice how all of the examples above return
``proposal['value']``. Returning a value
is necessary for validation to work
properly, since the new value of the trait will be the
return value of the function decorated by ``@validate``. If this
function does not have any ``return`` statement, then the returned
value will be ``None``, instead of what we wanted (which is ``proposal['value']``).
However, we recommend that custom cross-validators don't modify the state of
the HasTraits instance.
Advanced Example: Validating the Schema
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``List`` and ``Dict`` trait types allow the validation of nested
properties.
.. code:: python
from traitlets import HasTraits, Dict, Bool, Unicode
class Nested(HasTraits):
value = Dict(
per_key_traits={"configuration": Dict(value_trait=Unicode()), "flag": Bool()}
)
n = Nested()
n.value = dict(flag=True, configuration={}) # OK
n.value = dict(flag=True, configuration="") # raises a TraitError.
However, for deeply nested properties it might be more appropriate to use an
external validator:
.. code:: python
import jsonschema
value_schema = {
"type": "object",
"properties": {
"price": {"type": "number"},
"name": {"type": "string"},
},
}
from traitlets import HasTraits, Dict, TraitError, validate, default
class Schema(HasTraits):
value = Dict()
@default("value")
def _default_value(self):
return dict(name="", price=1)
@validate("value")
def _validate_value(self, proposal):
try:
jsonschema.validate(proposal["value"], value_schema)
except jsonschema.ValidationError as e:
raise TraitError(e)
return proposal["value"]
s = Schema()
s.value = dict(name="", price="1") # raises a TraitError
Holding Trait Cross-Validation and Notifications
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sometimes it may be impossible to transition between valid states for a
``HasTraits`` instance by changing attributes one by one. The
``hold_trait_notifications`` context manager can be used to hold the custom
cross validation until the context manager is released. If a validation error
occurs, changes are rolled back to the initial state.
Custom Events
-------------
Finally, trait types can emit other events types than trait changes. This
capability was added so as to enable notifications on change of values in
container classes. The items available in the dictionary passed to the observer
registered with ``observe`` depends on the event type.
|