test_jstraitlets.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:schemapi 作者: altair-viz 项目源码 文件源码
def test_to_python():
    class Foo(jst.JSONHasTraits):
        _required_traits = ['a', 'b']
        a = jst.JSONNumber()
        b = jst.JSONString()

    class Bar(jst.JSONHasTraits):
        c = jst.JSONArray(jst.JSONNumber())
        d = jst.JSONInstance(Foo)
        e = jst.JSONArray(jst.JSONInstance(Foo))

    D = {'c': [1, 2, 3], 'd': {'a': 5, 'b': 'blah'},
         'e': [{'a': 3, 'b': 'foo'}, {'a': 4, 'b': 'bar'}]}
    obj = Bar.from_dict(D)
    obj2 = eval(obj.to_python())
    assert obj2.to_dict() == obj.to_dict() == D

    # Make sure there is an error if required traits are missing
    foo = Foo(a=4)
    with pytest.raises(T.TraitError):
        foo.to_python()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号