General Python question (sorry): pass string as function and argument?



  • Sorry to clot the DrawBot board with what is certainly a general Python question (and probably a simple one at that), but stackoverflow isn't helping, probably because I'm not phrasing the question right. Here goes:

    I have code that uses functions to define shapes in a matrix and return their outlines as a BezierPath(). The functions have descriptive names that call the function with no arguments—for example,

    c1()
    

    The code also has a function, compareShape(), that tests all unique pairs of these outlines to see if they touch or intersect; if they do not, it creates a new frame and draws the shapes from those outlines in that frame. That function is called with the functions that draw the outlines to be tested as arguments; for example:

    compareShape(c1(), c1t())
    

    My problem: I can use combinations() from itertools to generate a list of these unique pairs, but I have to pass the text from those pairs as function calls/arguments. What am I trying to do here? Like I said, I'd look it up but I'm not sure I'm framing the question right. Code follows:

    from itertools import combinations
    
    canvasX = 1000
    canvasY = 1250
    u = (canvasX/50)
    
    def c1():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c1t():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u - (9 * u), -8 * u, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c1ti():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u + (7 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u + (7 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u - (9 * u), 7 * u, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c1b():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u - (9 * u), 0, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c1bi():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u - (7 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u - (7 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u - (9 * u), -8 * u - (7 * u), 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c1r():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u - (9 * u), -8 * u, 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c1l():
        c = BezierPath()
        c.oval(-8 * u - (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u - (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-(9 * u), -8 * u, 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c2():
        c = BezierPath()
        c.oval(-8 * u, -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
        
    def c2t():
        c = BezierPath()
        c.oval(-8 * u, -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, -8 * u, 16 * u, 7.5 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c2ti():
        c = BezierPath()
        c.oval(-8 * u, -8 * u + (7 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u + (7 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, 7 * u, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c2b():
        c = BezierPath()
        c.oval(-8 * u, -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, .5 * u, 16 * u, 7.5 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c2bi():
        c = BezierPath()
        c.oval(-8 * u, -8 * u - (7 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u - (7 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, -8 * u - (7 * u), 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c2r():
        c = BezierPath()
        c.oval(-8 * u, -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, -8 * u, 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c2l():
        c = BezierPath()
        c.oval(-8 * u, -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(0, -8 * u, 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3t():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u + (9 * u), -8 * u, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3ti():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u + (7 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u + (7 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u + (9 * u), 7 * u, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3b():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u + (9 * u), 0, 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3bi():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u - (7 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u - (7 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u + (9 * u), -8 * u - (7 * u), 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3r():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u + (9 * u), -8 * u, 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c3l():
        c = BezierPath()
        c.oval(-8 * u + (9 * u), -8 * u, 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u + (9 * u), -7 * u, 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(9 * u, -8 * u, 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c4():
        c = BezierPath()
        c.oval(-8 * u, -8 * u - (17 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u - (17 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
        
    def c4t():
        c = BezierPath()
        c.oval(-8 * u, -8 * u - (17 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u - (17 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, -8 * u - (17 * u), 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c4b():
        c = BezierPath()
        c.oval(-8 * u, -8 * u - (17 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u - (17 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, -(17 * u), 16 * u, 8 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c4r():
        c = BezierPath()
        c.oval(-8 * u, -8 * u - (17 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u - (17 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(-8 * u, -8 * u - (17 * u), 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    
    def c4l():
        c = BezierPath()
        c.oval(-8 * u, -8 * u - (17 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u - (17 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        r = BezierPath()
        r.rect(0, -8 * u - (17 * u), 8 * u, 16 * u)
        c = c.difference(r)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    """
    def c5(): #may not be needed
        c = BezierPath()
        c.oval(-8 * u, -8 * u + (17 * u), 16 * u, 16 * u)
        ch = BezierPath()
        ch.oval(-7 * u, -7 * u + (17 * u), 14 * u, 14 * u)
        ch.closePath()
        c = c.difference(ch)
        c.closePath()
        if toggle == 1:
            drawPath(c)
        return c
    """
    def b1m():
        b = BezierPath()
        b.rect(-12 * u, -7 * u, u, 14 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b1t():
        b = BezierPath()
        b.rect(-12 * u, -7 * u, u, 22 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b1b():
        b = BezierPath()
        b.rect(-12 * u, -15 * u, u, 22 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b2m():
        b = BezierPath()
        b.rect(-u/2, -7 * u, u, 14 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b2t():
        b = BezierPath()
        b.rect(-u/2, -7 * u, u, 22 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b2b():
        b = BezierPath()
        b.rect(-u/2, -15 * u, u, 22 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b3m():
        b = BezierPath()
        b.rect(11 * u, -7 * u, u, 14 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b3t():
        b = BezierPath()
        b.rect(11 * u, -7 * u, u, 22 * u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b3b():
        b = BezierPath()
        b.rect(11 * u, -15 * u, u, 22 * u)
        b.closePath()
        drawPath(b)
        return b
        
    def b4r():
        b = BezierPath()
        b.rect(0, -u/2, 8 * u, u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b4l():
        b = BezierPath()
        b.rect(-8 * u, -u/2, 8 * u, u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
    
    def b5():
        b = BezierPath()
        b.rect(-8 * u, 9 * u, 16 * u, u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def b6():
        b = BezierPath()
        b.rect(-8 * u, -10 * u, 16 * u, u)
        b.closePath()
        if toggle == 1:
            drawPath(b)
        return b
        
    def a1():
        a = BezierPath()
        a.moveTo((-9 * u, 7 * u))
        a.lineTo((-9 * u + (12.7017059 * u), -15 * u))
        a.lineTo((
            -9 * u + (12.7017059 * u) - (u * cos(radians(30))),
            -15 * u - (u * sin(radians(30)))
            ))
        a.lineTo((
            -9 * u - (u * cos(radians(30))),
            7 * u - (u * sin(radians(30)))
            ))
        a.closePath()
        if toggle == 1:
            drawPath(a)
        return a
    
    def a1s():
        a = BezierPath()
        a.moveTo((-9 * u, 7 * u))
        a.lineTo((-9 * u + (8.0829037 * u), -7 * u))
        a.lineTo((
            -9 * u + (8.0829037 * u) - (u * cos(radians(30))),
            -7 * u - (u * sin(radians(30)))
            ))
        a.lineTo((
            -9 * u - (u * cos(radians(30))),
            7 * u - (u * sin(radians(30)))
            ))
        a.closePath()
        if toggle == 1:
            drawPath(a)
        return a
    
    def a1si():
        a = BezierPath()
        a.moveTo((-9 * u, -7 * u))
        a.lineTo((-9 * u + (8.0829037 * u), 7 * u))
        a.lineTo((
            -9 * u + (8.0829037 * u) - (u * cos(radians(30))),
            7 * u + (u * sin(radians(30)))
            ))
        a.lineTo((
            -9 * u - (u * cos(radians(30))),
            -7 * u + (u * sin(radians(30)))
            ))
        a.closePath()
        if toggle == 1:
            drawPath(a)
        return a
    
    def a2():
        a = BezierPath()
        a.moveTo((9 * u, 7 * u))
        a.lineTo((9 * u - (12.7017059 * u), -15 * u))
        a.lineTo((
            9 * u - (12.7017059 * u) + (u * cos(radians(30))),
            -15 * u - (u * sin(radians(30)))
            ))
        a.lineTo((
            9 * u + (u * cos(radians(30))),
            7 * u - (u * sin(radians(30)))
            ))
        a.closePath()
        if toggle == 1:
            drawPath(a)
        return a
    
    def a2s():
        a = BezierPath()
        a.moveTo((9 * u, 7 * u))
        a.lineTo((9 * u - (8.0829037 * u), -7 * u))
        a.lineTo((
            9 * u - (8.0829037 * u) + (u * cos(radians(30))),
            -7 * u - (u * sin(radians(30)))
            ))
        a.lineTo((
            9 * u + (u * cos(radians(30))),
            7 * u - (u * sin(radians(30)))
            ))
        a.closePath()
        if toggle == 1:
            drawPath(a)
        return a
    
    def a2si():
        a = BezierPath()
        a.moveTo((9 * u, -7 * u))
        a.lineTo((9 * u - (8.0829037 * u), 7 * u))
        a.lineTo((
            9 * u - (8.0829037 * u) + (u * cos(radians(30))),
            7 * u + (u * sin(radians(30)))
            ))
        a.lineTo((
            9 * u + (u * cos(radians(30))),
            -7 * u + (u * sin(radians(30)))
            ))
        a.closePath()
        if toggle == 1:
            drawPath(a)
        return a
    
    matrix = ['c1', 'c1t', 'c1ti', 'c1b', 'c1bi', 'c1r', 'c1l', 'c2', 'c2t', 'c2ti', 'c2b', 'c2bi', 'c2r', 'c2l', 'c3', 'c3t', 'c3ti', 'c3b', 'c3bi', 'c3r', 'c3l', 'c4', 'c4t', 'c4b', 'c4r', 'c4l', 'b1m', 'b1t', 'b1b', 'b2m', 'b2t', 'b2b', 'b3m', 'b3t', 'b3b', 'b4r', 'b4l', 'b5', 'b6', 'a1', 'a1s', 'a1si', 'a2', 'a2s', 'a2si']
    pairs = []
    
    def compareShape(s1, s2):
        if len(set(s1.points) & set(s2.points)) == 0 and len(s1.intersectionPoints(s2)) == 0:
            newPage(canvasX, canvasY)
            fill(1)
            rect(0, 0, canvasX, canvasY)
            translate(canvasX/2, canvasY/2)
            fill(0)
            drawPath(s1)
            drawPath(s2)           
    
    for subset in combinations(matrix, 2):
        pairs.append(subset)
    
    for n in range(len(pairs)):
        compareShape(pairs[n][0](), pairs[n][1]())
    
    #saveImage('~/Desktop/gürtler_script_matrix.jpg')
    


  • The error being thrown is:

    Traceback (most recent call last): File "gürtler_script1_matrix.py", line 619, in <module> TypeError: 'str' object is not callable



  • @mauricemeilleur Figured it out: the problem is that the matrix and pairs lists are populated with strings. I moved all the function definitions to the top and populated the matrix with their names—problem solved. Now I have a bunch of new problems to solve, but they're what I expected. Coding is the gift that keeps on giving.


  • admin

    side note use triple ` to highlight code

    (Ive edited your post)



  • Thanks! Forgot the extras on the last section—that code is crappy anyway and has since been improved …


Log in to reply