In [6]:
import dbProvider as db
def getRegionsTree(parentIds=None, depth=1):
result = []
if parentIds == None:
regions = getRegionsByLevel([1])
else:
regions = []
for pid in parentIds:
regions.append(db.getRegionById(pid))
if depth > 0:
for region in regions:
print((1-depth)*\t + "{} {} {}".format(depth, region['id'], region['name']))
regionId = region['id']
children = db.getRegionsByParentIds([regionId])
region['hasChildren'] = False
if len(children) > 0:
region['hasChildren'] = True
if depth > 1 and region['hasChildren']:
for child in children:
print((2-depth)*\t + "{} {}".format(child['id'], child['name']))
child['children'] = getRegionsTree(parentIds = [child['id']], depth = depth - 1)
region['children'] = children
result.append(region)
return result
tree=getRegionsTree(parentIds=[2], depth=2)
Out[6]:
In [7]:
for region in tree:
print(str(region['id'])+" "+region['name'])
for child in region.get('children', []):
print('\t'+str(child['id'])+" "+child['name'])
for child1 in child.get('children', []):
print('\t\t'+str(child1['id'])+" "+child1['name'])
for child2 in child1.get('children', []):
print('\t\t\t'+str(child2['id'])+" "+child2['name'])
for child3 in child2.get('children', []):
print('\t\t\t\t'+str(child3['id'])+" "+child3['name'])
In [ ]: