enums

Is it possible to change the attribute value in the enum?

Is it possible to change the attribute value in the enum? Question: I need to reassign the attribute value in Enum. from enum import Enum class Number(Enum): number = "1" Number.number = "2" # AttributeError: cannot reassign member ‘number’ I tried to reassign the attribute, but I got: AttributeError: cannot reassign member ‘number’ Asked By: …

Total answers: 4

How to add attributes to a `enum.StrEnum`?

How to add attributes to a `enum.StrEnum`? Question: I have an enum.StrEnum, for which I want to add attributes to the elements. For example: class Fruit(enum.StrEnum): APPLE = ("Apple", { "color": "red" }) BANANA = ("Banana", { "color": "yellow" }) >>> str(Fruit.APPLE) "Apple" >>> Fruit.APPLE.color "red" How can I accomplish this? (I’m running Python 3.11.0.) …

Total answers: 1

Problem inserting value into database: Flask SQLAlchemy

Problem inserting value into database: Flask SQLAlchemy Question: This is the route for the sign-up: @auth.route(‘/sign_up’, methods=[‘GET’, ‘POST’]) def sign_up(): if request.method == ‘POST’: gender = request.form.get(‘gender’) gender_option = Gender.from_str(gender) new_user = User(gender=gender_option) db.session.add(new_user) db.session.commit() return redirect(url_for(‘auth.my_profile’)) return render_template("sign_up.html",gender_options = Gender, user=current_user ) This is the structure of the database: class Gender(enum.Enum): not_specified="Not_Specified" female="Female" male="Male" …

Total answers: 1

Way to simplify enum?

Way to simplify enum? Question: Is there a better way to initialize all this boilerplate? class Type(Enum): Null=auto() Bool=auto() Int=auto() Float=auto() Decimal=auto() String=auto() Bytes=auto() Date=auto() Time=auto() Datetime=auto() Timestamp=auto() Interval=auto() Struct=auto() Array=auto() Json=auto() I wanted to do something like the following but unfortunately it kind of screws up how Pylance works (everything shows up as an …

Total answers: 1

Subclassing Python Enum with `auto()`, string values, and an extra property

Subclassing Python Enum with `auto()`, string values, and an extra property Question: I have an Enum subclass that I would like to do two things: Use the lowercased name of the enum as the value for each item and a property called function. I don’t want to have to type this out each time, so …

Total answers: 1

How can I set a default value for an enum attribute of a recordclass dataobject?

How can I set a default value for an enum attribute of a recordclass dataobject? Question: recordclass dataobjects can handle enum attributes just fine, unless you need to set a default value, which results in a SyntaxError (as of version 0.17.5): In [1]: from enum import Enum, auto In [2]: from recordclass import dataobject In …

Total answers: 2

How to get the index of a dataclass field

How to get the index of a dataclass field Question: Say I have a simple dataclass instance import dataclasses as dc @dc.dataclass class DCItem: name: str unit_price: float item = DCItem(‘test’, 11) Now I want to determine the position (index) of instance attribute item.unit_price. How can I make it simple to use and performant? I …

Total answers: 1

Indexing Python dict with string keys using Enums

Indexing Python dict with string keys using Enums Question: Is there a way to index a dict using an enum? e.g. I have the following Enum and dict: class STATUS(Enum): ACTIVE = "active" d = { "active": 1 } I’d like to add the appropriate logic to the class STATUS in order to get the …

Total answers: 2

Python Enum with multiple attributes

Python Enum with multiple attributes Question: I have his Enum : class MultiAttr(Enum): RED = (1, 10) GREEN = (2, 20) BLUE = (3, 20) def __init__(self, id, val): self.id = id self.val = val assert MultiAttr((2, 20)) == MultiAttr.GREEN Assertion passes. Why Pylance in VSCode complains Argument missing for parameter "val" ? As the …

Total answers: 1