Skip to content

Instantly share code, notes, and snippets.

@chrisgorgo
Created April 8, 2010 20:01
Show Gist options
  • Save chrisgorgo/360464 to your computer and use it in GitHub Desktop.
Save chrisgorgo/360464 to your computer and use it in GitHub Desktop.
'''
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
@satra
Copy link

satra commented Apr 9, 2010

very nice. this works great including the undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment