Automate a python script through strings?

Viewed 84

let say that thoses python objects below are locked we just cannot change the code, all we can is writing right after it. i know it's aweful. but let say that we are forced to work with this.

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"
#let say that there's 99 of them

How to print the name of each and single one of them (99) witouth repetition ?

from my noob perspective. the ideal way to resolve this case witouth repetition is using the same logic that we have with strings.

Because name => Name+index so it can be really easy to iterate with them.

so somewhat a code that work in the same logic of the totally fictive one below:

for i in range (1,100):
    print(Name+f"{i:02d}")
for i in range (1,100):
     string_v_of_obj = "Name" + str(f"{i:02d}")
     print(func_transform_string_to_code(string_v_of_obj))

maybe something like that is possible.

for python_object in script_objects:
    if Name in python_object:
       print(python_object)
5 Answers

This could do the trick:

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"

vars = locals().copy()
for i in vars:
    if 'Name' in i:
        print((i, eval(i)))

alternative in one line:

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"

print([(i, eval(i)) for i in locals().copy() if "Name" in i])

You can access the global variables through globals() or if you want the local variables with locals(). They are stored in a dict. So

for i in range (1,100):
    print(locals()[f"Name{i:02d}"])

should do what you want.

You can use exec:

Name01 = "Dorian"
Name02 = "Tom"
Name04 = "Jerry"
Name03 = "Jessica"
for i in range(1,5):
    num = f"{i:02d}" # python3.6+
    num = "{0:02d}".format(i) # python 3.x
    num = "%02d"%i # python 2
    exec('print(Name'+num+')')

Using this code, as per requirement, you will just print names without printing duplicates:

Name01 = "Dorian"
Name02 = "Tom"
Name03 = "Tom"
Name04 = "Jerry"
Name05 = "Jessica"
Name06 = "Jessica"

vars = locals().copy()

names = []

for variable in vars:
    # This will make sure that we are only
    # using variables having 'Name' in their names
    if 'Name' in variable: 
        # Getting the variable's value
        value = eval(variable)
        # If the value is not duplicate
        if value not in names:
            # Append it to our final names list
            names.append(value)

for name in names:
    print (name)

Output

Dorian
Tom
Jessica
Jerry

Explanation

The locals() function returns a dictionary containing the variables defined in the local namespace. Calling locals() in the global namespace is same as calling globals() and returns a dictionary representing the global namespace of the module.

use eval()

>>> Name01 = "Dorian"
>>> Name02 = "Tom"
>>> Name04 = "Jerry"
>>> Name03 = "Jessica"
>>> for i in range(1, 100):
...   print(eval('Name%02d'%i))
... 
Dorian
Tom
Jessica
Jerry

incase if you are using 3.7+ you can go with f string

f"Name{i:02d}"
Related