本文整理汇总了Python中traitlets.HasTraits方法的典型用法代码示例。如果您正苦于以下问题:Python traitlets.HasTraits方法的具体用法?Python traitlets.HasTraits怎么用?Python traitlets.HasTraits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类traitlets
的用法示例。
在下文中一共展示了traitlets.HasTraits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validaton_error
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_validaton_error():
# Test with a squeeze coercion
def maxlen(trait, value):
if len(value) > 10:
raise ValueError('Too long sequence!')
return value
class Foo(HasTraits):
bar = SciType().valid(maxlen)
# Check that it works as expected:
foo = Foo(bar=list(range(5)))
assert foo.bar == list(range(5))
# Check that it fails as expected:
with pytest.raises(TraitError): # Should convert ValueError to TraitError
foo.bar = list(range(10, 40))
assert foo.bar == list(range(5))
# Check that it can again be set correctly
foo = Foo(bar=list(range(5, 10)))
assert foo.bar == list(range(5, 10))
示例2: test_array_equal
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_array_equal(self):
notifications = []
class Foo(HasTraits):
bar = Array([1, 2])
@observe('bar')
def _(self, change):
notifications.append(change)
foo = Foo()
foo.bar = [1, 2]
self.assertFalse(len(notifications))
foo.bar = [1, 1]
self.assertTrue(len(notifications))
示例3: test_initial_values
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_initial_values(self):
class Foo(HasTraits):
a = Array()
b = Array(dtype='int')
c = Array(None, allow_none=True)
d = Array([])
e = Array(Undefined)
foo = Foo()
self.assertTrue(np.array_equal(foo.a, np.array(0)))
self.assertTrue(np.array_equal(foo.b, np.array(0)))
self.assertTrue(foo.c is None)
self.assertTrue(np.array_equal(foo.d, []))
self.assertTrue(foo.e is Undefined)
示例4: test_allow_none
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_allow_none(self):
class Foo(HasTraits):
bar = Array()
baz = Array(allow_none=True)
foo = Foo()
with self.assertRaises(TraitError):
foo.bar = None
foo.baz = None
示例5: test_custom_validators
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_custom_validators(self):
# Test with a squeeze coercion
def squeeze(trait, value):
if 1 in value.shape:
value = np.squeeze(value)
return value
class Foo(HasTraits):
bar = Array().valid(squeeze)
foo = Foo(bar=[[1], [2]])
self.assertTrue(np.array_equal(foo.bar, [1, 2]))
foo.bar = [[1], [2], [3]]
self.assertTrue(np.array_equal(foo.bar, [1, 2, 3]))
# Test with a shape constraint
def shape(*dimensions):
def validator(trait, value):
if value.shape != dimensions:
raise TraitError('Expected an of shape %s and got and array with shape %s' % (dimensions, value.shape))
else:
return value
return validator
class Foo(HasTraits):
bar = Array(np.identity(2)).valid(shape(2, 2))
foo = Foo()
with self.assertRaises(TraitError):
foo.bar = [1]
new_value = [[0, 1], [1, 0]]
foo.bar = new_value
self.assertTrue(np.array_equal(foo.bar, new_value))
示例6: test_df_equal
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_df_equal(self):
notifications = []
class Foo(HasTraits):
bar = DataFrame([1, 2])
@observe('bar')
def _(self, change):
notifications.append(change)
foo = Foo()
foo.bar = [1, 2]
self.assertEqual(notifications, [])
foo.bar = [1, 1]
self.assertEqual(len(notifications), 1)
示例7: test_series_equal
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_series_equal(self):
notifications = []
class Foo(HasTraits):
bar = Series([1, 2])
@observe('bar')
def _(self, change):
notifications.append(change)
foo = Foo()
foo.bar = [1, 2]
self.assertEqual(notifications, [])
foo.bar = [1, 1]
self.assertEqual(len(notifications), 1)
示例8: test_ds_equal
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_ds_equal(self):
notifications = []
class Foo(HasTraits):
bar = Dataset({'foo': xr.DataArray([[0, 1, 2], [3, 4, 5]], coords={'x': ['a', 'b']}, dims=('x', 'y')), 'bar': ('x', [1, 2]), 'baz': 3.14})
@observe('bar')
def _(self, change):
notifications.append(change)
foo = Foo()
foo.bar = {'foo': xr.DataArray([[0, 1, 2], [3, 4, 5]], coords={'x': ['a', 'b']}, dims=('x', 'y')), 'bar': ('x', [1, 2]), 'baz': 3.14}
self.assertEqual(notifications, [])
foo.bar = {'foo': xr.DataArray([[0, 1, 2], [3, 4, 5]], coords={'x': ['a', 'b']}, dims=('x', 'y')), 'bar': ('x', [1, 2]), 'baz': 3.15}
self.assertEqual(len(notifications), 1)
示例9: test_coercion_validator
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_coercion_validator():
# Test with a squeeze coercion
def truncate(trait, value):
return value[:10]
class Foo(HasTraits):
bar = SciType().valid(truncate)
foo = Foo(bar=list(range(20)))
assert foo.bar == list(range(10))
foo.bar = list(range(10, 40))
assert foo.bar == list(range(10, 20))
示例10: __init__
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def __init__(self, obj, interface_name, exclude=None):
self._interface_name = interface_name
self._obj = obj
self._ro_props = OrderedDict()
self._rw_props = OrderedDict()
self._methods = []
self._signals = []
self._exclude = exclude
if isinstance(obj, HasTraits):
self._parse_traits()
示例11: test_typed_tuple_uninitialized_ints
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_typed_tuple_uninitialized_ints():
class TestCase(HasTraits):
value = TypedTuple(trait=Int())
obj = TestCase()
assert obj.value == ()
示例12: test_typed_tuple_init_ints
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_typed_tuple_init_ints():
class TestCase(HasTraits):
value = TypedTuple(trait=Int())
obj = TestCase(value=(1, 2, 3))
assert obj.value == (1, 2, 3)
示例13: test_typed_tuple_set_ints
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_typed_tuple_set_ints():
class TestCase(HasTraits):
value = TypedTuple(trait=Int())
obj = TestCase()
obj.value = (1, 2, 3)
assert obj.value == (1, 2, 3)
示例14: test_typed_tuple_default
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_typed_tuple_default():
class TestCase(HasTraits):
value = TypedTuple(default_value=(1, 2, 3))
obj = TestCase()
assert obj.value == (1, 2, 3)
示例15: test_typed_tuple_mixed_default
# 需要导入模块: import traitlets [as 别名]
# 或者: from traitlets import HasTraits [as 别名]
def test_typed_tuple_mixed_default():
class TestCase(HasTraits):
value = TypedTuple(default_value=(1, 2, 'foobar'))
obj = TestCase()
assert obj.value == (1, 2, 'foobar')