change variable name related to loop iteration number

change variable name related to loop iteration number

Andy_rapid-ood
Participant Participant
762 Views
3 Replies
Message 1 of 4

change variable name related to loop iteration number

Andy_rapid-ood
Participant
Participant

Hello,

I am just creating a script that should implement a certain number of text strings (t_1, t_2...) into the same number of sketches. This sketches are always named "Text1_sketch, Text2_sketch..." the text strings ("ZZZ", "UUU"...) are entered manually in the script.

In general I am just starting a loop 1-5 and iterate "i" to use it as indicator for the related sketch AND the related variable. But the variable is not correctly assigned or initialized. I guess it is just my lag in python programming. Has anyone a hint how to "easily" achieve my goal

 

        # change text in sketch "i" to variable "t_i"
        sketches = app.activeProduct.rootComponent.sketches  
        for i in range(1,5😞
            t_1 = "ZZZ"
            t_2 = "UUU"
            t_3 = "PPP"
            t_4 = "EEE"
            sketchname = "Text" + str(i) + "_sketch"
            textstring = "t_"+(i)
            textsketch = sketches.itemByName(sketchname)
            textsketchtexts = textsketch.sketchTexts
            #.item(no.): no. indicates number of text in dedicated sketch
            textinputitem = textsketchtexts.item(0)
            textinputitem.text = textstring
0 Likes
Accepted solutions (1)
763 Views
3 Replies
Replies (3)
Message 2 of 4

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

You can do it this way:

 

    # change text in sketch "i" to variable "t_i"
    texts = {
        1 : "ZZZ",
        2 : "UUU",
        3 : "PPP",
        4 : "EEE",
    }
    sketches = app.activeProduct.rootComponent.sketches  
    for i in range(1,5):
        sketchname = f"Text{i}_sketch"
        textstring = texts[i]
        textsketch = sketches.itemByName(sketchname)
        textsketchtexts = textsketch.sketchTexts
        #.item(no.): no. indicates number of text in dedicated sketch
        textinputitem = textsketchtexts.item(0)
        textinputitem.text = textstring

 

 

This is the result in my testing:

wtallerdemadera_0-1687265222462.png

I hope this can help.

 

Regards,

Jorge Jaramillo

Message 3 of 4

Andy_rapid-ood
Participant
Participant

Hello Jorge,

 

great, yes, that works. Bad python knowledge from my side I guess.

Am I right that {1, 2, 3, 4} is creating a "dictionary" and texts[i] is accessing the dictionary?

I don't understand the "f" and the curly braces here. Can you maybe drop two sentences for explanation?

sketchname = f"Text{i}_sketch"

 

0 Likes
Message 4 of 4

Jorge_Jaramillo
Collaborator
Collaborator

Hi @Andy_rapid-ood ,

 

The "texts" variable is a dictionary.  It is first defined and then with texts[i] you get an item from it.  In this case, an integer value as the key; it also support many other variable types as key values.

 

Regard the f-string, you can read about it here on section 7.1.1.

It is straight way to format strings.  What is included inside curly brackets will be replace by the interpret at run time, and its value is append in the position it was placed.  I used regularly, mainly for debugging purposes.

 

Best regards,

Jorge Jaramillo