/usr/share/pyshared/mvpa/misc/param.py is in python-mvpa 0.4.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyMVPA package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##g
"""Parameter representation"""
__docformat__ = 'restructuredtext'
import re
import textwrap
from mvpa.misc.state import CollectableAttribute
if __debug__:
from mvpa.base import debug
_whitespace_re = re.compile('\n\s+|^\s+')
__all__ = [ 'Parameter', 'KernelParameter' ]
class Parameter(CollectableAttribute):
"""This class shall serve as a representation of a parameter.
It might be useful if a little more information than the pure parameter
value is required (or even only useful).
Each parameter must have a value. However additional property can be
passed to the constructor and will be stored in the object.
BIG ASSUMPTION: stored values are not mutable, ie nobody should do
cls.parameter1[:] = ...
or we wouldn't know that it was changed
Here is a list of possible property names:
min - minimum value
max - maximum value
step - increment/decrement stepsize
"""
def __init__(self, default, name=None, doc=None, index=None, **kwargs):
"""Specify a parameter by its default value and optionally an arbitrary
number of additional parameters.
TODO: :Parameters: for Parameter
"""
self.__default = default
CollectableAttribute.__init__(self, name=name, doc=doc, index=index)
self.resetvalue()
self._isset = False
if __debug__:
if kwargs.has_key('val'):
raise ValueError, "'val' property name is illegal."
# XXX probably is too generic...
for k, v in kwargs.iteritems():
self.__setattr__(k, v)
def __str__(self):
res = CollectableAttribute.__str__(self)
# it is enabled but no value is assigned yet
res += '=%s' % (self.value,)
return res
def doc(self, indent=" ", width=70):
"""Docstring for the parameter to be used in lists of parameters
:Returns:
string or list of strings (if indent is None)
"""
paramsdoc = " %s" % self.name
if hasattr(paramsdoc, 'allowedtype'):
paramsdoc += " : %s" % self.allowedtype
paramsdoc = [paramsdoc]
try:
doc = self.__doc__
if not doc.endswith('.'): doc += '.'
try:
doc += " (Default: %s)" % self.default
except:
pass
# Explicitly deal with multiple spaces, for some reason
# replace_whitespace is non-effective
doc = _whitespace_re.sub(' ', doc)
paramsdoc += [' ' + x
for x in textwrap.wrap(doc, width=width-len(indent),
replace_whitespace=True)]
except Exception, e:
pass
if indent is None:
return paramsdoc
else:
return ('\n' + indent).join(paramsdoc)
# XXX should be named reset2default? correspondingly in
# ParameterCollection as well
def resetvalue(self):
"""Reset value to the default"""
#CollectableAttribute.reset(self)
if not self.isDefault:
self._isset = True
self.value = self.__default
def _set(self, val):
if self._value != val:
if __debug__:
debug("COL",
"Parameter: setting %s to %s " % (str(self), val))
if hasattr(self, 'min') and val < self.min:
raise ValueError, \
"Minimal value for parameter %s is %s. Got %s" % \
(self.name, self.min, val)
if hasattr(self, 'max') and val > self.max:
raise ValueError, \
"Maximal value for parameter %s is %s. Got %s" % \
(self.name, self.max, val)
if hasattr(self, 'choices') and (not val in self.choices):
raise ValueError, \
"Valid choices for parameter %s are %s. Got %s" % \
(self.name, self.choices, val)
self._value = val
self._isset = True
elif __debug__:
debug("COL",
"Parameter: not setting %s since value is the same" \
% (str(self)))
@property
def isDefault(self):
"""Returns True if current value is bound to default one"""
return self._value is self.default
@property
def equalDefault(self):
"""Returns True if current value is equal to default one"""
return self._value == self.__default
def setDefault(self, value):
wasdefault = self.isDefault
self.__default = value
if wasdefault:
self.resetvalue()
self._isset = False
# incorrect behavior
#def reset(self):
# """Override reset so we don't clean the flag"""
# pass
default = property(fget=lambda x:x.__default, fset=setDefault)
value = property(fget=lambda x:x._value, fset=_set)
class KernelParameter(Parameter):
"""Just that it is different beast"""
pass
|