1 #@ MODIF N_utils Noyau DATE 11/05/2010 AUTEUR COURTOIS M.COURTOIS
2 # -*- coding: iso-8859-1 -*-
3 # RESPONSABLE COURTOIS M.COURTOIS
4 # CONFIGURATION MANAGEMENT OF EDF VERSION
5 # ======================================================================
6 # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
7 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
8 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
9 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
10 # (AT YOUR OPTION) ANY LATER VERSION.
12 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
13 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
14 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
15 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
17 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
18 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
19 # 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
22 # ======================================================================
26 Ce module contient des fonctions utilitaires
33 from N_Exception import AsException
34 from N_types import is_int, is_float, is_complex, is_str, is_enum, is_assd
39 # Si la version de Python possède la fonction _getframe
41 cur_frame=sys._getframe
44 def cur_frame(offset=0):
45 """ Retourne la frame d execution effective eventuellement en remontant
46 de offset niveaux dans la pile d execution
47 Si il y a moins de offset niveaux retourne None
51 frame=sys.exc_info()[2].tb_frame.f_back
53 if frame == None:return None
59 def callee_where(niveau=4):
61 recupere la position de l appel
63 frame=cur_frame(niveau)
64 if frame == None: return 0,"inconnu",0,{}
66 return frame.f_lineno,frame.f_code.co_filename,frame.f_code.co_firstlineno,frame.f_locals
68 return 0,"inconnu",0,{}
73 Retourne le type d'un concept (a) à partir
74 des caractéristiques de l'objet Python
76 if is_enum(a): return AsType(a[0])
77 if is_assd(a): return type(a)
78 if is_float(a): return "R"
79 if is_int(a): return "I"
80 if is_str(a): return "TXM"
81 if a == None: return None
82 print 'a=', a, type(a)
83 raise AsException("type inconnu")
88 print "*"*5 + s + "*"*5
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 : valeur = eval(valeur)
100 if valeur == 0. : return '0.0'
101 if abs(valeur) > 1. :
102 if abs(valeur) < 10000. : return repr(valeur)
104 if abs(valeur) > 0.01 : return repr(valeur)
106 if t.find('e') != -1 or t.find('E') != -1 :
107 # le réel est déjà sous forme mantisse exposant !
108 # --> on remplace e par E
110 # --> on doit encore vérifier que la mantisse contient bien un '.'
114 # -->il faut rajouter le point avant le E
115 t=t.replace('E','.E')
123 if t[0].atof() == 0.:
124 # réel plus petit que 1
128 while t[0].atof() == 0. :
135 # réel plus grand que 1
137 if t[1:].atof() == 0.:
146 if pt != 1 : cpt = cpt + 1
150 if r+1 == len(t) or t[r+1:].atof() == 0.:break
151 s=s+'E'+neg*'-'+repr(cpt)