Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_pretty_obj(obj, tabs=0, tab=' '):
- pretty_obj = '{\n'
- keys = list(obj.keys())
- for i in range(0, len(obj)):
- key = keys[i]
- value = obj[key]
- pretty_obj += f'{(tabs + 1)*tab}{key}: {get_pretty(value, tabs+1)}'
- if i + 1 != len(obj):
- pretty_obj += ',\n'
- pretty_obj += f'\n{(tabs)*tab}' + '}'
- return pretty_obj
- def get_pretty_array(array, tabs=0, tab=' '):
- pretty_array = '[\n'
- for i in range(0, len(array)):
- value = array[i]
- pretty_array += f'{(tabs + 1)*tab}{get_pretty(value, tabs+1)}'
- if i + 1 != len(array):
- pretty_array += ',\n'
- pretty_array += f'\n{(tabs)*tab}' + ']'
- return pretty_array
- def get_pretty(input, tabs=0):
- input_type = type(input)
- if input_type is dict:
- return get_pretty_obj(input, tabs)
- elif input_type is list:
- return get_pretty_array(input, tabs)
- return input
- if __name__ == "__main__":
- # main()
- obj = {'key': {'key': 'value'}, 'key2': 'value2', 'array': ['hey', {'hey': 'hey'}]}
- print(get_pretty(obj))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement