Salome HOME
modif pour MT
[tools/eficas.git] / Noyau / N_types.py
1 # coding=utf-8
2 # Copyright (C) 2007-2017   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 from __future__ import absolute_import
26 import six
27 try:
28     import numpy as NP
29     _np_arr = NP.ndarray
30 except ImportError:
31     _np_arr = None
32
33 # use isinstance() instead of type() because objects returned from numpy arrays
34 # inherit from python scalars but are numpy.float64 or numpy.int32...
35
36
37 def isInt(obj):
38     return isinstance(obj, int) or type(obj) is int
39
40
41 def isFloat(obj):
42     return isinstance(obj, float)
43
44
45 def isComplex(obj):
46     return isinstance(obj, complex)
47
48 from decimal import Decimal
49
50
51 def isFloat_or_int(obj):
52     return isFloat(obj) or isInt(obj) or isinstance(obj, Decimal)
53
54
55 def isNumber(obj):
56     return isFloat_or_int(obj) or isComplex(obj)
57
58
59 def isStr(obj):
60     return isinstance(obj, (str, six.text_type))
61
62
63 def isList(obj):
64     return type(obj) is list
65
66
67 def isTuple(obj):
68     return type(obj) is tuple
69
70
71 def isArray(obj):
72     """a numpy array ?"""
73     return type(obj) is _np_arr
74
75
76 def isSequence(obj):
77     """a sequence (allow iteration, not a string) ?"""
78     return isList(obj) or isTuple(obj) or isArray(obj)
79
80
81 def isASSD(obj):
82     from .N_ASSD import ASSD
83     return isinstance(obj, ASSD)
84
85
86 def forceList(obj):
87     """Retourne `obj` si c'est une liste ou un tuple,
88     sinon retourne [obj,] (en tant que list).
89     """
90     if not isSequence(obj):
91         obj = [obj, ]
92     return list(obj)
93
94
95 def forceTuple(obj):
96     """Return `obj` as a tuple."""
97     return tuple(forceList(obj))
98
99 # backward compatibility
100 from warnings import warn
101
102
103 def isEnum(obj):
104     """same as isSequence"""
105     warn("'isEnum' is deprecated, use 'isSequence'",
106          DeprecationWarning, stacklevel=2)
107     return isSequence(obj)