Files
wpylib/params/params_flat_test.py
wirawan aad559d86b * Class Parameters: important change of behavior for more safety to avoid
potential confusion.

* Detail of the important changes:

  1) by default now the function's local variables are not included in the
     lookup chain. Use _localvars_ flag if you insist on doing that; but
     be aware of the caveat (see Parameters' documentation).

  2) the default _kwparam_ and _userparam_ names are now passed on to the
     child Parameters object instead of the override values given through
     the _create_ function argument.
     Rationale: To begin with, the default names defined during the creation
     of the parent Parameters object should be used by *all* the relevant
     functions.
     Overrides should be regarded as exceptional rather than common cases.

  3) Value overrides (_kwparam_, _userparam_, etc.) in _create_ must now
     be specified in keyword=value manner rather than as positional argument.
     Rationale: Those overrides must be made conspicuous.
     The keyword=value approach also makes the code more resilient to minor
     API changes.
     All unnamed parameters are supposed to be low-priority defaults.
2011-09-09 18:58:48 +00:00

64 lines
1.3 KiB
Python

# $Id: params_flat_test.py,v 1.2 2011-09-09 18:58:48 wirawan Exp $
# 20100930
from wpylib.params import flat as params
global_defaults = params(
nbasis= 275,
npart= 29,
deltau= 0.01,
)
def test1():
defaults = {
'nbasis': 320,
'npart': 37,
'deltau': 0.025,
}
p = params(defaults, nbasis=332)
nbasis = 327
print "test1()"
print "self-defined values = ", p
print "nbasis = ", p.nbasis
print "npart = ", p.npart
print "deltau = ", p.deltau
p.deltau = 0.01
print "new deltau = ", p.deltau
def test2(**_opts_):
p = global_defaults._create_(_localvars_=1)
nbasis = 327
print "test2()"
print "self-defined values = ", p
print "nbasis = ", p.nbasis # gives 275 -- although _localvars_ already requested.
print "npart = ", p.npart
print "deltau = ", p.deltau
p.deltau = 0.01
print "new deltau = ", p.deltau
def test2b(**_opts_):
nbasis = 327
p = global_defaults._create_(_localvars_=1)
nbasis = 3270
print "test2()"
print "self-defined values = ", p
print "nbasis = ", p.nbasis # gives 327. Changes to local vars won't alter anything.
print "npart = ", p.npart
print "deltau = ", p.deltau
p.deltau = 0.01
print "new deltau = ", p.deltau
if __name__ == "__main__":
test1()
test2()
test2b()