Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active June 3, 2020 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/7571b0da0ca3da25f1cd170b1030f4a0 to your computer and use it in GitHub Desktop.
Save Integralist/7571b0da0ca3da25f1cd170b1030f4a0 to your computer and use it in GitHub Desktop.
[Python Enum Example] #python #python3 #enum
"""
the 'inherit from enum' class approach helps give structure to otherwise
much more verbose constants.
for example...
RenderMode.HUMAN_READABLE
RenderMode.STRUCTURED_JSON
vs
RENDER_MODE_HUMAN_READABLE
RENDER_MODE_STRUCTURED_JSON
"""
class RenderMode(Enum):
HUMAN_READABLE = 1
STRUCTURED_JSON = 2
render_mode = RenderMode.STRUCTURED_JSON if settings.get('PRINT_STRUCTURED_JSON') else RenderMode.HUMAN_READABLE
if render_mode == RenderMode.HUMAN_READABLE:
# print data in human readable format
else:
# print data for machine consumption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment