Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[tools/eficas.git] / Noyau / N_utils.py
1 #@ MODIF N_utils Noyau  DATE 27/03/2002   AUTEUR DURAND C.DURAND 
2 #            CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
5 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
6 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
7 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR   
8 # (AT YOUR OPTION) ANY LATER VERSION.                                 
9 #
10 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT 
11 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF          
12 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU    
13 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.                            
14 #
15 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE   
16 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,       
17 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.      
18 #                                                                       
19 #                                                                       
20 # ======================================================================
21 """
22    Ce module contient des fonctions utilitaires
23 """
24
25 # Modules Python
26 import sys,types
27 import string
28
29 # Modules EFICAS
30 from N_Exception import AsException
31
32 SEP='_'
33
34 try:
35    # Si la version de Python possède la fonction _getframe
36    # on l'utilise.
37    cur_frame=sys._getframe
38 except:
39    # Sinon on l'émule
40    def cur_frame(offset=0):
41      """ Retourne la frame d execution effective eventuellement en remontant
42          de offset niveaux dans la pile d execution
43          Si il y a moins de offset niveaux retourne None
44      """
45      try:1/0
46      except:
47        frame=sys.exc_info()[2].tb_frame.f_back
48      while offset > 0:
49        if frame == None:return None
50        frame=frame.f_back
51        offset=offset-1
52      return frame
53
54
55 def callee_where(niveau=4):
56    """ 
57       recupere la position de l appel 
58    """
59    frame=cur_frame(niveau)
60    if frame == None: return 0,"inconnu",0,{}
61    try:
62      return frame.f_lineno,frame.f_code.co_filename,frame.f_code.co_firstlineno,frame.f_locals
63    except:
64      return 0,"inconnu",0,{}
65
66 def AsType(a):
67    """
68       Retourne le type d'un concept (a) à partir
69       des caractéristiques de l'objet Python
70    """
71    if type(a) in (types.TupleType,types.ListType):return AsType(a[0])
72    if type(a) == types.InstanceType:return a.__class__
73    if type(a) == types.FloatType:return "R"
74    if type(a) == types.IntType:return "I"
75    if type(a) == types.StringType:return "TXM"
76    if a == None : return None
77    print 'a=',a,type(a)
78    raise AsException("type inconnu")
79
80 def prbanner(s):
81    print "*"*(len(s)+10)
82    print "*"*5 + s + "*"*5
83    print "*"*(len(s)+10)
84
85 def repr_float(valeur):
86   """ 
87       Cette fonction représente le réel valeur comme une chaine de caractères
88       sous forme mantisse exposant si nécessaire cad si le nombre contient plus de
89       5 caractères
90       NB : valeur est un réel au format Python ou une chaine de caractères représentant un réel
91   """
92   if type(valeur) == types.StringType : valeur = eval(valeur)
93   if valeur == 0. : return '0.0'
94   if abs(valeur) > 1. :
95     if abs(valeur) < 10000. : return repr(valeur)
96   else :
97     if abs(valeur) > 0.01 : return repr(valeur)
98   t=repr(valeur)
99   if string.find(t,'e') != -1 or string.find(t,'E') != -1 :
100     # le réel est déjà sous forme mantisse exposant !
101     # --> on remplace e par E
102     t=string.replace(t,'e','E')
103     # --> on doit encore vérifier que la mantisse contient bien un '.'
104     if string.find(t,'.')!= -1:
105       return t
106     else:
107       # -->il faut rajouter le point avant le E
108       t=string.replace(t,'E','.E')
109       return t
110   s=''
111   neg = 0
112   if t[0]=='-':
113     s=s+t[0]
114     t=t[1:]
115   cpt = 0
116   if string.atof(t[0]) == 0.:
117     # réel plus petit que 1
118     neg = 1
119     t=t[2:]
120     cpt=1
121     while string.atof(t[0]) == 0. :
122       cpt = cpt+1
123       t=t[1:]
124     s=s+t[0]+'.'
125     for c in t[1:]:
126       s=s+c
127   else:
128     # réel plus grand que 1
129     s=s+t[0]+'.'
130     if string.atof(t[1:]) == 0.:
131       l=string.split(t[1:],'.')
132       cpt = len(l[0])
133     else:
134       r=0
135       pt=0
136       for c in t[1:]:
137         r=r+1
138         if c != '.' :
139           if pt != 1 : cpt = cpt + 1
140           s=s+c
141         else:
142           pt = 1
143           if r+1 == len(t) or string.atof(t[r+1:]) == 0.:break
144   s=s+'E'+neg*'-'+repr(cpt)
145   return s
146