2019-06-17 15:33:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from six import string_types
|
|
|
|
|
|
|
|
|
2019-07-09 10:55:34 +00:00
|
|
|
def all_keys(d):
|
|
|
|
keys = []
|
|
|
|
for key, val in d.items():
|
|
|
|
if key not in keys:
|
|
|
|
keys.append(key)
|
|
|
|
if isinstance(val, dict):
|
|
|
|
keys += all_keys(val)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
|
|
|
|
def check_keys(keys, separator):
|
2019-07-09 11:09:35 +00:00
|
|
|
if not separator:
|
|
|
|
return
|
|
|
|
for key in keys:
|
|
|
|
if key and isinstance(key, string_types) and separator in key:
|
|
|
|
raise ValueError(
|
|
|
|
'keys should not contain keypath separator '
|
|
|
|
'\'{}\'.'.format(separator))
|
2019-07-09 10:55:34 +00:00
|
|
|
|
|
|
|
|
2019-06-17 15:33:28 +00:00
|
|
|
def join_keys(keys, separator):
|
|
|
|
return separator.join(keys)
|
|
|
|
|
2019-07-09 10:55:34 +00:00
|
|
|
|
2019-06-17 15:33:28 +00:00
|
|
|
def split_keys(key, separator):
|
2019-07-09 11:09:35 +00:00
|
|
|
if not separator:
|
|
|
|
return [key]
|
|
|
|
elif isinstance(key, string_types):
|
|
|
|
keypath = key
|
|
|
|
if separator in keypath:
|
|
|
|
keys = list(keypath.split(separator))
|
2019-06-17 15:33:28 +00:00
|
|
|
return keys
|
|
|
|
else:
|
|
|
|
return [key]
|
2019-07-09 11:09:35 +00:00
|
|
|
elif isinstance(key, (list, tuple, )):
|
|
|
|
keys = []
|
|
|
|
for key_item in key:
|
|
|
|
keys += split_keys(key_item, separator)
|
|
|
|
return keys
|
2019-06-17 15:33:28 +00:00
|
|
|
else:
|
|
|
|
return [key]
|