我正在从专有数据库获取目录和项列表。这些列表可能非常庞大,包含数千个视图和各种嵌套。以下是列表示例:
"MIPK",
"MIPK\/CM.toroidal",
"MIPK\/CM.Supervoid",
"MIPK\/DORAS",
"MIPK\/DORAS\/CRUDE",
"MIPK\/DORAS\/CRUDE\/CM.forest",
"MIPK\/DORAS\/CRUDE\/CM.benign",
"MIPK\/DORAS\/CRUDE\/CM.dunes",
"MIPK\/DORAS\/COMMODITIES",
"MIPK\/DORAS\/COMMODITIES\/CRUDE",
"MIPK\/DORAS\/COMMODITIES\/CRUDE\/CM.tangeant",
"MIPK\/DORAS\/COMMODITIES\/CRUDE\/CM.astral",
"MIPK\/DORAS\/COMMODITIES\/CRUDE\/CM.forking"
目录使用大写字母 \/
分隔,大小写混合表示项目。
我的当前返回的 JSON 如下:
{
"contents": [{
"root_path": "MIPK",
"root_name": "MIPK",
"directories": [{
"subd_name": "DORAS",
"subd_path": "MIPK.DORAS"
}],
"views": [{
"view_name": "CM.toroidal"
},
{
"view_name": "CM.Supervoid"
}
]
}, {
"root_path": "MIPK.DORAS",
"root_name": "DORAS",
"directories": [{
"subd_name": "CRUDE",
"subd_path": "MIPK.DORAS.CRUDE"
},
{
"subd_name": "COMMODITIES",
"subd_path": "MIPK.DORAS.COMMODITIES"
}
],
"views": []
}, {
"root_path": "MIPK.DORAS.CRUDE",
"root_name": "CRUDE",
"directories": [],
"views": [{
"view_name": "CM.forest"
},
{
"view_name": "CM.benign"
},
{
"view_name": "CM.dunes"
}
]
}, {
"root_path": "MIPK.DORAS.COMMODITIES",
"root_name": "COMMODITIES",
"directories": [{
"subd_name": "CRUDE",
"subd_path": "MIPK.DORAS.COMMODITIES.CRUDE"
}],
"views": []
}, {
"root_path": "MIPK.DORAS.COMMODITIES.CRUDE",
"root_name": "CRUDE",
"directories": [],
"views": [{
"view_name": "CM.tangeant"
},
{
"view_name": "CM.astral"
},
{
"view_name": "CM.forking"
}
]
}]
}
当前代码:
import logging
import copy
import time
def fetch_resources(input_resources_list):
'''
:return: Json list of dictionaries each dictionary element containing:
Directory name, directory path, list of sub-directories, list of views
resource_list is a flattened list produced by a database walk function
'''
start = time.time()
resources = {
'contents': [{}]
}
for item in input_resources_list:
# Parsing list into usable pieces
components = item.rsplit('\\', 1)
if len(components) == 1:
# Handles first element
root_dict = {'root_path': components[0],
'root_name': components[-1],
'directories': [],
'views': []
}
resources['contents'][0].update(root_dict)
else:
# Enumerate resources in list so search by key value can be done and then records can be appended.
for ind, content in enumerate(copy.deepcopy(resources['contents'])):
if resources['contents'][ind]['root_path'] == components[0]:
# Directories are upper case, adds a new entry if
if clean_item.isupper() :
root_dict = {'root_path': components[0],
'root_name': components[-1],
'directories': [],
'views': []
}
resources['contents'].append(root_dict)
sub_dict = {'subd_path': components[0],
'subd_name': components[-1]}
resources['contents'][ind]['directories'].append(sub_dict)
elif clean_item.isupper() == False :
resources['contents'][ind]['views'] \
.append({'view_name':components[-1]})
print 'It took {}'.format((time.time() - start)*1000)
return resources
这在小工作负载(大约100-500)上运行良好,但在目标工作负载(000的)上则不是。
如何优化该方法以节省时间?
目前,“枚举”循环为了按“root_path”键值搜索输入列表中的每个项而被重建。是否有更简单的方法来搜索字典列表的键值,并将条目附加到该条目的目录和视图列表中?