From d62e875b99c7d05ce77d8938df6010b7a890210e Mon Sep 17 00:00:00 2001 From: Mark McIntyre Date: Tue, 13 Jun 2023 12:14:48 -0400 Subject: [PATCH] made flatten a runable script --- python/flatten | 95 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 20 deletions(-) diff --git a/python/flatten b/python/flatten index 3fd1b38..0230d02 100755 --- a/python/flatten +++ b/python/flatten @@ -1,32 +1,87 @@ -def flatten(data_object, key_name=None): - # print('-' * 10) - # print(f"data_object = {data_object}, key_name = {key_name}") +#!/usr/bin/env python3 + +import argparse +import json +import logging + + +_level = logging.INFO +_format = "%(asctime)-15s [%(levelname)-8s] %(lineno)d : %(funcName)s : %(message)s" +logging.basicConfig(format=_format, level=_level) +logger = logging.getLogger() + + +def parse_args(): + argp = argparse.ArgumentParser() + + argp.add_argument( + '--debug', + action='store_true', + help="Run in debug mode" + ) + + argp.add_argument( + 'json_filename', + help="JSON filename to flatten" + ) + + argp.add_argument( + '--output-file', '-o', + help="Output filename for results" + ) + + return argp.parse_args() + + +def flatten(data_object, key_name=None, variable_list=[]): + """Will flatten a JSON file by listing the variables in + a dot notation. + + Args: + data_object: Data as dict, list, string, etc. to parse recursively + key_name: String of the key name to pass into in the next call + variable_list: List of the generated variables + + Returns: + The list of the variables in dot notation. + """ if type(data_object) == list: for data_line in data_object: - # print(f"dl | data_line = {data_line}") - # print(f"dl | key_name = {key_name}") - # print(f":recursion") - flatten(data_line, key_name=key_name) - # print(":returned to the stack") + flatten(data_line, key_name=key_name, variable_list=variable_list) elif type(data_object) == dict: for k, v in data_object.items(): - # print(f"dd | k, v = {k}, {v}") - # print(f"dd | key_name = {key_name}") key_name = f"{key_name}.{k}" if key_name else k - # print(f"dd | key_name.k = {key_name}") - # print(f":recursion") - flatten(v, key_name=key_name) + flatten(v, key_name=key_name, variable_list=variable_list) - # print(":returned to the stack") - # print(f"dd | key_name = {key_name}") key_name = ".".join(key_name.split('.')[:-1]) - # print("...back tracking key_name one level") else: - print(f"--> {key_name}: {data_object}") if key_name else None + variable_list.append(f"{key_name}: {data_object}" if key_name else None) - # print("<< returning from this run") - # print('=' * 10) + return variable_list - return + +def main(): + args = parse_args() + + if args.debug: + logger.info("Running in debug mode") + logger.setLevel(logging.DEBUG) + + with open(args.json_filename) as fp: + json_file = json.load(fp) + + json_variables = flatten(json_file) + + if args.output_file: + with open(args.output_file, 'w') as fp: + for variable in json_variables: + fp.write(f"{variable}\n") + else: + for variable in json_variables: + print(f"{variable}") + + +if __name__ == "__main__": + main()