1 | """ |
---|
2 | This program checks the underlying type object for int32 data types. |
---|
3 | """ |
---|
4 | |
---|
5 | import platform |
---|
6 | import numpy as num |
---|
7 | |
---|
8 | |
---|
9 | ROWHDR_WIDTH = 15 |
---|
10 | |
---|
11 | NAME_COLWIDTH = 11 |
---|
12 | STR_COLWIDTH = 10 |
---|
13 | KIND_COLWIDTH = 11 |
---|
14 | CHAR_COLWIDTH = 11 |
---|
15 | ID_COLWIDTH = 10 |
---|
16 | |
---|
17 | NUM_BITS = platform.architecture()[0] |
---|
18 | |
---|
19 | def show(a, t): |
---|
20 | element = a.flatten()[0] |
---|
21 | print ('%s | %s | %s | %s | %s | %10x |' |
---|
22 | % (t.rjust(ROWHDR_WIDTH), |
---|
23 | a.dtype.name.center(NAME_COLWIDTH), |
---|
24 | a.dtype.str.center(STR_COLWIDTH), |
---|
25 | a.dtype.kind.center(KIND_COLWIDTH), |
---|
26 | a.dtype.char.center(CHAR_COLWIDTH), |
---|
27 | id(type(element))) |
---|
28 | ) |
---|
29 | |
---|
30 | print ("This table shows 'dtype' attributes for the '32bit integer' types on a " |
---|
31 | "%s platform:" % NUM_BITS) |
---|
32 | |
---|
33 | print '' |
---|
34 | print ('%s | .dtype.name | .dtype.str | .dtype.kind | .dtype.char | id(type) |' |
---|
35 | % ' '.rjust(ROWHDR_WIDTH)) |
---|
36 | print ('%s-+-------------+------------+-------------+-------------+--------------+' |
---|
37 | % '-'.rjust(ROWHDR_WIDTH)) |
---|
38 | |
---|
39 | # check all attributes of 'numpy', ignore that cause exception. |
---|
40 | for t in dir(num): |
---|
41 | try: |
---|
42 | a = eval('num.array([[1,2],[3,4]], dtype=num.%s)' % t) |
---|
43 | except (TypeError, ValueError): |
---|
44 | pass |
---|
45 | else: |
---|
46 | # if a.dtype.name != 'object': |
---|
47 | if a.dtype.name == 'int32': |
---|
48 | show(a, t) |
---|
49 | |
---|
50 | print ('%s-+-------------+------------+-------------+-------------+--------------+' |
---|
51 | % '-'.rjust(ROWHDR_WIDTH)) |
---|