/usr/share/pyshared/brian/tools/parameters.py is in python-brian 1.3.1-1build1.
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | # ----------------------------------------------------------------------------------
# Copyright ENS, INRIA, CNRS
# Contributors: Romain Brette (brette@di.ens.fr) and Dan Goodman (goodman@di.ens.fr)
#
# Brian is a computer program whose purpose is to simulate models
# of biological neural networks.
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
# ----------------------------------------------------------------------------------
#
"""
Classes and functions for storing and using parameters
"""
__all__ = ['attribdict', 'Parameters']
from itertools import chain
from inspect import *
class attribdict(dict):
"""
Dictionary that can be accessed via keys
Note that attributes starting with _ won't be added to the dict but
will instead be directly added to the object.
Note that an attribdict cannot have new real attributes
added to it unless they start with _, because they will
be added to the dict instead. To add a real attribute,
use object.__setattr__(obj,name,val).
"""
def __init__(self, **kwds):
super(attribdict, self).__init__(**kwds)
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError
def __setattr__(self, name, val):
if name in dir(self) or (len(name) and name[0] == '_'):
dict.__setattr__(self, name, val)
return
self[name] = val
def __repr__(self):
s = 'Attributes:'
for k, v in self.iteritems():
s += '\n ' + k + ' = ' + str(v)
return s
class Parameters(attribdict):
"""
A storage class for keeping track of parameters
Example usage::
p = Parameters(
a = 5,
b = 6,
computed_parameters = '''
c = a + b
''')
print p.c
p.a = 1
print p.c
The first ``print`` statement will give 11, the second gives 7.
Details:
Call as::
p = Parameters(...)
Where the ``...`` consists of a list of keyword / value pairs (like a ``dict``).
Keywords must not start with the underscore ``_`` character. Any
keyword that starts with ``computed_`` should be a string of valid Python statements
that compute new values based on the given ones. Whenever a non-computed value is
changed, the computed parameters are recomputed, in alphabetical order of their
keyword names (so ``computed_a`` is computed before ``computed_b`` for example).
Non-computed values can be accessed and set via ``p.x``, ``p.x=1`` for example, whereas
computed values can only be accessed and not set. New parameters can be added
after the :class:`Parameters` object is created, including new ``computed_*`` parameters. You
can 'derive' a new parameters object from a given one as follows::
p1 = Parameters(x=1)
p2 = Parameters(y=2,**p1)
print p2.x
Note that changing the value of ``x`` in ``p2`` will not change the value of ``x`` in ``p1`` (this
is a copy operation).
"""
def __init__(self, **kwds):
super(Parameters, self).__init__(**kwds)
self._recompute()
def __getattr__(self, name):
try:
return self[name]
except KeyError:
try:
return self._computed_values[name]
except KeyError:
raise AttributeError
def __setattr__(self, name, val):
if hasattr(self, '_computed_values') and name in self._computed_values:
raise AttributeError('Cannot set computed value')
attribdict.__setattr__(self, name, val)
self._recompute()
def _recompute(self):
cv = dict(self)
items = self.items()
items.sort()
for k, v in items:
if k[:9] == 'computed_':
v = '\n'.join([line.strip() for line in v.split('\n')])
exec v in cv
for k in cv.keys():
if k[:1] == '_': # this is used to get rid of things like __builtins__ etc.
cv.pop(k)
for k in self.iterkeys():
cv.pop(k)
object.__setattr__(self, '_computed_values', cv)
def ascode(self, name):
"""
Returns a string which can be executed which gives all the parameters
name is the name of the Parameters variable in the local namespace:
Usage:
P = Parameters(x=1)
exec P.ascode('P')
print x
"""
s = ''
allvals = dict(**self)
allvals.update(self._computed_values)
for k in allvals.iterkeys():
if k[:9] != 'computed_':
s += k + '=' + name + '.' + k + '\n'
return s
def get_vars(self, *vars):
'''
Returns a tuple of variables given their names
vars can be a list of string names, or a single space separated string of names.
'''
vars = [v.split(' ') for v in vars]
return tuple(getattr(self, v) for v in chain(*vars))
def __repr__(self):
s = 'Values:'
for k, v in self.iteritems():
if k[:9] != 'computed_':
s += '\n ' + k + ' = ' + str(v)
if len(self._computed_values):
s += '\n_computed values:'
for k, v in self._computed_values.iteritems():
s += '\n ' + k + ' = ' + str(v)
return s
def __call__(self, **kwds):
'''
Returns a copy with specified arguments overwritten
Sample usage:
default_p = Parameters(x=1,y=2)
specific_p = default_p(x=3)
'''
p = Parameters(**self) # pylint: disable-msg=W0621
for k, v in kwds.iteritems():
setattr(p, k, v)
return p
def __reduce__(self):
return (_load_Parameters_from_pickle, (self.items(),))
def _load_Parameters_from_pickle(items):
return Parameters(**dict(items))
if __name__ == "__main__":
# turn off warning about attribute defined outside __init__
# pylint: disable-msg=W0201
p = Parameters(
a=5,
b=6,
computed_p1='''
c = a + b
''',
computed_p2='''
x = c**2
''')
q = Parameters(
d=100,
computed_q='''
e = a+d
''',
**p
)
print p.c
p.a = 1
print p.c
print p
print
print q
q.a = -50
print
print q
print
try:
q.c = 5
except AttributeError, e:
print e
p.y = 6
p.computed_p3 = '''
w = a*b
'''
print p
p.a = 2
print p
|