Last active
November 17, 2016 16:26
-
-
Save agounaris/95adbc0057100255d45d135b61ed9fc5 to your computer and use it in GitHub Desktop.
This file contains 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
import collections | |
def json_object_hook(d): | |
return namedtuple('X', d.keys())(*d.values()) | |
def json2obj(data): | |
return json.loads(data, object_hook=json_object_hook) | |
data = '{"name": "whatever","phone_number": {"home": "aaaa1","mobile": "aaaa2"}}' | |
obj = json2obj(data) | |
print(obj.phone_number.mobile) | |
def find_in_obj(obj, condition, path=None): | |
if path is None: | |
path = [] | |
# In case this is a list | |
if isinstance(obj, list): | |
for index, value in enumerate(obj): | |
new_path = list(path) | |
new_path.append(index) | |
for result in find_in_obj(value, condition, path=new_path): | |
yield result | |
# In case this is a dictionary | |
if isinstance(obj, dict): | |
for key, value in obj.items(): | |
new_path = list(path) | |
new_path.append(key) | |
for result in find_in_obj(value, condition, path=new_path): | |
yield result | |
if not str(value).isalnum() and condition in value: | |
# if condition == value: | |
new_path = list(path) | |
new_path.append(key) | |
new_path.append(condition) | |
yield new_path | |
def find_missing(given_string=None, start='{{', end='}}'): | |
"""Finds and returns non replaced placeholders | |
:param given_string: str | |
:param start: str | |
:param end: str | |
:return: list | |
""" | |
starting_pos = [m.start() for m in re.finditer(start, given_string)] | |
ending_pos = [m.start() for m in re.finditer(end, given_string)] | |
positions = zip(starting_pos, ending_pos) | |
missing = [] | |
for pos in positions: | |
x, y = pos | |
missing_placeholder = given_string[x + len(start):y] | |
if missing_placeholder not in missing: | |
missing.append(missing_placeholder) | |
return missing | |
def replace_placeholders(config=None, by_config=None): | |
"""Replaces {{ }} placeholders based on values | |
from the second argument | |
:param config: string | |
:param by_config: dict | |
:return: dict | |
""" | |
rep = dict((re.escape('{{%s}}' % k), str(v)) for k, v in | |
by_config.iteritems()) | |
pattern = re.compile("|".join(rep.keys())) | |
events_config_as_string = pattern.sub(lambda m: rep[re.escape(m.group(0))], | |
config) | |
replaced_events_config = json.loads(events_config_as_string) | |
return replaced_events_config | |
def flatten(config=None, parent_key='', sep='.'): | |
"""Flattens a dictionary to the form of | |
key_a.key_b.value | |
:param config: string | |
:param parent_key: string | |
:param sep: string | |
:return: dict | |
""" | |
items = [] | |
for key, value in config.items(): | |
new_key = parent_key + sep + key if parent_key else key | |
if isinstance(value, collections.MutableMapping): | |
items.extend(flatten(value, new_key, sep=sep).items()) | |
else: | |
items.append((new_key, value)) | |
return dict(items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment