当前位置: 首页>>代码示例>>Python>>正文


Python traitlets.HasTraits方法代码示例

本文整理汇总了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)) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:22,代码来源:test_validators.py

示例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)) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:14,代码来源:test_traittypes.py

示例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) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:15,代码来源:test_traittypes.py

示例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 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:10,代码来源:test_traittypes.py

示例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)) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:34,代码来源:test_traittypes.py

示例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) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:14,代码来源:test_traittypes.py

示例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) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:14,代码来源:test_traittypes.py

示例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) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:14,代码来源:test_traittypes.py

示例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)) 
开发者ID:jupyter-widgets,项目名称:traittypes,代码行数:14,代码来源:test_validators.py

示例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() 
开发者ID:cyanogen,项目名称:uchroma,代码行数:13,代码来源:dbus_utils.py

示例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 == () 
开发者ID:luckystarufo,项目名称:pySINDy,代码行数:8,代码来源:test_traits.py

示例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) 
开发者ID:luckystarufo,项目名称:pySINDy,代码行数:8,代码来源:test_traits.py

示例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) 
开发者ID:luckystarufo,项目名称:pySINDy,代码行数:9,代码来源:test_traits.py

示例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) 
开发者ID:luckystarufo,项目名称:pySINDy,代码行数:8,代码来源:test_traits.py

示例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') 
开发者ID:luckystarufo,项目名称:pySINDy,代码行数:8,代码来源:test_traits.py


注:本文中的traitlets.HasTraits方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。