You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a class EnhancedString which basically inherits from str used for a enum created lets say Pets such that Pets.DOG will give me str representation directy 'dog' but when passing it as in dictionary as a key against a value and on dumping it is giving different output since it is understanding it as the object of EnhancedString not str but I want to dump it as if it is a str only.
class EnumDirectValueMeta(EnumMeta):
"""Metaclass to fetch the value of the enum directly."""
def __getattribute__(self, name):
value = super().__getattribute__(name)
if isinstance(value, self):
value = value.value
return value
I have a class EnhancedString which basically inherits from str used for a enum created lets say Pets such that Pets.DOG will give me str representation directy 'dog' but when passing it as in dictionary as a key against a value and on dumping it is giving different output since it is understanding it as the object of EnhancedString not str but I want to dump it as if it is a str only.
class EnumDirectValueMeta(EnumMeta):
"""Metaclass to fetch the value of the enum directly."""
class EnhancedString(str):
def new(cls, seq, display_name=None, is_deprecated=False):
instance = super().new(cls, seq)
instance.__display_name = display_name or seq.replace('_', ' ').title()
instance.__is_deprecated = is_deprecated
return instance
class EnhancedEnum(Enum, metaclass=EnumDirectValueMeta):
"""Class to facilitate the Enum creation with metaclass EnumDirectValuesMeta.
"""
pass
class StringifiedEnum(EnhancedEnum):
def new(cls, value, name=None):
instance = EnhancedString(value, display_name=name)
return instance
class Pets(StringifiedEnum):
DOG = ('Fido', 'Hey Fido!')
CAT = ('Kitty', 'Hey Kitty!')
TIGER = ('Tiger')
my_dict = {Pets.DOG : 'hey fido', 'home': 'India', 'travelled_place': ['usa','Australia']}
import yaml
isinstance(Pets.DOG , str)
type(Pets.DOG)
<class 'main.EnhancedString'>
output = yaml.dump(my_dict)
print(output)
_? !!python/object/new:main.EnhancedString
args:
state:
_EnhancedString__display_name: Hey Fido!
_EnhancedString__is_deprecated: false
_EnhancedString__seq: Fido
objclass: !!python/name:main.Pets ''
name: DOG
value: !!python/tuple
: hey fido
home: India
travelled_place:
The text was updated successfully, but these errors were encountered: