Skip to content

Instantly share code, notes, and snippets.

@hardiksondagar
Created July 14, 2017 09:58
Show Gist options
  • Save hardiksondagar/739b01eea0ca42187616275823d7a066 to your computer and use it in GitHub Desktop.
Save hardiksondagar/739b01eea0ca42187616275823d7a066 to your computer and use it in GitHub Desktop.
Generate tree from parent child list
raw_data = [
{
"locationid": "106",
"parentlocationid": None,
"name": "Main Meter - 11KV",
"sensorid": 1176,
"appliancetype": "Main HT",
"messageversion": "M26",
"active": True,
"path": [106],
"depth": 1
},
{
"locationid": "114",
"parentlocationid": None,
"name": "Main Meter - 66 KV",
"sensorid": 1180,
"appliancetype": "Main HT",
"messageversion": "M59",
"active": True,
"path": [114],
"depth": 1
},
{
"locationid": "4317",
"parentlocationid": "109",
"name": "500CFM Chicago Pneumatic Air compressor",
"sensorid": 5267,
"appliancetype": "CESC HT-Modem",
"messageversion": "M40",
"active": True,
"path": [4317],
"depth": 1
},
{
"locationid": "107",
"parentlocationid": "106",
"name": "2500 KVA X'mer #1",
"sensorid": 1178,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 107],
"depth": 2
},
{
"locationid": "107",
"parentlocationid": "106",
"name": "2500 KVA X'mer #1",
"sensorid": 1222,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 107],
"depth": 2
},
{
"locationid": "107",
"parentlocationid": "106",
"name": "2500 KVA X'mer #1",
"sensorid": 1239,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 107],
"depth": 2
},
{
"locationid": "108",
"parentlocationid": "106",
"name": "2000 KVA X'mer",
"sensorid": 1177,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 108],
"depth": 2
},
{
"locationid": "108",
"parentlocationid": "106",
"name": "2000 KVA X'mer",
"sensorid": 1179,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 108],
"depth": 2
},
{
"locationid": "108",
"parentlocationid": "106",
"name": "2000 KVA X'mer",
"sensorid": 1230,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 108],
"depth": 2
},
{
"locationid": "108",
"parentlocationid": "106",
"name": "2000 KVA X'mer",
"sensorid": 1231,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 108],
"depth": 2
},
{
"locationid": "109",
"parentlocationid": "106",
"name": "2500 KVA X'mer #2",
"sensorid": 1171,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 109],
"depth": 2
},
{
"locationid": "109",
"parentlocationid": "106",
"name": "2500 KVA X'mer #2",
"sensorid": 1172,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 109],
"depth": 2
},
{
"locationid": "109",
"parentlocationid": "106",
"name": "2500 KVA X'mer #2",
"sensorid": 1173,
"appliancetype": "CESC HT-Modem",
"messageversion": "M33",
"active": True,
"path": [106, 109],
"depth": 2
},
{
"locationid": "109",
"parentlocationid": "106",
"name": "2500 KVA X'mer #2",
"sensorid": 1174,
"appliancetype": "CESC HT-Modem",
"messageversion": "M33",
"active": True,
"path": [106, 109],
"depth": 2
},
{
"locationid": "109",
"parentlocationid": "106",
"name": "2500 KVA X'mer #2",
"sensorid": 1175,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 109],
"depth": 2
},
{
"locationid": "109",
"parentlocationid": "106",
"name": "2500 KVA X'mer #2",
"sensorid": 1240,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [106, 109],
"depth": 2
},
{
"locationid": "186",
"parentlocationid": "114",
"name": "API 2 Main Panel LT",
"sensorid": 1257,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [114, 186],
"depth": 2
},
{
"locationid": "186",
"parentlocationid": "114",
"name": "API 2 Main Panel LT",
"sensorid": 1258,
"appliancetype": "CESC HT-Modem",
"messageversion": "V4",
"active": True,
"path": [114, 186],
"depth": 2
},
{
"locationid": "186",
"parentlocationid": "114",
"name": "API 2 Main Panel LT",
"sensorid": 1259,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [114, 186],
"depth": 2
},
{
"locationid": "186",
"parentlocationid": "114",
"name": "API 2 Main Panel LT",
"sensorid": 1260,
"appliancetype": "CESC HT-Modem",
"messageversion": "V2",
"active": True,
"path": [114, 186],
"depth": 2
},
{
"locationid": "186",
"parentlocationid": "114",
"name": "API 2 Main Panel LT",
"sensorid": 1261,
"appliancetype": "CESC HT-Modem",
"messageversion": "M33",
"active": True,
"path": [114, 186],
"depth": 2
},
{
"locationid": "4317",
"parentlocationid": "109",
"name": "500CFM Chicago Pneumatic Air compressor",
"sensorid": 5267,
"appliancetype": "CESC HT-Modem",
"messageversion": "M40",
"active": True,
"path": [106,109, 4317],
"depth": 3
},
{
"locationid": "1312",
"parentlocationid": "109",
"name": "New Chiller (trane chiller - 8)",
"sensorid": 2340,
"appliancetype": "CESC HT-Modem",
"messageversion": "V4",
"active": True,
"path": [106, 109, 1312],
"depth": 3
},
{
"locationid": "1313",
"parentlocationid": "109",
"name": "SPMBSR Chiller",
"sensorid": 2341,
"appliancetype": "CESC HT-Modem",
"messageversion": "V4",
"active": True,
"path": [106, 109, 1313],
"depth": 3
}
];
for index, location in enumerate(raw_data):
if location["depth"] == 1:
a = [int(location["locationid"])]
if location["parentlocationid"]:
a.append(int(location["parentlocationid"]))
if list(set(a) - set(location["path"])):
if any(raw_location.get('locationid', None) == location["locationid"] and raw_location["depth"] > 1 for raw_location in raw_data):
del raw_data[index]
# pass 1: create nodes dictionary
nodes = {}
#for index,location in enumerate(raw_data):
for index in xrange(len(raw_data) -1, -1, -1):
l = raw_data[index].copy()
id = int(l["locationid"])
if not id in nodes:
nodes[id] = {
'locationid':l["locationid"],
'parentlocationid':l["parentlocationid"],
'sensors':[]
}
else:
del raw_data[index]
l.pop("locationid")
l.pop("parentlocationid")
l.pop("path")
# l.pop("depth")
nodes[id]['sensors'].append(l)
# pass 2: create trees and parent-child relations
forest = []
for location in raw_data:
id = int(location["locationid"])
parent_id = 0
if location["parentlocationid"] != None:
parent_id = int(location["parentlocationid"])
node = nodes[id]
# either make the node a new tree or link it to its parent
if id == parent_id or parent_id == 0:
# start a new tree in the forest
forest.append(node)
else:
# add new_node as child to parent
parent = nodes[parent_id]
if not 'child' in parent:
# ensure parent has a 'children' field
parent['child'] = []
children = parent['child']
children.append(node)
print forest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment