Salome HOME
*** empty log message ***
[tools/eficas.git] / Noyau / N_utils.py
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.                                 
11 #
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.                            
16 #
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.      
20 #                                                                       
21 #                                                                       
22 # ======================================================================
23
24
25 """
26    Ce module contient des fonctions utilitaires
27 """
28
29 # Modules Python
30 import sys
31
32 # Modules EFICAS
33 from N_Exception import AsException
34 from N_types     import is_int, is_float, is_complex, is_str, is_enum, is_assd
35
36 SEP='_'
37
38 try:
39    # Si la version de Python possède la fonction _getframe
40    # on l'utilise.
41    cur_frame=sys._getframe
42 except:
43    # Sinon on l'émule
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
48      """
49      try:1/0
50      except:
51        frame=sys.exc_info()[2].tb_frame.f_back
52      while offset > 0:
53        if frame == None:return None
54        frame=frame.f_back
55        offset=offset-1
56      return frame
57
58
59 def callee_where(niveau=4):
60    """ 
61       recupere la position de l appel 
62    """
63    frame=cur_frame(niveau)
64    if frame == None: return 0,"inconnu",0,{}
65    try:
66      return frame.f_lineno,frame.f_code.co_filename,frame.f_code.co_firstlineno,frame.f_locals
67    except:
68      return 0,"inconnu",0,{}
69
70
71 def AsType(a):
72    """
73       Retourne le type d'un concept (a) à partir
74       des caractéristiques de l'objet Python
75    """
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")
84
85
86 def prbanner(s):
87    print "*"*(len(s)+10)
88    print "*"*5 + s + "*"*5
89    print "*"*(len(s)+10)
90
91
92 def repr_float(valeur):
93   """ 
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
96       5 caractères
97       NB : valeur est un réel au format Python ou une chaine de caractères représentant un réel
98   """
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)
103   else :
104     if abs(valeur) > 0.01 : return repr(valeur)
105   t=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
109     t=t.replace('e','E')
110     # --> on doit encore vérifier que la mantisse contient bien un '.'
111     if t.find('.')!= -1:
112       return t
113     else:
114       # -->il faut rajouter le point avant le E
115       t=t.replace('E','.E')
116       return t
117   s=''
118   neg = 0
119   if t[0]=='-':
120     s=s+t[0]
121     t=t[1:]
122   cpt = 0
123   if t[0].atof() == 0.:
124     # réel plus petit que 1
125     neg = 1
126     t=t[2:]
127     cpt=1
128     while t[0].atof() == 0. :
129       cpt = cpt+1
130       t=t[1:]
131     s=s+t[0]+'.'
132     for c in t[1:]:
133       s=s+c
134   else:
135     # réel plus grand que 1
136     s=s+t[0]+'.'
137     if t[1:].atof() == 0.:
138       l=t[1:].split('.')
139       cpt = len(l[0])
140     else:
141       r=0
142       pt=0
143       for c in t[1:]:
144         r=r+1
145         if c != '.' :
146           if pt != 1 : cpt = cpt + 1
147           s=s+c
148         else:
149           pt = 1
150           if r+1 == len(t) or t[r+1:].atof() == 0.:break
151   s=s+'E'+neg*'-'+repr(cpt)
152   return s
153