當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。