2 # Copyright (C) 2007-2017 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 Ce module contient des fonctions utilitaires
26 from __future__ import absolute_import
27 from __future__ import print_function
28 from __future__ import division
30 from builtins import str
31 from builtins import object
37 from .N_Exception import AsException
38 from .N_types import isInt, isFloat, isComplex, isStr, isSequence, isASSD
39 from .strfunc import getEncoding
45 cur_frame = sys._getframe
47 print ('pb avec la version de python pour cur_frame = sys._getframe')
49 def calleeWhere(niveau=4):
51 recupere la position de l appel
53 frame = sys._getframe(niveau)
55 return 0, "inconnu", 0, {}
57 # Python 2.7 compile function does not accept unicode filename, so we encode it
58 # with the current locale encoding in order to have a correct traceback.
59 # Here, we convert it back to unicode.
60 filename = six.text_type(frame.f_code.co_filename, getEncoding())
61 return frame.fLineNo, filename, frame.f_code.co_firstlineno, frame.f_locals
63 return 0, "inconnu", 0, {}
68 Retourne le type d'un concept (a) à partir
69 des caractéristiques de l'objet Python
83 raise AsException("type inconnu: %r %s" % (a, type(a)))
87 print(("*" * (len(s) + 10)))
88 print(("*" * 5 + s + "*" * 5))
89 print(("*" * (len(s) + 10)))
92 def repr_float(valeur):
94 Cette fonction représente le réel valeur comme une chaine de caractères
95 sous forme mantisse exposant si nécessaire cad si le nombre contient plus de
97 NB : valeur est un réel au format Python ou une chaine de caractères représentant un réel
99 if type(valeur) == str:
100 valeur = eval(valeur)
104 if abs(valeur) < 10000.:
107 if abs(valeur) > 0.01:
110 if t.find('e') != -1 or t.find('E') != -1:
111 # le réel est déjà sous forme mantisse exposant !
112 # --> on remplace e par E
113 t = t.replace('e', 'E')
114 # --> on doit encore vérifier que la mantisse contient bien un '.'
115 if t.find('.') != -1:
118 # -->il faut rajouter le point avant le E
119 t = t.replace('E', '.E')
127 if t[0].atof() == 0.:
128 # réel plus petit que 1
132 while t[0].atof() == 0.:
139 # réel plus grand que 1
141 if t[1:].atof() == 0.:
155 if r + 1 == len(t) or t[r + 1:].atof() == 0.:
157 s = s + 'E' + neg * '-' + repr(cpt)
161 def importObject(uri):
162 """Load and return a python object (class, function...).
163 Its `uri` looks like "mainpkg.subpkg.module.object", this means
164 that "mainpkg.subpkg.module" is imported and "object" is
165 the object to return.
167 path = uri.split('.')
168 modname = '.'.join(path[:-1])
169 if len(modname) == 0:
170 raise ImportError(u"invalid uri: %s" % uri)
175 mod = sys.modules[modname]
176 except ImportError as err:
178 "can not import module : %s (%s)" % (modname, str(err)))
180 object = getattr(mod, objname)
181 except AttributeError as err:
182 raise AttributeError("object (%s) not found in module '%s'. "
183 "Module content is: %s" % (objname, modname, tuple(dir(mod))))
187 class Singleton(object):
189 """Singleton implementation in python."""
190 # add _singleton_id attribute to the class to be independant of import
194 def __new__(cls, *args, **kargs):
195 cls_id = getattr(cls, '_singleton_id', cls)
196 if Singleton.__inst.get(cls_id) is None:
197 Singleton.__inst[cls_id] = object.__new__(cls)
198 return Singleton.__inst[cls_id]
204 This class emulates a C-like enum for python. It is initialized with a list
205 of strings to be used as the enum symbolic keys. The enum values are automatically
206 generated as sequencing integer starting at 0.
209 def __init__(self, *keys):
212 for inum, key in enumerate(keys):
213 setattr(self, key, 2 ** inum)
214 self._dict_keys[2 ** inum] = key
216 def exists(self, value):
217 """Tell if value is in the enumeration"""
218 return self.getId(value) is not None
220 def getId(self, value):
221 """Return the key associated to the given value"""
222 return self._dict_keys.get(value, None)