Robert Smith
Robert Smith

Reputation: 309

Iteratively declare variables based on string?

Not sure if this has been asked before or not. Its a bit of an odd question, so I'll go ahead and fire away.

I've got some variable (or rather constant) definitions:

# Constants

# Colors
RED="RED"
ORANGE="ORANGE"
YELLOW="YELLOW"
GREEN="GREEN"
CYAN="CYAN"
BLUE="BLUE"
MAGENTA="MAGENTA"
# Modes
PANIC="PANIC"
SOLID="SOLID"
BREATHING="BREATHING"
# Special sub-modes (for panic)
BLINKING="BLINKING"

# Declare them
SOLID_RED="{}_{}".format(SOLID,RED)
SOLID_BLUE="{}_{}".format(SOLID,BLUE)
SOLID_MAGENTA="{}_{}".format(SOLID,MAGENTA)
## ..
BREATHING_RED="{}_{}".format(BREATHING,RED)
BREATHING_BLUE="{}_{}".format(BREATHING,BLUE)
BREATHING_MAGENTA="{}_{}".format(BREATHING,MAGENTA)
## ..
PANIC_RED="{}_{}".format(PANIC,RED)
PANIC_BLUE="{}_{}".format(PANIC,BLUE)
PANIC_MAGENTA="{}_{}".format(PANIC,MAGENTA)
## ..
PANIC_BLINKING="{}_{}".format(PANIC,BLINKING)

I got a lot of definitions! Instead of having to type them all out like this, would there be a way for me to just construct all these constants into existence as strings only using the definitions BEFORE # declare them , or by using, say, a dictionary?

The format I'd need for such a iterative construction is: MODE_COLOR naming convention.

I require that this answer works using Python 2.7. As I have some dependent 2.7 APIs included.

Upvotes: 1

Views: 131

Answers (3)

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

Another way using itertools.combinations and locals():

from itertools import combinations
from pprint import pprint

# Colors
RED="RED"
ORANGE="ORANGE"
YELLOW="YELLOW"
GREEN="GREEN"
CYAN="CYAN"
BLUE="BLUE"
MAGENTA="MAGENTA"
# Modes
PANIC="PANIC"
SOLID="SOLID"
BREATHING="BREATHING"
# Special sub-modes (for panic)
BLINKING="BLINKING"

v_consts = {k:v for k, v in locals().items() if k.isupper()}
combs = combinations(v_consts.values(), 2)
d_consts = {'%s_%s' % k: '%s_%s' % k for k  in combs}
pprint(d_consts)

# Edit:
# If you want to add the created variables in Python's scope
# You can do something like this
globals().update(d_consts)
print SOLID_BLINKING, type(SOLID_BLINKING)

Output:

{'BLINKING_CYAN': 'BLINKING_CYAN',
 'BLINKING_MAGENTA': 'BLINKING_MAGENTA',
 'BLINKING_ORANGE': 'BLINKING_ORANGE',
 'BLINKING_PANIC': 'BLINKING_PANIC',
 'BLINKING_RED': 'BLINKING_RED',
  ...
 'YELLOW_MAGENTA': 'YELLOW_MAGENTA',
 'YELLOW_ORANGE': 'YELLOW_ORANGE',
 'YELLOW_PANIC': 'YELLOW_PANIC',
 'YELLOW_RED': 'YELLOW_RED'}
 SOLID_BLINKING <type 'str'>

Upvotes: 2

erip
erip

Reputation: 16935

I think what you're trying to do is emitting a bit of a code smell.

The way I might approach this is by using a dictionary and a cross product. Here's a minified example:

from itertools import product

A = ['a', 'b', 'c']
B = ['d', 'e', 'f']

AB = {"{0} {1}".format(a, b): "{0}_{1}".format(a, b) for a, b in product(A, B)}

print(AB)

You can apply this to your colors and modifiers and access the colors by name:

colors['Magenta Solid']

Upvotes: 1

Alexander
Alexander

Reputation: 109546

I would use a dictionary as the container to store the variables. Just list all of the colors and modes in lists, and then use a dictionary comprehension:

colors_list = ['red', 'blue']
modes_list = ['panic', 'solid']
color_modes = {k1 + '_' + k2: k1.upper() + '_' + k2.upper()
               for k1 in colors_list for k2 in modes_list}
>>> color_modes
{'blue_panic': 'BLUE_PANIC',
 'blue_solid': 'BLUE_SOLID',
 'red_panic': 'RED_PANIC',
 'red_solid': 'RED_SOLID'}

Upvotes: 2

Related Questions