"구성"세트를 생성하도록 시스템을 설정하려고합니다. 이러한 구성은 python dict에 저장된 간단한 키/값 쌍입니다.Python : "yield"를 사용하여 트리 생성
이러한 구성은 dict을 일련의 함수로 변환 한 결과이며, 이것이 바로 워크 플로입니다. 여기
내가 함께 결국 무엇의 간단한 예입니다 :global_data = [dict()]
def workflow_step1(data):
results = []
for i in range(1,4):
data['key'] = i
results.append(copy.deepcopy(data))
return results
def workflow_step2(data):
results = []
for i in range(1,3):
data['otherkey'] = i
results.append(copy.deepcopy(data))
return results
def workflow_step3(data):
data['yetanotherkey'] = 42
return [copy.deepcopy(data)]
def list_workflow():
return [workflow_step1, workflow_step2, workflow_step3]
def merge(lhs,rhs):
return lhs+rhs
def run(data):
for step in list_workflow():
data = reduce(lambda lhs, rhs: lhs+rhs, [step(d) for d in data])
return data
print run(global_data)
이 잘 가지 작동, 내가 얻을 :
[{'yetanotherkey': 42, 'otherkey': 1, 'key': 1},
{'yetanotherkey': 42, 'otherkey': 2, 'key': 1},
{'yetanotherkey': 42, 'otherkey': 1, 'key': 2},
{'yetanotherkey': 42, 'otherkey': 2, 'key': 2},
{'yetanotherkey': 42, 'otherkey': 1, 'key': 3},
{'yetanotherkey': 42, 'otherkey': 2, 'key': 3}]
당신이 볼 수 있듯이, 목표는 모든 얻는 것입니다 dict의 가능한 조합. 워크 플로의 각 단계는 가능한 조합의 집합을 반환하며 다음 단계에 대한 새로운 가능성을 창출해야합니다.
내가 직면 한 문제는 사용자가 점점 더 많은 워크 플로 단계를 만들어서 조합 폭발로 이어지는 것입니다.
내 순진한 디자인의 문제는 모든 포즈의 전체 트리를 한 번에 생성한다는 것입니다.
나는 한 번에 하나의 가능성을 생성하고 동시에 모든 것을 저장하지 않기 위해 yield
과 발전기를 사용하여 이것을 해결하기를 바랬다.
def workflow_step1(data):
for i in range(1,4):
data['key'] = i
yield copy.deepcopy(data)
def workflow_step2(data):
for i in range(1,3):
data['otherkey'] = i
yield copy.deepcopy(data)
def workflow_step3(data):
data['yetanotherkey'] = 42
yield copy.deepcopy(data)
def list_workflow():
yield workflow_step1
yield workflow_step2
yield workflow_step3
을하지만 난 그냥 내 머리가 각 단계를 순차적으로 처리하는 run
기능을 재 작성하는 방법을 생각 할 수 없습니다. 나는 수확량과 발전기의 두뇌에서 길을 잃는다.
더 많은 아이디어가 환영입니다!
당신은 아마'실행을 쓸 수 있습니다()'당신이 이전과 같은 방식으로. – martineau