mirror of
https://github.com/GoodStartLabs/AI_Diplomacy.git
synced 2026-05-01 17:45:26 +00:00
The PowerEnum correctly handles some misspellings. It can be easily expanded to handle more within the _POWER_ALIASES dict.
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from enum import Enum
|
||
from typing import Any, Optional
|
||
|
||
# your “typo → canonical” map
|
||
_POWER_ALIASES = {
|
||
"EGMANY": "GERMANY",
|
||
"GERMAN": "GERMANY",
|
||
"UK": "ENGLAND",
|
||
"BRIT": "ENGLAND",
|
||
}
|
||
|
||
|
||
class PowerEnum(str, Enum):
|
||
AUSTRIA = "AUSTRIA"
|
||
ENGLAND = "ENGLAND"
|
||
FRANCE = "FRANCE"
|
||
GERMANY = "GERMANY"
|
||
ITALY = "ITALY"
|
||
RUSSIA = "RUSSIA"
|
||
TURKEY = "TURKEY"
|
||
|
||
@classmethod
|
||
def _missing_(cls, value: Any) -> Optional["Enum"]:
|
||
"""
|
||
Called when you do PowerEnum(value) and `value` isn't one of the raw enum values.
|
||
Here we normalize strings to upper‐stripped, apply aliases, then retry.
|
||
"""
|
||
if isinstance(value, str):
|
||
normalized = value.upper().strip()
|
||
# apply any synonyms/typos
|
||
normalized = _POWER_ALIASES.get(normalized, normalized)
|
||
# look up in the normal value→member map
|
||
member = cls._value2member_map_.get(normalized)
|
||
if member is not None:
|
||
return member
|
||
|
||
# by default, let Enum raise the ValueError
|
||
return super()._missing_(value)
|