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
:
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 Enum
s. 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 Enum
s …).
The Question
I am thinking about creating all the Enum
s 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 |