Chris@87
|
1 #!/usr/bin/env python
|
Chris@87
|
2 """Prints type-coercion tables for the built-in NumPy types
|
Chris@87
|
3
|
Chris@87
|
4 """
|
Chris@87
|
5 from __future__ import division, absolute_import, print_function
|
Chris@87
|
6
|
Chris@87
|
7 import numpy as np
|
Chris@87
|
8
|
Chris@87
|
9 # Generic object that can be added, but doesn't do anything else
|
Chris@87
|
10 class GenericObject(object):
|
Chris@87
|
11 def __init__(self, v):
|
Chris@87
|
12 self.v = v
|
Chris@87
|
13
|
Chris@87
|
14 def __add__(self, other):
|
Chris@87
|
15 return self
|
Chris@87
|
16
|
Chris@87
|
17 def __radd__(self, other):
|
Chris@87
|
18 return self
|
Chris@87
|
19
|
Chris@87
|
20 dtype = np.dtype('O')
|
Chris@87
|
21
|
Chris@87
|
22 def print_cancast_table(ntypes):
|
Chris@87
|
23 print('X', end=' ')
|
Chris@87
|
24 for char in ntypes: print(char, end=' ')
|
Chris@87
|
25 print()
|
Chris@87
|
26 for row in ntypes:
|
Chris@87
|
27 print(row, end=' ')
|
Chris@87
|
28 for col in ntypes:
|
Chris@87
|
29 print(int(np.can_cast(row, col)), end=' ')
|
Chris@87
|
30 print()
|
Chris@87
|
31
|
Chris@87
|
32 def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray, use_promote_types=False):
|
Chris@87
|
33 print('+', end=' ')
|
Chris@87
|
34 for char in ntypes: print(char, end=' ')
|
Chris@87
|
35 print()
|
Chris@87
|
36 for row in ntypes:
|
Chris@87
|
37 if row == 'O':
|
Chris@87
|
38 rowtype = GenericObject
|
Chris@87
|
39 else:
|
Chris@87
|
40 rowtype = np.obj2sctype(row)
|
Chris@87
|
41
|
Chris@87
|
42 print(row, end=' ')
|
Chris@87
|
43 for col in ntypes:
|
Chris@87
|
44 if col == 'O':
|
Chris@87
|
45 coltype = GenericObject
|
Chris@87
|
46 else:
|
Chris@87
|
47 coltype = np.obj2sctype(col)
|
Chris@87
|
48 try:
|
Chris@87
|
49 if firstarray:
|
Chris@87
|
50 rowvalue = np.array([rowtype(inputfirstvalue)], dtype=rowtype)
|
Chris@87
|
51 else:
|
Chris@87
|
52 rowvalue = rowtype(inputfirstvalue)
|
Chris@87
|
53 colvalue = coltype(inputsecondvalue)
|
Chris@87
|
54 if use_promote_types:
|
Chris@87
|
55 char = np.promote_types(rowvalue.dtype, colvalue.dtype).char
|
Chris@87
|
56 else:
|
Chris@87
|
57 value = np.add(rowvalue, colvalue)
|
Chris@87
|
58 if isinstance(value, np.ndarray):
|
Chris@87
|
59 char = value.dtype.char
|
Chris@87
|
60 else:
|
Chris@87
|
61 char = np.dtype(type(value)).char
|
Chris@87
|
62 except ValueError:
|
Chris@87
|
63 char = '!'
|
Chris@87
|
64 except OverflowError:
|
Chris@87
|
65 char = '@'
|
Chris@87
|
66 except TypeError:
|
Chris@87
|
67 char = '#'
|
Chris@87
|
68 print(char, end=' ')
|
Chris@87
|
69 print()
|
Chris@87
|
70
|
Chris@87
|
71 print("can cast")
|
Chris@87
|
72 print_cancast_table(np.typecodes['All'])
|
Chris@87
|
73 print()
|
Chris@87
|
74 print("In these tables, ValueError is '!', OverflowError is '@', TypeError is '#'")
|
Chris@87
|
75 print()
|
Chris@87
|
76 print("scalar + scalar")
|
Chris@87
|
77 print_coercion_table(np.typecodes['All'], 0, 0, False)
|
Chris@87
|
78 print()
|
Chris@87
|
79 print("scalar + neg scalar")
|
Chris@87
|
80 print_coercion_table(np.typecodes['All'], 0, -1, False)
|
Chris@87
|
81 print()
|
Chris@87
|
82 print("array + scalar")
|
Chris@87
|
83 print_coercion_table(np.typecodes['All'], 0, 0, True)
|
Chris@87
|
84 print()
|
Chris@87
|
85 print("array + neg scalar")
|
Chris@87
|
86 print_coercion_table(np.typecodes['All'], 0, -1, True)
|
Chris@87
|
87 print()
|
Chris@87
|
88 print("promote_types")
|
Chris@87
|
89 print_coercion_table(np.typecodes['All'], 0, 0, False, True)
|