[英]Inspect for synchronized traits: Traits/TraitsUI
我正在大型程序中遍歷所有特征,並且我們的許多特征是同步的。 例如,考慮結構的HasTrait對象:
a = Material1.ShellMaterial
b = Material2.CoreMaterial
c = Material3.MaterialX
在我們的應用程序中,事實證明a和c是同步特征。 換句話說, Material3.MaterialX
與Material1.ShellMaterial
相同,它們使用sync_trait()
(HasTraits API)進行設置 。
是否可以檢查a,b,c並動態確定a和c已同步?
目標是繪制所有這些圖,但向用戶隱藏多余的圖。 盡管這些對象表示相同的數據,但它們之間的典型比較(例如a==c
返回False
。
據我所知,沒有官方的API可以檢查特征的同步狀態。
當然,您可以再次簡單地再次調用sync_trait()
方法,以確保特征已同步(如果使用remove=True
,則不同步)。 結果,您將知道特征的同步狀態。
如果您不想更改同步狀態,則必須依靠非官方的API函數,這些函數未記錄在案並可能會更改-因此,使用這些函數的風險自負。
from traits.api import HasTraits, Float
class AA(HasTraits):
a =Float()
class BB(HasTraits):
b = Float()
aa = AA()
bb = BB()
aa.sync_trait("a", bb, "b")
# aa.a and bb.b are synchronized
# Now we use non-official API functions
info = aa._get_sync_trait_info()
synced = info.has_key("a") # True if aa.a is synchronized to some other trait
if synced:
sync_info = info["a"] # fails if a is not a synchronized trait
# sync_info is a dictionary which maps (id(bb),"b") to a tuple (wr, "b")
# If you do not know the id() of the HasTraits-object and the name of
# the trait, you have to loop through all elements of sync_info and
# search for the entry you want...
wr, name = sync_info[(id(bb), "b")]
# wr is a weakref to the class of bb, and name is the name
# of the trait which aa.a is synced to
cls = wr() # <__main__.BB at 0x6923a98>
同樣,使用后果自負,但對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.