« Back to Index

Enum Example

View original Gist on GitHub

Tags: #python

enum example.py

"""
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