python - Traits for SortedListWithKey -
i using sortedlistwithkey
class new sortedcontainers
module (link). there way adapt list trait specify want sortedlistwithkey(instance(someclass), key=some_key)
?
to more specific, how can achieve like:
from traits.api import hastraits, instance, sortedlistwithkey # know cannot imported class myclass(hastraits): sorted_list = sortedlistwithkey(instance(elementclass))
after looking @ question again, think looking way of accessing sortedlistwithkey
object if list , using traits
or traitsui
machinery able validate/view/modify it. property
trait should here allowing view sortedlistwithkey
list
. have modified code example below have trait property(list(str))
, view simple traitsui:
from sortedcontainers import sortedlistwithkey traits.api import callable, hastraits, list, property, str 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()
Comments
Post a Comment