Python Enum : achieve multiple attributes with IntelliSense through code generation (jinja2)

CONTEXT

My project is about implementing the “conventional method for house-energy-efficiency assessment” in France (120 pages).

This method, comes with a huge .xls file providing definition for 10’s of enum-like data structure (see sample table below)

I want to model this data as a Python Enum with multiple attributes. Each table always has an id column (int|str) and a label column (str). Possibly several other columns are present as in the sample table (see below the hors_methode column).

I already know how to achieve that using a modified aenum.Enum :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from aenum import Enum
class BaseEnumWithProps(Enum):
def __str__(self):
return self.label
@classmethod
def _missing_value_(cls, value):
"""
Permet d'instancier une Enum à partir de son label (str) ou de son id (int)
"""
if isinstance(value, int):
for member in cls:
if member.id == value:
return member
if isinstance(value, str):
for member in cls:
if member.label == value:
return member
class TypePlancherBas(BaseEnumWithProps):
_init_ = "id, label, hors_methode"
ID_1 = 1, "Inconnu", 0
"""Inconnu"""
ID_2 = 2, "Plancher avec ou sans remplissage", 0
"""Plancher avec ou sans remplissage"""
# ...
ID_13 = 13, "Autre type de plancher non répertorié", 1
"""Autre type de plancher non répertorié"""
pb_1 = TypePlancherBas.ID_1
pb_2 = TypePlancherBas.ID_2
print(pb_1.label)
print(pb_1.hors_methode)
</code>
<code>from aenum import Enum class BaseEnumWithProps(Enum): def __str__(self): return self.label @classmethod def _missing_value_(cls, value): """ Permet d'instancier une Enum à partir de son label (str) ou de son id (int) """ if isinstance(value, int): for member in cls: if member.id == value: return member if isinstance(value, str): for member in cls: if member.label == value: return member class TypePlancherBas(BaseEnumWithProps): _init_ = "id, label, hors_methode" ID_1 = 1, "Inconnu", 0 """Inconnu""" ID_2 = 2, "Plancher avec ou sans remplissage", 0 """Plancher avec ou sans remplissage""" # ... ID_13 = 13, "Autre type de plancher non répertorié", 1 """Autre type de plancher non répertorié""" pb_1 = TypePlancherBas.ID_1 pb_2 = TypePlancherBas.ID_2 print(pb_1.label) print(pb_1.hors_methode) </code>
from aenum import Enum

class BaseEnumWithProps(Enum):

    def __str__(self):
        return self.label
    
    @classmethod
    def _missing_value_(cls, value):
        """
        Permet d'instancier une Enum à partir de son label (str) ou de son id (int)
        """
        if isinstance(value, int):
            for member in cls:
                if member.id == value:
                    return member
        if isinstance(value, str):
            for member in cls:
                if member.label == value:
                    return member 
    

class TypePlancherBas(BaseEnumWithProps):
  
  _init_ = "id, label, hors_methode"
  
  ID_1 = 1, "Inconnu", 0
  """Inconnu"""
  ID_2 = 2, "Plancher avec ou sans remplissage", 0
  """Plancher avec ou sans remplissage"""
  # ...
  ID_13 = 13, "Autre type de plancher non répertorié", 1
  """Autre type de plancher non répertorié"""

pb_1 = TypePlancherBas.ID_1
pb_2 = TypePlancherBas.ID_2

print(pb_1.label)
print(pb_1.hors_methode)

The Problem

The problem with this way of doing things is because TypePlancherBas is dynamically created (through metaclass), there is no way to get IntelliSense working on Enums. vscose will never tell me that label or id is an attribute of TypePlancherBas.ID_XX. This is bad for code discovery and usability of the API I am building (remember there are 10’s of such Enums …).

The Question

I am thinking about creating all the Enums through code generation : a python script (run once for ever) coud process the .xls file and build a enums.py module gathering all the Enum’s (through templating with jinja2).

What would be the proper class structure for such an Enum (see sample below) to make the code discoverable by IntelliSense ?

I’ve tried countless options but could not reach my goal …

Related posts :
/a/59417442/7060811
Python Enum: How to get enum values with multiple attributes

Sample Table : type_plancher_bas

id label hors_methode
1 Inconnu 0
2 Plancher avec ou sans remplissage 0
3 Plancher entre solives métalliques avec ou sans remplissage 0
4 Plancher entre solives bois avec ou sans remplissage 0
5 Plancher bois sur solives métalliques 0
6 Bardeaux et remplissage 0
7 Voutains sur solives métalliques 0
8 Voutains en briques ou moellons 0
9 Dalle béton 0
10 Plancher bois sur solives bois 0
11 Plancher lourd type entrevous terre-cuite, poutrelles béton 0
12 Plancher à entrevous isolant 0
13 Autre type de plancher non répertorié 1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật