gpt4 book ai didi

python - SortedListWithKey 的特征

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:16 27 4
gpt4 key购买 nike

我正在使用新的 sortedcontainers 模块 ( link ) 中的 SortedListWithKey 类。有没有办法调整 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 机制,以便能够验证/查看/修改它。 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()

关于python - SortedListWithKey 的特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com