enums

unexpected behavior with EnumMeta class

unexpected behavior with EnumMeta class Question: I defined an IntEnum for some weather codes and provided a metaclass. from enum import IntEnum, EnumMeta class WXCodeMeta(EnumMeta): def __iter__(self: type[IntEnum]) -> Iterator[tuple[str, int]]: for member in super().__iter__(): yield member.name, member.value def names(self: type[IntEnum]): return tuple(member.name for member in super().__iter__()) def values(self: type[IntEnum]): return tuple(member.value for member in …

Total answers: 1

json.dumps() encodes StrEnum with custom __new__ to empty string

json.dumps() encodes StrEnum with custom __new__ to empty string Question: I have an enum similar to the following: from enum import Enum class Currencies(str, Enum): EURO = ("EUR", True) YEN = ("JPY", False) supports_decimals: bool def __new__(cls, value: str, supports_decimals: bool): obj = super().__new__(cls) obj._value_ = value setattr(obj, "supports_decimals", supports_decimals) return obj This allows things …

Total answers: 1

How to get value or name of enum from SQLAlchemy result query?

How to get value or name of enum from SQLAlchemy result query? Question: I want to build an API for my project and return everything as JSON using Flask and SQLAlchemy. Unfortunately, SQLAlchemy did not return the query as JSON Seriazeble, so I’m using data classes to solve that problem. The code working and it …

Total answers: 2

Python / Python.NET 3.0 / OSIsoft / Enum-related error

Python.Runtime.PythonException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value) Question: I recently moved my code to a new computer at work. A basic example code is as follows (but you wouldn’t be able to run it as you can’t connect to my server – sorry that I couldn’t make it …

Total answers: 2

Dynamic enum values on nested classes with Python

Dynamic enum values on nested classes with Python Question: Consider the following enum class: from enum import Enum class Namespace: class StockAPI(Enum): ITEMS = "{url}/items" INVENTORY = "{url}/inventory" class CustomerAPI(Enum): USERS = "{url}/users" PURCHASES = "{url}/purchases" def __init__(self, url): self.url = url I am trying to make url a dynamic value for each enum class. …

Total answers: 2

Create an Enum using a custom mix-in type

Create an Enum using a custom mix-in type Question: In the relevant section of the Python Enum documentation, it is mentioned that mix-in types can be used to ensure items belong to that type and act as objects of that type, like in the example: class IntEnum(int, Enum): pass However, the documentation does not give …

Total answers: 1

Python dynamic enum

Python dynamic enum Question: I would like to create dynamic enums in python loaded from a SQL Table. The output of SQL will be a list of tuplets, which with I want to fill the attributes of the enum. Lets say I receive this list: lst = [(‘PROCESS_0’, 0, "value", 123, False), (‘PROCESS_1’,1,"anothervalue", 456, True)] …

Total answers: 2

f-string representation different than str()

f-string representation different than str() Question: I had always thought that f-strings invoked the __str__ method. That is, f'{x}’ was always the same as str(x). However, with this class class Thing(enum.IntEnum): A = 0 f'{Thing.A}’ is ‘0’ while str(Thing.A) is ‘Thing.A’. This example doesn’t work if I use enum.Enum as the base class. What functionality …

Total answers: 2

Python – How to get Enum value by index

Python – How to get Enum value by index Question: I have an Enum of days_of_the week in Python: class days_of_the_week(str, Enum): monday = ‘monday’ tuesday = ‘tuesday’ wednesday = ‘wednesday’ thursday = ‘thursday’ friday = ‘friday’ saturday = ‘saturday’ sunday = ‘sunday’ I want to access the value using the index. I’ve tried: days_of_the_week.value[index] …

Total answers: 2

Python Enum: How to get enum values with multiple attributes

Python Enum: How to get enum values with multiple attributes Question: I have defined two enums with attributes. When I want to access an enum element by specifying its attribute it works for enum A (one attribute) but not for enum B (two attributes): from enum import Enum class A(Enum): ValOne = (‘One’) ValTwo = …

Total answers: 2