I have two enums NAME and ALIAS which are guaranteed to have the same number of constants, and I need a way to convert each constant from NAME to its corresponding one from ALIAS, and vice-versa. For example:
def name_to_alias(name):
if name == Name.PAUL:
return Alias.STARCHILD
elif name == Name.GENE:
return Alias.DEMON
...
def alias_to_name(alias):
if alias == Alias.STARCHILD:
return Name.PAUL
elif alias == Alias.DEMON:
return Name.GENE
...
I don't wish to maintain two functions (or dicts) like these. Ideally, I'd have the enum mappings in a single data structure which I can access from both conversion functions.
I'm thinking of something like:
mappings = {
Name.PAUL: Alias.STARCHILD
Name.GENE: Alias.DEMON
...
}
This would work for converting from Name to Alias, but the opposite may have issues (what happens if I make a copy-paste error and end up with two dict keys with the same value?). Is there an easy and safe way to achieve this?
Aliaswith colliding member names? Perhaps the mapping isn't as symmetric as you initially suggested?NameandAlias? Is it 1-to-1, or many-to-1?def alias_to_name(alias): return [name for name, a in mappings.items() if a == alias]