mingrammer / diagrams

:art: Diagram as Code for prototyping cloud system architectures
https://diagrams.mingrammer.com
MIT License
37.54k stars 2.45k forks source link

TypeError: unsupported operand type(s) for -: 'list' and 'list' #490

Open jbrunetext opened 3 years ago

jbrunetext commented 3 years ago

Try his on my computer

ELB("lb") >> [EC2("worker1"), EC2("worker2"), EC2("worker3"), EC2("worker4"), EC2("worker5")] - [EC2("Ec1"), EC2("Ec2"), EC2("Ec3")]

don' t work for me , do you have any idea ?

File "/Users/JULIEN/github/diagrams/main.py", line 186, in ELB("lb") >> [EC2("worker1"), TypeError: unsupported operand type(s) for -: 'list' and 'list'

jbrunetext commented 3 years ago

You can't connect two lists directly because shift/arithmetic operations between lists are not allowed in Python.

clayms commented 3 years ago

If you need a generic solution, see https://github.com/mingrammer/diagrams/issues/493#issuecomment-806158448

clayms commented 3 years ago

for completeness


from diagrams import Diagram, Node
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB

def list_to_list(l1:list, l2:list) -> None:
    for nd in l1:
        nd >> l2

with Diagram("", show=False, direction="TB") as diag:
    elb = ELB("lb") 
    list_a = [EC2("worker1"), EC2("worker2"), EC2("worker3"), EC2("worker4"), EC2("worker5")]
    list_b = [EC2("Ec1"), EC2("Ec2"), EC2("Ec3")]

    elb >> list_a
    list_to_list(list_a, list_b)

diag

image

clayms commented 3 years ago

For a more elegant look:

from diagrams import Diagram, Node, Edge
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB

graph_attr = {
    "layout":"dot",
    "concentrate":"true",
    "splines":"spline",
}

with Diagram("", show=False, direction="TB", graph_attr=graph_attr,) as diag:
    elb = ELB("lb") 
    list_a = [EC2("worker1"), EC2("worker2"), EC2("worker3"), EC2("worker4"), EC2("worker5")]
    list_b = [EC2("Ec1"), EC2("Ec2"), EC2("Ec3")]

    blank = Node("", shape="plaintext", height="0.0", width="0.0")

    elb >> Edge(headport="n", tailport="s", minlen="1") >> list_a
    list_a - Edge(headport="n", tailport="s", minlen="1") - blank
    blank >> Edge(headport="n", tailport="s", minlen="1") >> list_b

diag

image