File: Evaluation.py

package info (click to toggle)
mobyle 1.5.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,288 kB
  • sloc: python: 22,709; makefile: 35; sh: 33; ansic: 10; xml: 6
file content (128 lines) | stat: -rw-r--r-- 4,275 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
########################################################################################
#                                                                                      #
#   Author: Bertrand Neron,                                                            #
#   Organization:'Biological Software and Databases' Group, Institut Pasteur, Paris.   #  
#   Distributed under GPLv2 Licence. Please refer to the COPYING.LIB document.         #
#                                                                                      #
########################################################################################

import imp
from logging import getLogger
e_log = getLogger( __name__ )

from Mobyle.MobyleError import MobyleError , EvaluationError


__extra_epydoc_fields__ = [('call', 'Called by','Called by')]




class Evaluation:
    """
    The Evaluation Class stock the parameter values and permit to evaluate expression (Ctrl precond format ...) in a protected environment avoidig names collision
    """
    def __init__(self , dict= None ):
        try:
            fp , pathname , description = imp.find_module("re")
            self.re = imp.load_module( "re" , fp , pathname , description )
            
        finally:
            if fp:
                fp.close()
        if dict:
            for key in dict.keys():
                setattr(self , key , dict[key])

        
    def isFill( self ):
        """
        @return: returns True if the Evaluation is already filled, if there isn't any user value in evalution return False.
        @rtype: boolean
        """
        
        for var in self.__dict__.keys():
            if var == "re" or var == "__builtins__":
                continue
            else:
                return True
        return False


    def _2dict_( self ):
        """
        @return: Returns a dictionary containing the attributes creates by L{setVar}
        @rtype: dictionary
        """
        dict = {}
        
        for param in self.__dict__.keys():
            if param == "__builtins__" or param == "vdef" or param == "value" or param == "re" :
                pass
            else:
                dict[ param ] = self.__dict__[ param ]
                
        return dict

    def setVar(self, varName , value):
        """
        Creates an attribute varName with the value value
             
        @param varName: the name of the attribute to create
        @type varName: String
        @param value: the value of the attribute varName
        @type value: any
      
        """
        setattr(self , varName , value)
  

    def getVar(self, varName):
        """
        @param varName: the name of the parameter 
        @type varName: String
        @return: the value of the varName attribute or None if there is no attribute varName  
        """
        try:
            return getattr(self, varName)
        except AttributeError:
            return None
   
          
    def eval(self, expr):
        """
        eval the expr in self namespace. be careful to specify the value and vdef before calling eval.
        @param expr: the expression to evalute
        @type expr: String
        @return: the expression evaluated in the Evalution namespace
        @todo: evaluer le temps passe a importe re a chaque fois, le faire que si re dans la string a evalue?
        """
        try:
            myEval = eval( expr , vars(self) )
            return myEval
        
        except Exception , err:
            raise EvaluationError , err
                  


    def isDefined(self, varName):
        """
        we decided to fill the evaluation instance with all the parameter of the service.
        if the user provide a value or there is a vdef for the parameter the value is affected
        to the parameter.
        else the parameter will be affected at None
        @return: True if the varName has been defined (not None), False Otherwise
        """
        try:
            if getattr(self, varName) == None:
                return False
            else:
                return True
        except NameError:
            msg = "the variable: "+str(varName)+" is unknown"
            e_log.error(msg)
            raise MobyleError , msg