Я анализирую некоторые дрянные списки в python из api anbd, выводимого во вложенную структуру json с определенным форматом, который будет использоваться тонной интерфейсных сервисов.
Ниже приведен пример, в котором каждый элемент является полным путем к файлу.Я не могу изменить этот вход, поскольку он поступает от внешнего сервиса, который проходит через базу данных.Элементы каталога не отображаются в этом списке, только файлы, каталог, в котором находится файл, является очевидным из пути, то есть, ниже нет файлов MIPK / DORAS.Пример ниже:
"/generic_root/site_store/MIPK/CM.toroidal",
"/generic_root/site_store/MIPK/CM.Supervoid",
"/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest",
"/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign",
"/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes",
"/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant",
"/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral",
"/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking"
Была использована предыдущая функция, которая была невероятно медленной, однако в настоящее время я анализирую список, используя следующий код.Формат вывода не тот формат, который я хочу.Я застреваю при добавлении узла в корень.
В приведенном ниже примере он берет путь, находит вложенные каталоги и удаляет корневой путь, присутствующий в каждом файле, затем создает объект узла, который имеет соответствующую вложенную структуру.
После того, как это добавлено в prev_node, оно добавляется в словарь, используя имя каталога в качестве ключа.
import logging
logger = logging.getLogger(__name__)
def main():
# Initialise
root_path = '/generic_root'
store = '/site_store'
file_list = [
"/generic_root/site_store/MIPK/CM.toroidal",
"/generic_root/site_store/MIPK/CM.Supervoid",
"/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest",
"/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign",
"/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes",
"/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant",
"/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral",
"/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking"
]
# Call loop and display results
viewstore_tree_map = create_dir_tree(file_list, root_path, store)
logging.info('\n\tView store keys: %s\n\tKeys: %s\n\tDict of store: %s',
len(viewstore_tree_map.keys()), viewstore_tree_map.keys(),
viewstore_tree_map)
def joiner(delimiter, *args):
'''
Joins path strings correctly, unpack before passing args
'''
return delimiter.join(list(args))
def create_dir_tree(file_list, root_path, store):
'''
File_list [LIST][STR]
root_path [STR]
store [STR]
Return [DICT]
'''
node_map = {}
full_root = root_path+store
for sub_path in file_list:
parents = sub_path.replace(full_root, '').split('/')[1:-1]
prev_node = None
node = None
node_path = full_root
# create tree structure for directory nodes
for parent in parents:
node_path = joiner('/', node_path, parent)
node_exists = 1
if node_path not in node_map:
node_exists = 0
node_map[node_path] = {
'name': parent,
'data': [],
'type': 'dir',
'path': node_path,
}
node = node_map[node_path]
# Handles appending previous dict to data field of new dict
if prev_node and not node_exists:
prev_node['data'].append(node)
prev_node = node
# logger.info(pprint.pprint(prev_node))
if node:
node['data'].append({
'name': sub_path.rsplit('/')[-1],
'type': 'file',
'path': sub_path
})
return node_map
Ниже приведен вывод из приведенного выше кода.Это огромно и будет серьезной проблемой с памятью, поскольку их список со временем будет расти.
node_map = {
'/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE': {
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant',
'type': 'file',
'name': 'CM.tangeant'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral',
'type': 'file',
'name': 'CM.astral'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking',
'type': 'file',
'name': 'CM.forking'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE'
},
'/generic_root/site_store/MIPK/DORAS/CRUDE': {
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest',
'type': 'file',
'name': 'CM.forest'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign',
'type': 'file',
'name': 'CM.benign'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes',
'type': 'file',
'name': 'CM.dunes'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE'
},
'/generic_root/site_store/MIPK/DORAS/COMMODITIES': {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant',
'type': 'file',
'name': 'CM.tangeant'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral',
'type': 'file',
'name': 'CM.astral'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking',
'type': 'file',
'name': 'CM.forking'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE'
}],
'name': 'COMMODITIES',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES'
},
'/generic_root/site_store/MIPK': {
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/CM.toroidal',
'type': 'file',
'name': 'CM.toroidal'
}, {
'path': '/generic_root/site_store/MIPK/CM.Supervoid',
'type': 'file',
'name': 'CM.Supervoid'
}, {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest',
'type': 'file',
'name': 'CM.forest'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign',
'type': 'file',
'name': 'CM.benign'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes',
'type': 'file',
'name': 'CM.dunes'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE'
}, {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant',
'type': 'file',
'name': 'CM.tangeant'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral',
'type': 'file',
'name': 'CM.astral'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking',
'type': 'file',
'name': 'CM.forking'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE'
}],
'name': 'COMMODITIES',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES'
}],
'name': 'DORAS',
'path': '/generic_root/site_store/MIPK/DORAS'
}],
'name': 'MIPK',
'path': '/generic_root/site_store/MIPK'
},
'/generic_root/site_store': {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/CM.toroidal',
'type': 'file',
'name': 'CM.toroidal'
}, {
'path': '/generic_root/site_store/MIPK/CM.Supervoid',
'type': 'file',
'name': 'CM.Supervoid'
}, {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest',
'type': 'file',
'name': 'CM.forest'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign',
'type': 'file',
'name': 'CM.benign'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes',
'type': 'file',
'name': 'CM.dunes'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE'
}, {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant',
'type': 'file',
'name': 'CM.tangeant'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral',
'type': 'file',
'name': 'CM.astral'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking',
'type': 'file',
'name': 'CM.forking'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE'
}],
'name': 'COMMODITIES',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES'
}],
'name': 'DORAS',
'path': '/generic_root/site_store/MIPK/DORAS'
}],
'name': 'MIPK',
'path': '/generic_root/site_store/MIPK'
}],
'name': 'site_store',
'path': '/generic_root/site_store'
},
'/generic_root/site_store/MIPK/DORAS': {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest',
'type': 'file',
'name': 'CM.forest'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign',
'type': 'file',
'name': 'CM.benign'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes',
'type': 'file',
'name': 'CM.dunes'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/CRUDE'
}, {
'type': 'dir',
'data': [{
'type': 'dir',
'data': [{
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant',
'type': 'file',
'name': 'CM.tangeant'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral',
'type': 'file',
'name': 'CM.astral'
}, {
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking',
'type': 'file',
'name': 'CM.forking'
}],
'name': 'CRUDE',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE'
}],
'name': 'COMMODITIES',
'path': '/generic_root/site_store/MIPK/DORAS/COMMODITIES'
}],
'name': 'DORAS',
'path': '/generic_root/site_store/MIPK/DORAS'
}
}
2 вопроса:
- Как вывести правильный нужный формат?(пример ниже)
- Учитывая вышеизложенное, как можно дополнительно оптимизировать функцию по времени для миллионов записей в списке?
desired output = {
"type": "dir",
"data": [{
"type": "dir",
"data": [{
"path": "/generic_root/site_store/MIPK/CM.toroidal",
"type": "file",
"name": "CM.toroidal"
}, {
"path": "/generic_root/site_store/MIPK/CM.Supervoid",
"type": "file",
"name": "CM.Supervoid"
}, {
"type": "dir",
"data": [{
"type": "dir",
"data": [{
"path": "/generic_root/site_store/MIPK/DORAS/CRUDE/CM.forest",
"type": "file",
"name": "CM.forest"
}, {
"path": "/generic_root/site_store/MIPK/DORAS/CRUDE/CM.benign",
"type": "file",
"name": "CM.benign"
}, {
"path": "/generic_root/site_store/MIPK/DORAS/CRUDE/CM.dunes",
"type": "file",
"name": "CM.dunes"
}],
"name": "CRUDE",
"path": "/generic_root/site_store/MIPK/DORAS/CRUDE"
}, {
"type": "dir",
"data": [{
"type": "dir",
"data": [{
"path": "/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.tangeant",
"type": "file",
"name": "CM.tangeant"
}, {
"path": "/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.astral",
"type": "file",
"name": "CM.astral"
}, {
"path": "/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE/CM.forking",
"type": "file",
"name": "CM.forking"
}],
"name": "CRUDE",
"path": "/generic_root/site_store/MIPK/DORAS/COMMODITIES/CRUDE"
}],
"name": "COMMODITIES",
"path": "/generic_root/site_store/MIPK/DORAS/COMMODITIES"
}],
"name": "DORAS",
"path": "/generic_root/site_store/MIPK/DORAS"
}],
"name": "MIPK",
"path": "/generic_root/site_store/MIPK"
}],
"name": "site_store",
"path": "/generic_root/site_store"
}