Merge config dictionary a into config dictionary b, clobbering the options in b whenever they are also specified in a.
(a, b)
| 60 | |
| 61 | |
| 62 | def _merge_a_into_b(a, b): |
| 63 | """Merge config dictionary a into config dictionary b, clobbering the |
| 64 | options in b whenever they are also specified in a. |
| 65 | """ |
| 66 | if type(a) is not edict: |
| 67 | return |
| 68 | |
| 69 | for k, v in a.iteritems(): |
| 70 | # a must specify keys that are in b |
| 71 | if not b.has_key(k): |
| 72 | raise KeyError('{} is not a valid config key'.format(k)) |
| 73 | |
| 74 | # the types must match, too |
| 75 | old_type = type(b[k]) |
| 76 | if old_type is not type(v): |
| 77 | if isinstance(b[k], np.ndarray): |
| 78 | v = np.array(v, dtype=b[k].dtype) |
| 79 | else: |
| 80 | raise ValueError(('Type mismatch ({} vs. {}) ' |
| 81 | 'for config key: {}').format(type(b[k]), |
| 82 | type(v), k)) |
| 83 | |
| 84 | # recursively merge dicts |
| 85 | if type(v) is edict: |
| 86 | try: |
| 87 | _merge_a_into_b(a[k], b[k]) |
| 88 | except: |
| 89 | print('Error under config key: {}'.format(k)) |
| 90 | raise |
| 91 | else: |
| 92 | b[k] = v |
| 93 | |
| 94 | |
| 95 | def cfg_from_file(filename): |