Skip to content

Instantly share code, notes, and snippets.

@lkluft
Created May 8, 2019 08:17
Show Gist options
  • Save lkluft/1efee290fcc4861b2a19b62b744f2b9e to your computer and use it in GitHub Desktop.
Save lkluft/1efee290fcc4861b2a19b62b744f2b9e to your computer and use it in GitHub Desktop.
Representing cloud flags using Python enum
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-05-08T08:16:41.897218Z",
"end_time": "2019-05-08T08:16:41.959383Z"
},
"trusted": true
},
"cell_type": "code",
"source": "import enum\n\n\nclass CloudFlags(enum.Enum):\n CLEAR = 0\n CLOUDY = 1\n CONFIDENTLY_CLEAR = 2\n PROBABLY_CLEAR = 3\n PROBABLY_CLOUDY = 4\n CONFIDENTLY_CLOUDY = 5\n \n# Print the mapping both ways\nprint(repr(CloudFlags(0)))\nprint(repr(CloudFlags['CLEAR']))\n\n# Verbosely use the int value\nm = np.zeros((5, 5))\nm[1, :] = CloudFlags.PROBABLY_CLOUDY.value\nprint(m)\n\nprint(m == CloudFlags.PROBABLY_CLOUDY.value)",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "<CloudFlags.CLEAR: 0>\n<CloudFlags.CLEAR: 0>\n[[0. 0. 0. 0. 0.]\n [4. 4. 4. 4. 4.]\n [0. 0. 0. 0. 0.]\n [0. 0. 0. 0. 0.]\n [0. 0. 0. 0. 0.]]\n[[False False False False False]\n [ True True True True True]\n [False False False False False]\n [False False False False False]\n [False False False False False]]\n",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"toc_threshold": 6,
"toc_number_sections": true,
"toc_cell": false,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "Representing cloud flags using Python enum",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment