Issue
So I'm trying to achieve something like this
from enum import Enum
tabulate_formats = ['fancy_grid', 'fancy_outline', 'github', 'grid']
class TableFormat(str, Enum):
for item in tabulate_formats:
exec(f"{item} = '{item}'")
Though i get this error
Traceback (most recent call last):
File "/app/src/main.py", line 25, in <module>
class TableFormat(str, Enum):
File "/app/src/main.py", line 26, in TableFormat
for item in tabulate_formats:
File "/usr/local/lib/python3.6/enum.py", line 92, in __setitem__
raise TypeError('Attempted to reuse key: %r' % key)
TypeError: Attempted to reuse key: 'item'
How do I properly assign them into the class
Solution
Not sure if this would also be acceptable, but based on turning a dict
into an Enum
, you could do:
TableFormat = Enum('TableFormat', {i:i for i in tabulate_formats})
Answered By - Tom
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.