You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
1.7 KiB

# -*- coding: utf-8 -*-
#
import os
import json
def join_dir_and_filenames(dirname, filenames):
paths = []
for basename in filenames:
path = dirname + basename
paths.append(path)
return paths
def read_file(path):
with open(path) as f:
file_content = f.read()
return file_content
def write_json(content, outpath):
# create path if not existing
out_dir = os.path.dirname(outpath)
if not os.path.isdir(out_dir):
os.makedirs(out_dir)
# write
with open(outpath, "w+") as f:
json.dump(content, f, indent=4)
# works on valid json like structure lists/dict
def recursive_query(data, filter, transform=lambda x: x):
# all datatypes accepted
# print(type(data))
# assert (type(data) in [list, dict, int, str, bool])
resultset = []
# decorator just handling exceptions
def filter_decorator(data):
try:
return filter(data)
except KeyError:
pass
# filter current data
# only dict types are filtered
if type(data) in [dict]:
if filter_decorator(data):
# transform result
xformed = transform(data)
if xformed:
resultset.append(xformed)
if isinstance(data, dict):
for item in data.values():
childres = recursive_query(item, filter, transform)
if childres:
resultset += childres
elif isinstance(data, list):
for item in data:
childres = recursive_query(item, filter, transform)
if childres:
resultset += childres
return resultset
def remove_dup_dicts(arr_of_dicts):
arr_no_dups = [dict(i) for i in {tuple(d.items()) for d in arr_of_dicts}]
return arr_no_dups