Salome HOME
sauve du 20/12
[tools/eficas.git] / Noyau / N_types.py
1 # coding=utf-8
2 # Copyright (C) 2007-2013   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 """
21    Ce module contient des fonctions utilitaires pour tester les types
22 """
23
24 # eficas sentinel
25 try:
26     import numpy as NP
27     _np_arr = NP.ndarray
28 except ImportError:
29     _np_arr = None
30
31 # use isinstance() instead of type() because objects returned from numpy arrays
32 # inherit from python scalars but are numpy.float64 or numpy.int32...
33
34
35 def is_int(obj):
36     return isinstance(obj, int) or type(obj) is long
37
38
39 def is_float(obj):
40     return isinstance(obj, float)
41
42
43 def is_complex(obj):
44     return isinstance(obj, complex)
45
46 from decimal import Decimal
47
48
49 def is_float_or_int(obj):
50     return is_float(obj) or is_int(obj) or isinstance(obj, Decimal)
51
52
53 def is_number(obj):
54     return is_float_or_int(obj) or is_complex(obj)
55
56
57 def is_str(obj):
58     return isinstance(obj, (str, unicode))
59
60
61 def is_list(obj):
62     return type(obj) is list
63
64
65 def is_tuple(obj):
66     return type(obj) is tuple
67
68
69 def is_array(obj):
70     """a numpy array ?"""
71     return type(obj) is _np_arr
72
73
74 def is_sequence(obj):
75     """a sequence (allow iteration, not a string) ?"""
76     return is_list(obj) or is_tuple(obj) or is_array(obj)
77
78
79 def is_assd(obj):
80     from N_ASSD import ASSD
81     return isinstance(obj, ASSD)
82
83
84 def force_list(obj):
85     """Retourne `obj` si c'est une liste ou un tuple,
86     sinon retourne [obj,] (en tant que list).
87     """
88     if not is_sequence(obj):
89         obj = [obj, ]
90     return list(obj)
91
92
93 def force_tuple(obj):
94     """Return `obj` as a tuple."""
95     return tuple(force_list(obj))
96
97 # backward compatibility
98 from warnings import warn
99
100
101 def is_enum(obj):
102     """same as is_sequence"""
103     warn("'is_enum' is deprecated, use 'is_sequence'",
104          DeprecationWarning, stacklevel=2)
105     return is_sequence(obj)