[Biopython-dev] checker_function for the options in
bio.application.abstractCommandline
Brad Chapman
chapmanb at uga.edu
Fri Jul 18 13:51:29 EDT 2003
Hi Bartek;
> I think, there's something not exactly consistent about the
> parameter setting in AbstractCommandline class.
[...]
> I'm not sure, whether the author meant checker_function to
> raise an exception when the parameter is not valid, or he
> just forgot to put the value assignement into another if-then
> statement with the result of checker_function as condition, but
> I would really appreciate if someone made it more clear.
>
> I would rather opt for the if-then statement, because it allows
> me to write simple and self-explanatory checker functions
I can definitely see how this would be useful. Philosophically, I
like the raise an exception approach better (and is actually what I
intended when I wrote the code), but there is really no reason why
we can't support both ways.
What do you think about the attached patch, which clears up the
documentation a bit and adds support for having a 0/1 return value
specifying whether the value is good? I haven't really tested it out
yet (but will before I check it in, promise :-), and just wanted to
make sure it fulfilled what you were looking for.
Thanks much for the comments on the Application commandline stuff.
This is something I worked up until it was decent enough to use, but
haven't had enough time to make it actually be good. If you have any
other patches/comments, they'd be gratefully accepted.
Thanks again.
Brad
-------------- next part --------------
Index: __init__.py
===================================================================
RCS file: /home/repository/biopython/biopython/Bio/Application/__init__.py,v
retrieving revision 1.1
diff -c -r1.1 __init__.py
*** __init__.py 14 Dec 2001 13:04:22 -0000 1.1
--- __init__.py 18 Jul 2003 17:53:02 -0000
***************
*** 89,103 ****
for parameter in self.parameters:
if name in parameter.names:
if value is not None:
! if parameter.checker_function is not None:
! paramater.checker_function(value)
!
parameter.value = value
parameter.is_set = 1
set_option = 1
if set_option == 0:
raise ValueError("Option name %s was not found." % name)
class _AbstractParameter:
"""A class to hold information about a parameter for a commandline.
--- 89,117 ----
for parameter in self.parameters:
if name in parameter.names:
if value is not None:
! self._check_value(value, name, parameter.checker_function)
parameter.value = value
parameter.is_set = 1
set_option = 1
if set_option == 0:
raise ValueError("Option name %s was not found." % name)
+
+ def _check_value(self, value, name, check_function):
+ """Check whether the given value is valid.
+
+ This uses the passed function 'check_function', which can either
+ return a [0, 1] (bad, good) value or raise an error. Either way
+ this function will raise an error if the value is not valid, or
+ finish silently otherwise.
+ """
+ if check_function is not None:
+ is_good = check_function(value)
+ if is_good in [0, 1]: # if we are dealing with a good/bad check
+ if not(is_good):
+ raise ValueError(
+ "Invalid parameter value %r for parameter %s" %
+ (value, name))
class _AbstractParameter:
"""A class to hold information about a parameter for a commandline.
***************
*** 118,124 ****
include 'input', 'output', 'file'
o checker_function -- a reference to a function that will determine
! if a given value is valid for this parameter.
o description -- a description of the option.
--- 132,140 ----
include 'input', 'output', 'file'
o checker_function -- a reference to a function that will determine
! if a given value is valid for this parameter. This function can either
! raise an error when given a bad value, or return a [0, 1] decision on
! whether the value is correct.
o description -- a description of the option.
More information about the Biopython-dev
mailing list