Salome HOME
CCAR : mise a jour avec la version 7.3.23 du Noyau superviseur Aster
[tools/eficas.git] / Noyau / N_utils.py
1 #@ MODIF N_utils Noyau  DATE 14/09/2004   AUTEUR MCOURTOI M.COURTOIS 
2 # -*- coding: iso-8859-1 -*-
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
24 """
25    Ce module contient des fonctions utilitaires
26 """
27
28 # Modules Python
29 import sys,types
30 import string
31
32 # Modules EFICAS
33 from N_Exception import AsException
34
35 SEP='_'
36
37 try:
38    # Si la version de Python possède la fonction _getframe
39    # on l'utilise.
40    cur_frame=sys._getframe
41 except:
42    # Sinon on l'émule
43    def cur_frame(offset=0):
44      """ Retourne la frame d execution effective eventuellement en remontant
45          de offset niveaux dans la pile d execution
46          Si il y a moins de offset niveaux retourne None
47      """
48      try:1/0
49      except:
50        frame=sys.exc_info()[2].tb_frame.f_back
51      while offset > 0:
52        if frame == None:return None
53        frame=frame.f_back
54        offset=offset-1
55      return frame
56
57
58 def callee_where(niveau=4):
59    """ 
60       recupere la position de l appel 
61    """
62    frame=cur_frame(niveau)
63    if frame == None: return 0,"inconnu",0,{}
64    try:
65      return frame.f_lineno,frame.f_code.co_filename,frame.f_code.co_firstlineno,frame.f_locals
66    except:
67      return 0,"inconnu",0,{}
68
69 def AsType(a):
70    """
71       Retourne le type d'un concept (a) à partir
72       des caractéristiques de l'objet Python
73    """
74    if type(a) in (types.TupleType,types.ListType):return AsType(a[0])
75    if type(a) == types.InstanceType:return a.__class__
76    if type(a) == types.FloatType:return "R"
77    if type(a) == types.IntType:return "I"
78    if type(a) == types.StringType:return "TXM"
79    if a == None : return None
80    print 'a=',a,type(a)
81    raise AsException("type inconnu")
82
83 def prbanner(s):
84    print "*"*(len(s)+10)
85    print "*"*5 + s + "*"*5
86    print "*"*(len(s)+10)
87
88 def repr_float(valeur):
89   """ 
90       Cette fonction représente le réel valeur comme une chaine de caractères
91       sous forme mantisse exposant si nécessaire cad si le nombre contient plus de
92       5 caractères
93       NB : valeur est un réel au format Python ou une chaine de caractères représentant un réel
94   """
95   if type(valeur) == types.StringType : valeur = eval(valeur)
96   if valeur == 0. : return '0.0'
97   if abs(valeur) > 1. :
98     if abs(valeur) < 10000. : return repr(valeur)
99   else :
100     if abs(valeur) > 0.01 : return repr(valeur)
101   t=repr(valeur)
102   if string.find(t,'e') != -1 or string.find(t,'E') != -1 :
103     # le réel est déjà sous forme mantisse exposant !
104     # --> on remplace e par E
105     t=string.replace(t,'e','E')
106     # --> on doit encore vérifier que la mantisse contient bien un '.'
107     if string.find(t,'.')!= -1:
108       return t
109     else:
110       # -->il faut rajouter le point avant le E
111       t=string.replace(t,'E','.E')
112       return t
113   s=''
114   neg = 0
115   if t[0]=='-':
116     s=s+t[0]
117     t=t[1:]
118   cpt = 0
119   if string.atof(t[0]) == 0.:
120     # réel plus petit que 1
121     neg = 1
122     t=t[2:]
123     cpt=1
124     while string.atof(t[0]) == 0. :
125       cpt = cpt+1
126       t=t[1:]
127     s=s+t[0]+'.'
128     for c in t[1:]:
129       s=s+c
130   else:
131     # réel plus grand que 1
132     s=s+t[0]+'.'
133     if string.atof(t[1:]) == 0.:
134       l=string.split(t[1:],'.')
135       cpt = len(l[0])
136     else:
137       r=0
138       pt=0
139       for c in t[1:]:
140         r=r+1
141         if c != '.' :
142           if pt != 1 : cpt = cpt + 1
143           s=s+c
144         else:
145           pt = 1
146           if r+1 == len(t) or string.atof(t[r+1:]) == 0.:break
147   s=s+'E'+neg*'-'+repr(cpt)
148   return s
149