astrapy.utils.str_enum
Expand source code
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from enum import Enum
from typing import TypeVar
T = TypeVar("T", bound="StrEnum")
class StrEnum(Enum):
@classmethod
def coerce(cls: type[T], value: str | T) -> T:
"""
Accepts either a string or an instance of the Enum itself.
If a string is passed, it converts it to the corresponding
Enum value (case-insensitive).
If an Enum instance is passed, it returns it as-is.
Raises ValueError if the string does not match any enum member.
"""
if isinstance(value, cls):
return value
elif isinstance(value, str):
v_upper = value.upper()
uvalue_map = {k: v.value.upper() for k, v in cls._member_map_.items()}
if v_upper in uvalue_map:
return cls[value.upper()]
# try *value* lookup
keys = [k for k, v in uvalue_map.items() if v == v_upper]
if keys:
return cls[keys[0]]
# no matches
raise ValueError(
f"Invalid value '{value}' for {cls.__name__}. "
f"Allowed values are: {[e.value for e in cls]}"
)
raise ValueError(
f"Invalid value '{value}' for {cls.__name__}. "
f"Allowed values are: {[e.value for e in cls]}"
)
class StrEnum (*args, **kwds)
Create a collection of name/value pairs.
Example enumeration:
>>> class Color(Enum):
... RED = 1
... BLUE = 2
... GREEN = 3
Access them by:
>>> Color.RED
<Color.RED: 1>
>>> Color(1)
<Color.RED: 1>
>>> Color['RED']
<Color.RED: 1>
Enumerations can be iterated over, and know how many members they have:
>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.
Expand source codeclass StrEnum(Enum):
@classmethod
def coerce(cls: type[T], value: str | T) -> T:
"""
Accepts either a string or an instance of the Enum itself.
If a string is passed, it converts it to the corresponding
Enum value (case-insensitive).
If an Enum instance is passed, it returns it as-is.
Raises ValueError if the string does not match any enum member.
"""
if isinstance(value, cls):
return value
elif isinstance(value, str):
v_upper = value.upper()
uvalue_map = {k: v.value.upper() for k, v in cls._member_map_.items()}
if v_upper in uvalue_map:
return cls[value.upper()]
# try *value* lookup
keys = [k for k, v in uvalue_map.items() if v == v_upper]
if keys:
return cls[keys[0]]
# no matches
raise ValueError(
f"Invalid value '{value}' for {cls.__name__}. "
f"Allowed values are: {[e.value for e in cls]}"
)
raise ValueError(
f"Invalid value '{value}' for {cls.__name__}. "
f"Allowed values are: {[e.value for e in cls]}"
)
Ancestors
def coerce(value: str | T) â> ~T
Accepts either a string or an instance of the Enum itself. If a string is passed, it converts it to the corresponding Enum value (case-insensitive). If an Enum instance is passed, it returns it as-is. Raises ValueError if the string does not match any enum member.
Expand source code@classmethod
def coerce(cls: type[T], value: str | T) -> T:
"""
Accepts either a string or an instance of the Enum itself.
If a string is passed, it converts it to the corresponding
Enum value (case-insensitive).
If an Enum instance is passed, it returns it as-is.
Raises ValueError if the string does not match any enum member.
"""
if isinstance(value, cls):
return value
elif isinstance(value, str):
v_upper = value.upper()
uvalue_map = {k: v.value.upper() for k, v in cls._member_map_.items()}
if v_upper in uvalue_map:
return cls[value.upper()]
# try *value* lookup
keys = [k for k, v in uvalue_map.items() if v == v_upper]
if keys:
return cls[keys[0]]
# no matches
raise ValueError(
f"Invalid value '{value}' for {cls.__name__}. "
f"Allowed values are: {[e.value for e in cls]}"
)
raise ValueError(
f"Invalid value '{value}' for {cls.__name__}. "
f"Allowed values are: {[e.value for e in cls]}"
)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4