You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum Generator: allow passing strings for parameters which are enums to make calling more pythonic #21
in turn parameter format is of Enum type defined in databricks.sdk.service.workspace.ExportFormat as
classExportFormat(Enum):
"""This specifies the format of the file to be imported. By default, this is `SOURCE`. However it may be one of: `SOURCE`, `HTML`, `JUPYTER`, `DBC`. The value is case sensitive."""DBC='DBC'HTML='HTML'JUPYTER='JUPYTER'R_MARKDOWN='R_MARKDOWN'SOURCE='SOURCE'
changed the title [-]allow passing strings for parameters which are enums to make calling more pythonic[/-][+]Enum Generator: allow passing strings for parameters which are enums to make calling more pythonic[/+]on Mar 1, 2023
fromenumimportEnumclassYetAnotherStrEnum(Enum):
@classmethoddeffrom_str(cls, s: str):
returncls.__members__.get(s)
@classmethoddeffrom_enum_or_str(cls, s_or_e):
ifisinstance(s_or_e, str):
returncls.from_str(s_or_e)
else:
returns_or_eclassExportFormat(YetAnotherStrEnum):
"""This specifies the format of the file to be imported. By default, this is `SOURCE`. However it may be one of: `SOURCE`, `HTML`, `JUPYTER`, `DBC`. The value is case sensitive."""DBC='DBC'HTML='HTML'JUPYTER='JUPYTER'R_MARKDOWN='R_MARKDOWN'SOURCE='SOURCE'aaa='bbb'assertExportFormat.from_str('SOURCE') ==ExportFormat.SOURCEassertExportFormat.from_enum_or_str('SOURCE') ==ExportFormat.SOURCEassertExportFormat.from_enum_or_str(ExportFormat.SOURCE) ==ExportFormat.SOURCE
Activity
grusin-db commentedon Mar 1, 2023
I am not sure what is the neat/correct way of solving it, SO has all sort of answers (https://stackoverflow.com/questions/41407414/convert-string-to-enum-in-python)
this stands out as a neat one: https://stackoverflow.com/a/56567247
[-]allow passing strings for parameters which are enums to make calling more pythonic[/-][+]Enum Generator: allow passing strings for parameters which are enums to make calling more pythonic[/+]grusin-db commentedon Mar 1, 2023
hence calling code can be change from:
to:
this way both
ExportFormatEnum andstrwould be handled.grusin-db commentedon Mar 1, 2023
Another issue
...with displaying/repr for classes having enums, displayed text has nasty looking
object_typeandlanguage, making copy & paste unfriendlydisplayed text should look like this, so it can be copied and run to created object back:
Solution:
adding custom string formatter:
gets rid of nasty EnumType.FieldName: FieldValue formatting, in favour of just EnumType.FieldName