按照依賴關係排序

2021-09-27 02:13:09 字數 1745 閱讀 5592

import copy

import utils.functool

@utils.functool.settimeout(timeout=15)

def dependsonsort(testfunnames, dependrelation):

"""有個問題就是如果出現a依賴b,而b又依賴a就會出現問題

:param testfunnames: 乙個列表,被排序的列表[a,b,c,d,...]

:param dependrelaton: 依賴關係[(a,b),(c,b),...],其中元祖中的第乙個值代表被依賴項,第二個值代表依賴項

:return: 返回按照依賴關係排序後的陣列

"""# 排序後的執行函式順序

target =

tmp = copy.deepcopy(testfunnames)

while true:

# 用來記錄依賴項,即低優先順序執行,可能有重複需要set去重

dependings =

# 用來記錄刪除的依賴關係中的依賴項,也是下次再次用來去除依賴項的資料來源

# depended =

if false not in map(lambda x: x is none, dependrelaton):

break

# 第一步,從rl中提取出依賴項

for _ in dependrelaton:

if _ is none:

continue

depend, depending = _

# 移除依賴的函式名,剩下的是需要被優先執行的

for _ in set(dependings):

tmp.remove(_)

# 第二步, 從函式依賴關係中,刪除被優先執行的關係項

for _ in dependrelation:

if _ is none:

continue

depend, depending = _

if depend in tmp:

index = dependrelation.index(_)

dependrelation.pop(index)

dependrelation.insert(index, none)

# 第三步, 將本次計算的可以先執行的函式,新增到上一次的計算出來的後面

target += tmp

# 補集, 下一次需要排序函式名

tmp = list(set(testfunnames) - set(target))

# +未被依賴的函式名,直接放在隊尾

return target + (list(set(testfunnames) - set(target)))

if __name__ == "__main__":

testfunnames = ["test_01", "test_02", "test_03", "test_04", "test_05", "test_06"]

dependrelaton = [("test_01", "test_02"),

("test_01", "test_04"),

("test_02", "test_03"),

("test_04", "test_03"),

("test_05", "test_06")

]print(dependsonsort(testfunnames, dependrelaton))

pass

根據依賴關係排序

就為了盡快實現功能,效能可能不是很好 1,2 1,3 2,3 3,none 3,4 4,5 1,2 1,3 2,3 3,none 3,4 4,2 5,4,3,2,1 exception 存在迴圈依賴 1,2 1,3 2,3 3,none 3,4 4,2 如下 def depend services ...

查詢使用者表(按照依賴物件排序)

set vi levelpath 1 得到所有物件,不包括系統物件 insert into vt objdeppath levelpath,oname,type select vi levelpath,o.name o.type from sysobjects o where xtype not i...

依賴關係 關聯關係

一 依賴關係 a物件作為b物件的區域性變數或是方法形參的,b依賴於a,這時候我們稱a與b之間存在一種依賴關係 給土豪打 物件作為形式引數 void calltuhaowithphone czphone phone void sendmessagetodiaosi 1 什麼是引數依賴 當a物件作為另外...