python 3.8.2 :
I have tried to get a string, created with join
from an enum
.
import enum
# import aenum
# https://pypi.org/project/aenum/
class ELEMENTS(enum.Enum):
L = ("L", "light", "licht")
P = ("P", "power connectors", "stopcontacten")
vM = ("vM", "ventilation motor", "ventilatie motor")
cvM = ("cvM", "CV motor", "CV motor")
S = ("S", "switch", "schakelaar")
V = ("V", "valve", "klep")
ELEMENTS_list = []
print(len(ELEMENTS))
for s in ELEMENTS:
ELEMENTS_list.append(s.value[0])
regexELEMENTS = "[" + ("|".join(ELEMENTS_list)) + "]"
returns, as result :
[L|P|vM|cvM|S|V]
Update on feedback of 'VPfB' - thanks
regexELEMENTS = "(" + ("|".join(ELEMENTS_list)) + ")"
returns, the wanted result :
(L|P|vM|cvM|S|V)
By my humble opinion the code would be more proper, if I could do it immediately from the enum.
...
print("|".join(ELEMENTS.value[0]))
returns error :
... in __getattr__
raise AttributeError(name) from None
an other approach :
print("|".join(ELEMENTS))
returns error :
TypeError: sequence item 0: expected str instance, ELEMENTS found
In the second step I'll use the variable 'regexELEMENTS' in a regular expression. '{regexELEMENTS}'
Based on this question : How to use a variable inside a regular expression?
What I'm missing here ... or do I wrong?
Hope to understand, what I'm doing wrong ...
Suggestions to make the code better are more then welcome.
Thanks