diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2010-11-28 17:00:36 -0800 |
---|---|---|
committer | Mark Wiebe <mwwiebe@gmail.com> | 2010-11-30 09:10:21 -0800 |
commit | af84876fac13ac2e4e44ac0cae599fe9d6e68643 (patch) | |
tree | 516a1dbdb03df1eb4fde2a7e1dd166a974cdb1a3 /numpy/testing | |
parent | 9273a6139e7b797244b5b88fb371059cc7c3ea3a (diff) | |
download | numpy-af84876fac13ac2e4e44ac0cae599fe9d6e68643.tar.gz |
ENH: Remove type number ordering assumptions in CanCastSafely, ScalarKinds, and CanCoerceScalar
Also add print_coercion_tables.py to aid when refactoring
type casting/coercion/promotion.
Diffstat (limited to 'numpy/testing')
-rwxr-xr-x | numpy/testing/print_coercion_tables.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/numpy/testing/print_coercion_tables.py b/numpy/testing/print_coercion_tables.py new file mode 100755 index 000000000..0c8a87d9a --- /dev/null +++ b/numpy/testing/print_coercion_tables.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +"""Prints type-coercion tables for the built-in NumPy types""" + +import numpy as np + +# Generic object that can be added, but doesn't do anything else +class GenericObject: + def __init__(self, v): + self.v = v + + def __add__(self, other): + return self + + def __radd__(self, other): + return self + +def print_cancast_table(ntypes): + print 'X', + for char in ntypes: print char, + print + for row in ntypes: + print row, + for col in ntypes: + print int(np.can_cast(row, col)), + print + +def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray): + print '+', + for char in ntypes: print char, + print + for row in ntypes: + if row == 'O': + rowtype = GenericObject + else: + rowtype = np.obj2sctype(row) + + print row, + for col in ntypes: + if col == 'O': + coltype = GenericObject + else: + coltype = np.obj2sctype(col) + try: + if firstarray: + rowvalue = np.array([rowtype(inputfirstvalue)], dtype=rowtype) + else: + rowvalue = rowtype(inputfirstvalue) + colvalue = coltype(inputsecondvalue) + value = np.add(rowvalue,colvalue) + if isinstance(value, np.ndarray): + char = value.dtype.char + else: + char = np.dtype(type(value)).char + except ValueError: + char = '!' + except OverflowError: + char = '@' + except TypeError: + char = '#' + print char, + print + +print "can cast" +print_cancast_table(np.typecodes['All']) +print +print "In these tables, ValueError is '!', OverflowError is '@', TypeError is '#'" +print +print "scalar + scalar" +print_coercion_table(np.typecodes['All'], 0, 0, False) +print +print "scalar + neg scalar" +print_coercion_table(np.typecodes['All'], 0, -1, False) +print +print "array + scalar" +print_coercion_table(np.typecodes['All'], 0, 0, True) +print +print "array + neg scalar" +print_coercion_table(np.typecodes['All'], 0, -1, True) + |