SortedListWithKey 的特征
Traits for SortedListWithKey
我正在使用来自新 sortedcontainers
模块 (link) 的 SortedListWithKey
class。有没有办法调整 List 特征以指定我想要 SortedListWithKey(Instance(SomeClass), key=some_key)
?
更具体地说,如何实现类似的东西:
from traits.api import HasTraits, Instance, SortedListWithKey # i know it cannot really be imported
class MyClass(HasTraits):
sorted_list = SortedListWithKey(Instance(ElementClass))
再次查看您的问题后,我认为您正在寻找一种访问 SortedListWithKey
对象的方法,就好像它是一个列表并使用 Traits
或 TraitsUI
机器就可以validate/view/modify了。 Property
特性在这里应该有所帮助,允许您将 SortedListWithKey
视为 list
。我修改了下面的代码示例,使另一个特征成为 Property(List(Str))
并使用简单的 TraitsUI 查看它:
from sortedcontainers import SortedListWithKey
from traits.api import Callable, HasTraits, List, Property, Str
from traitsui.api import Item, View
class MyClass(HasTraits):
sorted_list_object = SortedListWithKey(key='key_func')
sorted_list = Property(List(Str), depends_on='sorted_list_object')
key_func = Callable
def _get_sorted_list(self):
return list(self.sorted_list_object)
def default_key_func(self):
def first_two_characters(element):
return element[:2]
return first_two_characters
def default_traits_view(self):
view = View(
Item('sorted_list', style='readonly')
)
return view
if __name__ == '__main__':
example = MyClass()
example.sorted_list_object = SortedListWithKey(
['first', 'second', 'third', 'fourth', 'fifth']
)
example.configure_traits()
我正在使用来自新 sortedcontainers
模块 (link) 的 SortedListWithKey
class。有没有办法调整 List 特征以指定我想要 SortedListWithKey(Instance(SomeClass), key=some_key)
?
更具体地说,如何实现类似的东西:
from traits.api import HasTraits, Instance, SortedListWithKey # i know it cannot really be imported
class MyClass(HasTraits):
sorted_list = SortedListWithKey(Instance(ElementClass))
再次查看您的问题后,我认为您正在寻找一种访问 SortedListWithKey
对象的方法,就好像它是一个列表并使用 Traits
或 TraitsUI
机器就可以validate/view/modify了。 Property
特性在这里应该有所帮助,允许您将 SortedListWithKey
视为 list
。我修改了下面的代码示例,使另一个特征成为 Property(List(Str))
并使用简单的 TraitsUI 查看它:
from sortedcontainers import SortedListWithKey
from traits.api import Callable, HasTraits, List, Property, Str
from traitsui.api import Item, View
class MyClass(HasTraits):
sorted_list_object = SortedListWithKey(key='key_func')
sorted_list = Property(List(Str), depends_on='sorted_list_object')
key_func = Callable
def _get_sorted_list(self):
return list(self.sorted_list_object)
def default_key_func(self):
def first_two_characters(element):
return element[:2]
return first_two_characters
def default_traits_view(self):
view = View(
Item('sorted_list', style='readonly')
)
return view
if __name__ == '__main__':
example = MyClass()
example.sorted_list_object = SortedListWithKey(
['first', 'second', 'third', 'fourth', 'fifth']
)
example.configure_traits()