FloatConfigurator.py 4.36 KB
Newer Older
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
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Mar 30, 2015

30
:author: Eric C. Pellegrini
31
32
33
34
35
'''

from MDANSE.Framework.Configurators.IConfigurator import IConfigurator, ConfiguratorError

class FloatConfigurator(IConfigurator):
eric pellegrini's avatar
eric pellegrini committed
36
    '''
37
    This Configurator allows to input a float.
eric pellegrini's avatar
eric pellegrini committed
38
    '''
39
40
41
42
43
    
    type = 'float'
    
    _default = 0
    
44
    def __init__(self, configurable, name, mini=None, maxi=None, choices=None, **kwargs):
45
46
47
        '''
        Initializes the configurator.
        
48
        :param name: the name of the configurator as it will appear in the configuration.
49
        :type name: str
50
        :param mini: the minimum value allowed for the input value. If None, no restriction for the minimum.
51
        :type mini: float or None
52
        :param maxi: the maximum value allowed for the input value. If None, no restriction for the maximum.
53
        :type maxi: float or None
54
        :param choices: the list of floats allowed for the input value. If None, any value will be allowed.
55
56
        :type choices: list of float or None
        '''
57
58

        # The base class constructor.
59
        IConfigurator.__init__(self, configurable, name, **kwargs)
60
61
62
63
64
65
66
        
        self._mini = float(mini) if mini is not None else None

        self._maxi = float(maxi) if maxi is not None else None
        
        self._choices = choices if choices is not None else []

67
    def configure(self, value):
68
69
70
71
72
73
        '''
        Configure an input value.
                
        :param value: the input value
        :type value: float
        '''
74
75
76
77
                        
        try:
            value = float(value)
        except (TypeError,ValueError) as e:
78
            raise ConfiguratorError(e,self)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
            
        if self._choices:
            if not value in self._choices:
                raise ConfiguratorError('the input value is not a valid choice.', self)
                        
        if self._mini is not None:
            if value < self._mini:
                raise ConfiguratorError("the input value is lower than %r." % self._mini, self)

        if self._maxi is not None:
            if value > self._maxi:
                raise ConfiguratorError("the input value is higher than %r." % self._maxi, self)

        self['value'] = value

    @property
    def mini(self):
96
        '''
97
        Returns the minimum value allowed for an input float.
98
        
99
        :return: the minimum value allowed for an input float.
100
101
        :rtype: float or None
        '''
102
103
104
105
106
        
        return self._mini

    @property
    def maxi(self):
107
        '''
108
        Returns the maximum value allowed for an input float.
109
        
110
        :return: the maximum value allowed for an input float.
111
112
        :rtype: float or None
        '''
113
114
115
116
117
        
        return self._maxi

    @property
    def choices(self):
118
        '''
119
        Returns the list of floats allowed for an input float.
120
        
121
        :return: the choices allowed for an input float.
122
123
        :rtype: list of floats or None
        '''
124
125
126
127
        
        return self._choices

    def get_information(self):
128
129
130
131
132
133
        '''
        Returns some informations about this configurator.
        
        :return: the information about this configurator
        :rtype: str
        '''
134
135
        
        return "Value: %r" % self['value']