我希望有一些简单的界面让用户连接一些预定义的模块(如果有必要的话,我可以随意修改它们)。每个模块都有几个指定的输入和输出。一个模块可以连接到几个模块。几个模块可以连接到单个模块。
到目前为止,我的想法是这样的:选项1:每个模块都有用于输入和输出的" in“和"out”字典,连接是通过如下代码建立的:
a.out["out 1"].to(b.in["in 1"]) # connect a's "out 1" output to b's "in 1" input
a.out["out 2"].to(b.in["in 2"]) # connect a's "out 2" output to b's "in 2" input但是这看起来很乏味,所以我想出了选项2:
# connect a to b and c, then list which inputs connect to which outputs
a.to(b,{"out 1":"in 1",
"out 2":"in 2"},
c,{"out 4":"in 1",
"out 3":"in 2"})这看起来更好,因为在我看来,哪些模块是连接的,它们的输出和输入之间的映射也是明确列出的。
我不知道是否有改善上述情况的空间,以清楚显示:
我不精通Python(2.7),因此可能有一些语法、运算符或数据结构我不知道,但我可能会为此目的利用它。
发布于 2017-08-26 22:20:39
我遇到了一个确切的问题,就是能够线性地描述有向图的问题。没有一个特别令人愉快..。就我个人而言,我认为显示源和接收器(节点输入和输出)如何连接比显示哪个节点连接更重要,因为这种关系更具体。
在你列出的两种设计中,我推荐第一种,因为它更清晰。它准确地显示了每个输入和输出是如何连接的。但是,我会稍微加强您的实现。我将使节点类能够为对象中的实际属性的输入和输出创建新句柄。然后在描述图形和捕获流量等时使用这些句柄。使输入和输出实际对象而不是标签意味着构造时间检查和生成更好的错误消息的能力。例如:
b = Node()
b.input("in1", "in2") # this creates a new input object that is located in 'b' with the attribute name of the given string
b.output("out1") # this could be in the constructor
c = Node(in=["in1", "in2"], out=["out1"]) # like so
# describe the connections
a.out1.to(b.in1)
a.out2.to(b.in2)
a.out3.to(c.in2)
a.out4.to(c.in1)
# maybe syntax like 'a.out3 >> c.in2' would be prettier?
module_graph_output = c.out1.capture()
# do stuff with the graph output此外,当用户实现模块逻辑时,可以将这些输出和输入对象处理程序传递给用户。
撇开这一切不说,您将如何运行模块图是更大的问题。如果我没有打扰,这是什么应用?
https://stackoverflow.com/questions/45899716
复制相似问题