traits Either get/set

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

traits Either get/set

Satrajit Ghosh

we are trying to implement a derivative class of Either with a special handling for accession.

class MultiPath(Either):
    def __init__(self, trait  = None, **metadata):
        super(MultiPath, self).__init__(List(trait), trait, **metadata)

we would like to override some 'get' function with the following accession logic:

    def get_value(self, object, name, trait=None):
        value = super(MultiPath, self).get_value(object, name, trait=trait)
        if isdefined(value):
           # return contained element if length of stored list is 1
            if isinstance(value, list) and len(value) == 1:
                return value[0]
        return value

we could do this with a get/set function when the trait type was a List. is there a way to do this with Either?

our typical usage scenario for the above is:

class A(HasTraits):
    foo = MultiPath(File(exists=True))

a = A()  = '/path/to/existingfile.ext'
or = glob('/path/to/existingfile*.ext')

if len( == 1 it would return a string instead of a list.



Enthought-Dev mailing list
[hidden email]