[SciPy-dev] genetic algorithm, number theory, filter design,zerofinding

pearu at scipy.org pearu at scipy.org
Wed Apr 10 14:33:15 CDT 2002


On Wed, 10 Apr 2002, Chuck Harris wrote:

> I've decided to start with the zero finders, as fsolve is in general a
> poor choice in one dimension. 

Great.

> This brings up the problem of validating
> function arguments and I am searching for guidelines.
> 1: if an argument is a scalar function, should it be checked for
> return type and argument types and number.

I am not sure anymore if this is needed. It probably would require
evaluation of the function and it can be expensive. Though one can 
check if function accepts proper number of arguments using its
func_* attributs. But be warned that it can be tricky.
Let's leave it to the users, I suggest.

> 2: should all other arguments be checked, and either be explicitly
> cast or an error raised if they are of the wrong type.

Always apply asarray if applicable.

> 3: do integers need to be checked for range? I believe python 2.2 no
> longer distinquishes between int and long integers.

No. Unless there are some specific restrictions to integer arguments.

> My functions are pure python, so most of these issues are taken care
> of by the runtime checks --- oh wonderful python, destroyer of
> niggling detail --- but what about C/C++ code?

It depends. C/C++ code should not crash Python and therefore it should
check all arguments for consistency. If it is inconvinient then one can
write clever Python interfaces to C/C++ codes that take care of checks.

Note that f2py provides array_from_pyobj (see
f2py2e/src/fortranobject.c) that accepts all kinds of Python objects
(lists,tuples,numbers) and returns a proper Numeric array with requested
dimensions. This function has been heavily tested and is very robust to
its arguments. If extension writers would like to use it, we could ship it
into scipy_base. What do you think?

> Also, is there any way of checking execution time, do we really care?

See how it is done in linalg/tests/test_basic.py,
look for bench_* methods and how they use ScipyTestCase.measure method.
And yes, we do care and it can be very useful.

Pearu




More information about the Scipy-dev mailing list