scitools.errorcheck

Functions for checking compatibility of data structures, right type of data etc.

Example: given a program “check.py”:

from numpy import linspace
q1 = linspace(0, 1, 5)
try:
    right_length(q1, 2)
except Exception, e:
    print e.__class__.__name__, e
try:
    right_size1(q1, (2,2))
except Exception, e:
    print e.__class__.__name__, e
try:    
    q2 = linspace(0, 1, 6)
except Exception, e:
    print e.__class__.__name__, e
try:
    right_size2(q1, q2)
except Exception, e:
    print e.__class__.__name__, e
try:
    right_type(q2, list)
except Exception, e:
    print e.__class__.__name__, e
try:
    wrong_type(q1)
except Exception, e:
    print e.__class__.__name__, e

Here is the output (each call triggers the exception):

ValueError file "check.py", line 5, main program
q1 has length 5, which is not compatible with assumed length 2
ValueError file "check.py", line 9, main program
q1 has size (5,), which is not compatible with assumed size (2, 2)
ValueError file "check.py", line 17, main program
q1 has size (5,), which is not compatible with size (6,) of q2
TypeError file "check.py", line 21, main program
q2 is of type ndarray, while we expected <type 'list'>
TypeError q1 is of wrong type (ndarray).
scitools.errorcheck.right_length(a, length)[source]

Check that len(a) == length.

a any variable for which len(a) is meaningful
length the expected length of a (integer).
scitools.errorcheck.right_size1(a, shape)[source]

Check that a has correct shape.

a NumPy array
shape the expected shape of a (int or tuple)
scitools.errorcheck.right_size2(a1, a2)[source]

Check that a1 and a2 have equal shapes. a1 and a2 are NumPy arrays.

scitools.errorcheck.get_type(a)[source]

Extract a’s type. First, try to extract a’s class name. If this is unsuccessful, return type(a).

The session below illustrates differences between type(a) nad get_type(a) for standard classes, instances of user-defined classes, and class objects (new style and classic classes).

>>> # standard built-in types:
>>> c = [1,2,3]
>>> d = 'my string'
>>> type(c)
<type 'list'>
>>> get_type(c)
'list'
>>> type(d)
<type 'str'>
>>> get_type(d)
'str'
>>> 
>>> # user-defined classes and instances:
>>> class A:          # classic class
...     pass
... 
>>> class B(object):  # new style class
...     pass
... 
>>> a = A()
>>> type(a)
<type 'instance'>
>>> get_type(a)
'A'
>>> 
>>> b = B()
>>> type(b)
<class '__main__.B'>
>>> get_type(b)
'B'
>>>
>>> # class objects A and B:
>>> type(A)
<type 'classobj'>
>>> get_type(A)
'classobj (i.e. a class object)'
>>> type(B)
<type 'type'>
>>> get_type(B)
'type (i.e. a class object)'
>>> 
scitools.errorcheck.right_type(a, expected_types, raise_exception=True)[source]

Check that variable a is of the type(s) specified by expected_types, which is a list/tuple of built-in types (class names), user-defined class names, or one of the functions callable or numpy.iterable.

return: True if the type is right. Otherwise, raise a TypeError exception if raise_exception is True. If this value is False, return False and place a message string explaining what is wrong in the module variable errorcheck.message.

scitools.errorcheck.wrong_type(a, comment='')[source]

Raise a TypeError exception expressing the type of variable a and that this type is wrong.

a is any variable of any type, and comment is a text that can be appended to the exception string.

Previous topic

scitools.debug

Next topic

scitools.filetable

This Page