Created
April 8, 2010 20:01
-
-
Save chrisgorgo/360464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on Apr 7, 2010 | |
@author: filo | |
''' | |
from nipype.interfaces.base import traits, isdefined, TraitedSpec | |
from enthought.traits.trait_base import _Undefined | |
class OutputMultiPath(traits.List): | |
def get(self, object, name): | |
value = self.get_value(object, name) | |
if len(value) == 0: | |
return _Undefined() | |
elif len(value)==1: | |
return value[0] | |
else: | |
return value | |
def set(self, object, name, value): | |
self.set_value(object, name,self.validate(object,name,value)) | |
def validate(self, object, name, value): | |
if not isdefined(value): | |
return value | |
newvalue = value | |
if isinstance(value, str): | |
newvalue = [value] | |
return super(OutputMultiPath, self).validate(object, name, newvalue) | |
class InputMultiPath(traits.List): | |
def validate(self, object, name, value): | |
if not isdefined(value): | |
return value | |
newvalue = value | |
if isinstance(value, str): | |
newvalue = [value] | |
return super(InputMultiPath, self).validate(object, name, newvalue) | |
class Test(TraitedSpec): | |
foo = OutputMultiPath(traits.File()) | |
boo = InputMultiPath(traits.File()) | |
koo = traits.List() | |
t = Test() | |
print t.foo | |
t.foo = 'abc' | |
print t.foo | |
t.foo = ['abc'] | |
print t.foo | |
t.foo = 'abc' | |
print t.foo | |
t.foo = ['abc', 'abc'] | |
print t.foo | |
print t.boo | |
t.boo = 'abc' | |
print t.boo | |
t.boo = ['abc'] | |
print t.boo | |
t.boo = 'abc' | |
print t.boo | |
t.boo = ['abc', 'abc'] | |
print t.boo | |
print t.koo | |
print isdefined(t.koo) | |
t.foo = 67 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very nice. this works great including the undefined