简体   繁体   English

AttributeError: 'TraitCompound' object 没有属性 'clone'

[英]AttributeError: 'TraitCompound' object has no attribute 'clone'

Let's say I want to create, using the Traits package, a "bases" attribute that can be a 'none' string or a dictionary with keys which are 'hrf' or 'fourier' or 'fourier_han' or 'gamma' or 'fir' and with values which are a dictionary with keys which are 'derivs' or 'length' or 'order' and with values which are a list or a float).假设我想使用 Traits package 创建一个“bases”属性,它可以是一个“none”字符串或一个字典,其键是“hrf”或“fourier”或“fourier_han”或“gamma”或“fir” ' 并且值是一个字典,键是 'derivs' 或 'length' 或 'order' 并且值是列表或浮点数)。 Ex:前任:

bases == 'none'基地=='无'
or要么
bases == {'hrf': {'derivs': [0, 0]}}碱基 == {'hrf': {'derivs': [0, 0]}}
or要么
bases == {'fourier': {'length': 1.4, 'order': 3}} bases == {'fourier': {'length': 1.4, 'order': 3}}

if I define a 'none' string or a dictionary with keys which are 'hrf' or 'fourier' or 'fourier_han' or 'gamma' or 'fir' and with values which are a dictionary with keys which are 'derivs' or 'length' or 'order' and with values which are a list) it's working fine:如果我定义一个“无”字符串或一个字典,其键为“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]}}

But this is not exactly what I want.但这并不是我想要的。 If now I try to give the possibility to use list or float in the inside dictionary, it's not working:如果现在我尝试提供在内部字典中使用 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'

why?为什么?

It's not the first time I've interact with the team that develops the Traits project.这不是我第一次与开发 Traits 项目的团队互动。

As always, they very quickly managed to solve the problem and gave a very detailed answer on it. 一如既往,他们很快就设法解决了问题,并给出了非常详细的答案。

In a nutshell, as early as Traits 6.1, on the one hand, prefer:一言以蔽之,早在Traits 6.1,一方面,更喜欢:

traits.Union(traits.Float, traits.List)

to

traits.Either(traits.Float, traits.List)

On the other hand, pay attention to the default_value argument for Union() ( default for Either()).另一方面,注意 Union() 的default_value参数(Either() 的default )。 It would be better to initialise it by other means.最好通过其他方式初始化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM