1

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.

8
  • 1
    Not sure how this is Python 2.7 specific. Commented Aug 12, 2017 at 18:46
  • Raspberry Pi - python 2.7 library, and using dependent py2.7 APIs. Irrelevant comment, there, buddy. Commented Aug 12, 2017 at 18:48
  • Your code runs on both Python 2 and Python 3. What it's going to be run on is irrelevant with respect to your question. Commented Aug 12, 2017 at 18:49
  • Python 3 has additional syntax in question. If you actually looked over stack overflow, sir, you'd see that solutions are provided for both versions. Because the syntax and method support is inherently different. Commented Aug 12, 2017 at 18:51
  • 1
    "If you actually looked over stack overflow" -- I've been using StackOverflow for 6 years. Commented Aug 12, 2017 at 18:52

3 Answers 3

2

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'> 
Sign up to request clarification or add additional context in comments.

6 Comments

Heh, locals is pretty cute.
@erip I'm little lazy to write the consts again lol so using locals() will save me some time.
Thanks! This was the best answer, simple 4-liner, and it preserves case. But would this start throwing things declared above it in, as well?
Oh, I see. You're checking if the locals being looped through are all uppercase before adding them. ( if k.isupper()) I see. OK wow yes solution makes perfect sense now. I was asking if you had some code above that, would that loop begin to add anything else you'd declared? But I see thats not the case, because you check to be sure its only adding an all uppercase string only.
@RobertSmith, See my last update. I've added how you can add those created variables in Python's scope in order to use them by they variable names without using the created dict itself.
|
2

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'} 

Comments

1

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'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.