def camel_to_snake(camel_case): camel_case = camel_case.replace('ID', 'Id') res = camel_case[0].lower() for c in camel_case[1:]: if c.isupper(): res += f'_{c.lower()}' continue res += c return res def snake_to_camel(snake_case): res = snake_case.split('_')[0] res += ''.join(w.title() if w != 'id' else w.upper() for w in snake_case.split('_')[1:]) return res def convert_dict_names(rename_func, to_convert): if isinstance(to_convert, dict): new = to_convert.__class__() for key, v in to_convert.items(): new[rename_func(key)] = convert_dict_names(rename_func, v) elif isinstance(to_convert, (list, tuple, set)): new = to_convert.__class__([convert_dict_names(rename_func, item) for item in to_convert]) else: return to_convert return new def remove_dict_values(a_dict, map_to_none_fn): if isinstance(a_dict, dict): result = a_dict.__class__() for k, v in a_dict.items(): should_ignore = map_to_none_fn(v) if not should_ignore: result[k] = remove_dict_values(v, map_to_none_fn) return result elif isinstance(a_dict, list): return [remove_dict_values(v, map_to_none_fn) for v in a_dict] elif not map_to_none_fn(a_dict): return a_dict def emtpy_string_to_none(some_dict): def default_to_none(value): if not isinstance(value, bool): if not value: return True return False return remove_dict_values(some_dict, default_to_none) def dict_to_camel(value): return convert_dict_names(snake_to_camel, value) def dict_to_snake(value): return convert_dict_names(camel_to_snake, value)