Sunday, August 16, 2015

Flat file to hierarchical tree - Python Way

import json

"""
"This class converts a flat file into a json hierarchical tree
"Inputs: a pandas data frame with flat file loaded
""""
class hierarchy_tree(object):
def __init__(self, data, ):
# self.__file = json_file_path
self.__data = data
self.__result = {'name':None, 'children':[], 'result':None}


"""
Call this public function to convert a json flat file to json tree
"""
def create(self, start=0, finish=None, callback=None):
data = self.__data
self.__callback = None
# check if callback is a function
if callable(callback):
self.__callback = callback

# iterate on each item
for row in data.iterrows():
#each row is a tuple
if finish == None:
finish = len(row[1])

row = row[1][start:finish]
lineage = []
for x in range(len(row)):
lineage.append(row[x])
self.__build_path(lineage)
return json.dumps(self.__result)

"""
This function actually creates nested dictionary
that is later dumped as json
"""
def __build_path(self, lineage):
parent = self.__result

for item in lineage:
# check if the current item exists as dictionary name
index = -1
for child in range(len(parent['children'])):
if parent['children'][child]['name'] == item:
# reset index if item found
index = child
break
# if existing item was not found
if index == -1:
# update as last item in dictionary
parent['children'].append({'name':item, 'children':[], 'result':None})
#
# implement callback
#pass arguments - Item text and its index in lineage
#
if callable(self.__callback):
parent['children'][index]['result'] = self.__callback(lineage, lineage.index(item))
# reset parent
parent = parent['children'][index]


Example Usage:


def callbackfunc(list, index):
return list[index] + str(index)

data = pandas.read_json(<json file path>)
tree = hierarchy_tree(data)
print tree.create(start=0, finish=3, callback=callbackfunc)

No comments:

Post a Comment