Skip to content

Instantly share code, notes, and snippets.

@cmey
Last active September 21, 2016 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmey/b680afc890d61f3e1e94db8431f2bef6 to your computer and use it in GitHub Desktop.
Save cmey/b680afc890d61f3e1e94db8431f2bef6 to your computer and use it in GitHub Desktop.
Generalized params update function
def update_params(params):
# update params if user said 'auto', otherwise take user specified value
# a first try at reusing for all params the same code for that logic
for param_key, param_value in params.items():
actual_key = 'actual_' + param_key
# Is this a param that has a user mode?
has_user_mode = actual_key in params
if has_user_mode:
if param_value is 'user-forced':
# we could even do more checks here, maybe param-specific
assert params[actual_key] is not None
# just copy the user specified value to the actual
params[actual_key] = param_value
elif param_value is 'auto':
# debatable, we could just overwrite silently (but I prefer to assert)
assert params[actual_key] is None
# this is major change: need a compute_derived_param for all params separately
# makes things cleaner?
params[actual_key] = compute_derived_param(params, param_key, param_value)
else:
raise ValueError("unsupported value {} in param key: {}. Needs to be 'auto' "
"or 'user-forced'.".format(param_value, param_key))
def compute_derived_param(params, param_key, param_value):
# probably better to have a complete separate function per param update
if param_key is 'az_aperture':
return [1, 2, 3]
def test_update_params():
user_params = dict(
# legacy style: just the value
tx_frequency=4 * units.megahertz,
# new style: user-forced
depth='user-forced',
actual_depth=3,
# new style: auto
az_aperture='auto',
actual_az_aperture=None,
)
updated_params = update_params(user_params)
assert(user_params['tx_frequency'] == updated_params['tx_frequency'] == 4 * units.megahertz)
assert(user_params['depth'] == 'user-forced')
assert(user_params['actual_depth'] == 3)
assert(user_params['az_aperture'] == 'auto')
assert(user_params['actual_az_aperture'] == [1, 2, 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment