Created
May 23, 2018 22:10
-
-
Save mvsusp/97d14c0307b62eeea819defeee85b0cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def to_env_vars(mapping): # type: (dict) -> dict | |
| """Transform a dictionary in a dictionary of env vars. | |
| Example: | |
| >>>env_vars = mapping.to_env_vars({'model_dir': '/opt/ml/model', 'batch_size': 25}) | |
| >>> | |
| >>>print(args) | |
| ['MODEL_DIR', '/opt/ml/model', 'BATCH_SIZE', 25] | |
| Args: | |
| mapping (dict[str, object]): A Python mapping. | |
| Returns: | |
| (dict): Dictionary of env vars | |
| """ | |
| def transform_to_keys(key, value): | |
| """Transform a key value pair in one or more env keys""" | |
| keys = {} | |
| def format_key(name): | |
| """Decode a key, adds a SM_ prefix to the key and upper case it""" | |
| if not key: | |
| return u'' | |
| name = _decode(name).upper() | |
| return name if name.startswith(u'SM') else u'SM_%s' % name | |
| # e.f SM_KEY_NAME | |
| formatted_key = format_key(key) | |
| # if value is a dictionary, e.g {a:1, b:2} | |
| if hasattr(value, 'items'): | |
| # SM_KEY_NAME = a,b | |
| keys[formatted_key] = u','.join(sorted(value)) | |
| for _k, _v in value.items(): | |
| # SM_KEY_NAME_A | |
| _name = format_key('%s_%s' % (formatted_key, _k)) | |
| # RECURSIVELY CREATE KEYS | |
| sub_keys = transform_to_keys(_name, _v) | |
| keys.update(sub_keys) | |
| elif isinstance(value, collections.Sequence) and not isinstance(value, six.string_types) and not isinstance( | |
| value, six.binary_type): | |
| decoded_seq = sorted([_decode(element) for element in value]) | |
| keys[formatted_key] = u','.join(decoded_seq) | |
| else: | |
| keys[formatted_key] = _decode(value) | |
| return keys | |
| result = {} | |
| for k, v in mapping.items(): | |
| result.update(transform_to_keys(k, v)) | |
| return result | |
| def to_env_vars(mapping): | |
| return json.dumps({format_key(k): format_value(v) for k, v in mapping.items()}, indent=4) | |
| # | |
| def to_env_vars(mapping): | |
| def format_key(decoded_name): | |
| """Decode a key, adds a SM_ prefix to the key and upper case it""" | |
| if decoded_name: | |
| decoded_name = _decode(decoded_name).upper() | |
| return decoded_name if decoded_name.startswith(u'SM') else u'SM_%s' % decoded_name | |
| else: | |
| return u'' | |
| def format_value(_mapping): | |
| if hasattr(_mapping, 'items'): | |
| return json.dumps({_decode(k): _decode(v) for k, v in _mapping.items()}, sort_keys=True, separators=(',',':'), ensure_ascii=True) | |
| else: | |
| # if isinstance(_mapping, six.string_types + six.binary_type): | |
| return _decode(_mapping) | |
| return json.dumps({format_key(k): format_value(v) for k, v in mapping.items()}, sort_keys=True, separators=(',',':')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment