AttributeError: 'TraitCompound' object has no attribute 'clone'
AttributeError: 'TraitCompound' object has no attribute 'clone'
假设我想使用 Traits 包创建一个 "bases" 属性,它可以是一个 'none' 字符串或一个字典,其键是 'hrf' 或 'fourier' 或 ' fourier_han' 或 'gamma' 或 'fir' 并且值是一个字典,键是 'derivs' 或 'length' 或 'order' 并且值是列表或浮点数)。例如:
碱基 == 'none'
或
碱基 == {'hrf': {'derivs': [0, 0]}}
或
碱基 == {'fourier': {'length': 1.4, 'order': 3}}
如果我定义一个 'none' 字符串或字典,其键为 'hrf' 或 'fourier' 或 'fourier_han' 或 'gamma' 或 'fir' 且值为键为 'derivs' 或 'length' 或 'order' 且值为列表的字典)它工作正常:
>>> import traits.api as traits
>>> class Foo(traits.HasTraits):
... bases = traits.Either(traits.Dict(traits.Enum("hrf","fourier","fourier_han","gamma","fir"),traits.Dict(traits.Enum("derivs","length","order"), traits.List)),'none',default={"hrf":{"derivs":[0,0]}})
...
>>> foo=Foo()
>>> foo.bases
{'hrf': {'derivs': [0, 0]}}
但这并不是我想要的。如果现在我尝试提供在内部字典中使用 list 或 float 的可能性,它是行不通的:
>>> import traits.api as traits
>>> class Foo(traits.HasTraits):
... bases = traits.Either(traits.Dict(traits.Enum("hrf","fourier","fourier_han","gamma","fir"),traits.Dict(traits.Enum("derivs","length","order"), traits.Either(traits.List, traits.Float))),'none',default={"hrf":{"derivs":[0,0]}})
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in Foo
File "/home/econdami/.local/lib/python3.7/site-packages/traits/trait_types.py", line 2804, in __init__
handler = handler.clone()
AttributeError: 'TraitCompound' object has no attribute 'clone'
为什么?
这不是我第一次与开发 Traits 项目的团队互动。
As always, they very quickly managed to solve the problem and gave a very detailed answer on it.
简而言之,早在Traits 6.1,一方面,更喜欢:
traits.Union(traits.Float, traits.List)
至
traits.Either(traits.Float, traits.List)
另一方面,请注意 Union() 的 default_value
参数(Either() 的 default
)。最好用其他方式初始化。
假设我想使用 Traits 包创建一个 "bases" 属性,它可以是一个 'none' 字符串或一个字典,其键是 'hrf' 或 'fourier' 或 ' fourier_han' 或 'gamma' 或 'fir' 并且值是一个字典,键是 'derivs' 或 'length' 或 'order' 并且值是列表或浮点数)。例如:
碱基 == 'none'
或
碱基 == {'hrf': {'derivs': [0, 0]}}
或
碱基 == {'fourier': {'length': 1.4, 'order': 3}}
如果我定义一个 'none' 字符串或字典,其键为 'hrf' 或 'fourier' 或 'fourier_han' 或 'gamma' 或 'fir' 且值为键为 'derivs' 或 'length' 或 'order' 且值为列表的字典)它工作正常:
>>> import traits.api as traits
>>> class Foo(traits.HasTraits):
... bases = traits.Either(traits.Dict(traits.Enum("hrf","fourier","fourier_han","gamma","fir"),traits.Dict(traits.Enum("derivs","length","order"), traits.List)),'none',default={"hrf":{"derivs":[0,0]}})
...
>>> foo=Foo()
>>> foo.bases
{'hrf': {'derivs': [0, 0]}}
但这并不是我想要的。如果现在我尝试提供在内部字典中使用 list 或 float 的可能性,它是行不通的:
>>> import traits.api as traits
>>> class Foo(traits.HasTraits):
... bases = traits.Either(traits.Dict(traits.Enum("hrf","fourier","fourier_han","gamma","fir"),traits.Dict(traits.Enum("derivs","length","order"), traits.Either(traits.List, traits.Float))),'none',default={"hrf":{"derivs":[0,0]}})
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in Foo
File "/home/econdami/.local/lib/python3.7/site-packages/traits/trait_types.py", line 2804, in __init__
handler = handler.clone()
AttributeError: 'TraitCompound' object has no attribute 'clone'
为什么?
这不是我第一次与开发 Traits 项目的团队互动。
As always, they very quickly managed to solve the problem and gave a very detailed answer on it.
简而言之,早在Traits 6.1,一方面,更喜欢:
traits.Union(traits.Float, traits.List)
至
traits.Either(traits.Float, traits.List)
另一方面,请注意 Union() 的 default_value
参数(Either() 的 default
)。最好用其他方式初始化。