[SciPy-dev] a suggest patch for the traits package
Trent Mick
trentm at ActiveState.com
Thu Apr 3 17:04:47 CST 2003
This is directed mainly at David Morrill. Hi David, we met at PyCon if you'll
remember.
I have started using your Traits package in some scripts of mine
(I quite like it), and have a few of questions/suggested changes:
1. TraitFunction optionally allows for an "info" attribute on the
validation function to specify a more specific description than "a
legal value". How about using the function's docstring (if it exists)
for "info":
--- traits.py.orig Thu Apr 03 14:17:08 2003
+++ traits.py Thu Apr 03 14:14:03 2003
@@ -904,8 +904,10 @@
def info ( self ):
try:
return self.aFunc.info
except:
+ if self.aFunc.__doc__:
+ return self.aFunc.__doc__
return 'a legal value'
#--------------------------------------------------------------------------------
# 'TraitEnum' class:
End of Patch.
This patch still allows an "info" attribute to override the
docstring if you think that that is valueable to maintain. If not, if
wouldn't break any of my code to drop the 'aFunc.info' option in
favor of using the docstring. :)
2. Trait.do_list() does not currently translate a MethodType object
(i.e. a class method) to a TraitFunction. I think it should:
--- traits.py.orig Thu Apr 03 14:17:08 2003
+++ traits.py Thu Apr 03 14:49:47 2003
@@ -341,9 +341,10 @@
elif typeItem == DictType:
map.update( item )
elif typeItem == ClassType:
other.append( TraitInstance( item ) )
- elif typeItem == FunctionType:
+ elif (typeItem == FunctionType or
+ typeItem == MethodType):
other.append( TraitFunction( item ) )
elif (isinstance( item, TraitHandler ) or
isinstance( item, TraitDelegate ) or
isinstance( item, Trait )):
End of Patch.
What do you think? These two patches allow one to do something like
the following:
from traits import *
class CheechAndChong(HasTraits):
__traits__ = {"badges": None}
def __init__(self):
self.__traits__["badges"] = (self.validate_badges,)
def validate_badges(self, object, name, value):
"nuthin'"
if not value:
return value
raise ValueError("we don't need no stinkin' badges")
c = CheechAndChong()
c.badges = 2
raises this error string:
traits.traits.TraitError: The 'badges' trait of a CheechAndChong instance must be nuthin', but a value of 2 was specified.
3. The mixed tab spacing in the .py files makes it very difficult to
edit these files. Would you consider standardizing on 4 space
indentations? If so I would be happy to do a lot of the leg work to
reindent. (Actually, I think reindent.py in the Python source tree
will do most of that leg work automatically.)
Cheers,
Trent
--
Trent Mick
TrentM at ActiveState.com
More information about the Scipy-dev
mailing list