Returns the value of `key` if it exists, else `default`.
(self, key, default=None)
| 633 | return {n: getattr(self, n) for n in self._hparam_types.keys()} |
| 634 | |
| 635 | def get(self, key, default=None): |
| 636 | """Returns the value of `key` if it exists, else `default`.""" |
| 637 | if key in self._hparam_types: |
| 638 | # Ensure that default is compatible with the parameter type. |
| 639 | if default is not None: |
| 640 | param_type, is_param_list = self._hparam_types[key] |
| 641 | type_str = 'list<%s>' % param_type if is_param_list else str(param_type) |
| 642 | fail_msg = ("Hparam '%s' of type '%s' is incompatible with " |
| 643 | 'default=%s' % (key, type_str, default)) |
| 644 | |
| 645 | is_default_list = isinstance(default, list) |
| 646 | if is_param_list != is_default_list: |
| 647 | raise ValueError(fail_msg) |
| 648 | |
| 649 | try: |
| 650 | if is_default_list: |
| 651 | for value in default: |
| 652 | _cast_to_type_if_compatible(key, param_type, value) |
| 653 | else: |
| 654 | _cast_to_type_if_compatible(key, param_type, default) |
| 655 | except ValueError as e: |
| 656 | raise ValueError('%s. %s' % (fail_msg, e)) |
| 657 | |
| 658 | return getattr(self, key) |
| 659 | |
| 660 | return default |
| 661 | |
| 662 | def __contains__(self, key): |
| 663 | return key in self._hparam_types |