relima
relima

Reputation: 3423

Is there a better way to write this?

Is there a better way to do this? I feel like I am doing something wrong by being too repetitive.

O = viz.pick(1, viz.WORLD)

BackSetts = ["set_b1b", "set_b2a", "set_b1a",  "set_b2b"]
LeftSetts = ["set_l1a", "set_l1b", "set_l2a", "set_l1b"]
NormSetts = ["set_nr_a", "set_nr_b"]
Maps = ["MapA","MapB"]

if O.name in BackSetts:
    for i in set(BackSetts)|set(Maps):
        WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

elif O.name in LeftSetts:
    for i in set(LeftSetts)|set(Maps):
        WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

elif O.name in NormSetts:
    for i in NormSetts:
        WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

Upvotes: 6

Views: 187

Answers (1)

carl
carl

Reputation: 50554

The trivial transformation is:

O = viz.pick(1, viz.WORLD)

BackSetts = ["set_b1b", "set_b2a", "set_b1a",  "set_b2b"]
LeftSetts = ["set_l1a", "set_l1b", "set_l2a", "set_l1b"]
NormSetts = ["set_nr_a", "set_nr_b"]
Maps = ["MapA","MapB"]
anyset = []

if O.name in BackSetts:
    anyset = set(BackSetts)|set(Maps)

elif O.name in LeftSetts:
    anyset = set(LeftSetts)|set(Maps)

elif O.name in NormSetts:
    anyset = NormSetts

for i in anyset:
    WORLD[i].alpha(abs(WORLD[i].getAlpha()-1))

This takes care such that NormSetts is not union'd with Maps, as in your original code.

Upvotes: 3

Related Questions