gpt4 book ai didi

javascript - 从 JS 树创建 Python 函数

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:15 28 4
gpt4 key购买 nike

我需要从以下代码中创建一个函数:

jsHelperFunctions = """
function getTreeList(treeId) {
var $tree = $(treeId);
var itemsList = [];
var treeList = $($tree.jstree().get_json($tree, {
flat: true
})).each(function(i, val) {
itemsList.push($tree.jstree().get_node(val.id))
})

return itemsList;
}

function getItemByItemCode(itemCode, itemsList) {
return itemsList.filter(val => val.original.account_code ==
itemCode)[0]
}"""

这是我的主管给我的一行代码:

treeList = driver.execute_script(jsHelperFunctions + """ return 
getTreeList("#tree");""")

尝试过类似的操作,但我不确定(第一个功能):

def get_tree_list(treeId):
tree = treeId
itemsList = []
treeList = tree.jstree().get_json(tree, flat: true).each(function(i, val)
itemsList.push(tree.jstree().get_node(val.id))
return itemsList

Py 中的 valflat 又是什么?

最佳答案

找到了解决方案。我花了几天时间...

def getTreeList(treeId):
return driver.execute_script(jsHelperFunctions + """ return getTreeList(\""""+ treeId
+"\");")

treeList = getTreeList("#tree")

还有这个...

def getItemByCode(itemCode, treeList):
"""
Returns None if the item code is not found
"""
filtered_iterable = filter(lambda row : row["original"]["item_code"] == itemCode,
treeList)
filtered_list = list(filtered_iterable)
if len(filtered_list) == 0:
return None
assert(len(filtered_list) == 1) #item codes must be unique - so something's wrong
return filtered_list[0]

关于javascript - 从 JS 树创建 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53409700/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com