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