Sorted List Trait

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Sorted List Trait

Alexander Held
Hi all,

is there already an implementation for a List Trait that keeps the list sorted (with regard to a given key)?
I want the list to be sorted upon assigment and whenever elements of the list are added or changed.
If not, what is the best way, to implement it without having to explicitly add notification handlers?
I imagine to have s.th. like

class Foo(HasTraits):
   sortedList = SortedList(key=lambda x: x.someValue)

Alexander


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Sorted List Trait

Chris Colbert
There is nothing like that in traits to the best of my knowledge. 

However, you could install the blist module (http://pypi.python.org/pypi/blist/) which includes a sortedlist, and assign it to an Any trait. Of course you wont be able to get trait notifications for items events, but you will get notification of the whole list is swapped out. 

If you want to get items events, it will be a bit more tricky because sortedlist is not actually a python list (it's a btree), and it doesn't behave well when subclassing. 

Chris

On Thu, Feb 17, 2011 at 4:40 AM, Alexander Held <[hidden email]> wrote:
Hi all,

is there already an implementation for a List Trait that keeps the list sorted (with regard to a given key)?
I want the list to be sorted upon assigment and whenever elements of the list are added or changed.
If not, what is the best way, to implement it without having to explicitly add notification handlers?
I imagine to have s.th. like

class Foo(HasTraits):
   sortedList = SortedList(key=lambda x: x.someValue)

Alexander


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev