Last active
December 16, 2015 21:58
-
-
Save gazpachoking/5503252 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 merge_settings(request_setting, session_setting, dict_class=OrderedDict): | |
""" | |
Determines appropriate setting for a given request, taking into account the setting on that | |
request, and the setting in the session. If a setting is a dictionary, they will be merged together | |
using `dict_class` | |
""" | |
if session_setting is None: | |
return request_setting | |
if request_setting is None: | |
return session_setting | |
# Bypass if not a dictionary (e.g. timeout) | |
if ( | |
not isinstance(session_setting, Mapping) or | |
not isinstance(request_setting, Mapping) | |
): | |
return request_setting | |
merged_setting = dict_class(to_key_val_list(session_setting)) | |
request_setting = dict_class(to_key_val_list(request_setting)) | |
merged_setting.update(request_setting) | |
# Remove keys that are set to None. | |
for (k, v) in request_setting.items(): | |
if v is None: | |
del merged_setting[k] | |
return merged_setting |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment