Skip to content

Commit c9f78e1

Browse files
committed
enumeration in Python
1 parent 1fce429 commit c9f78e1

File tree

2 files changed

+285
-1
lines changed

2 files changed

+285
-1
lines changed

P-E-enumeration.ipynb

+283
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "42a3fd00",
6+
"metadata": {},
7+
"source": [
8+
"# Python Everything\n",
9+
"- 32) **Enumeration in python**: Using the *enum* module in the standard library\n",
10+
"<br> https://github.com/ostad-ai/Python-Everything\n",
11+
"<br>https://www.pinterest.com/HamedShahHosseini/programming-languages/python"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "c361779f",
17+
"metadata": {},
18+
"source": [
19+
"**Enumeration**: is a set of symbolic names (we call them members) that each is bounded to a unique value. \n",
20+
"- We can iterate over the members of an enumeration.\n",
21+
"- We can access the members by dot-syntax, function call-syntax, or index-syntax. "
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"id": "1f01481f",
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"# importing the required module\n",
32+
"from enum import Enum,auto,Flag"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"id": "7013971c",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"# creating an enumeration with class syntax:\n",
43+
"class Color(Enum):\n",
44+
" RED=1\n",
45+
" GREEN=2\n",
46+
" BLUE=3 \n",
47+
"# creating the enumeraton above using functional syntax:\n",
48+
"# Color=Enum('Color',['RED','GREEN',\"BLUE\"])"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 3,
54+
"id": "5932e426",
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"Name of the member: RED and its value: 1\n"
62+
]
63+
}
64+
],
65+
"source": [
66+
"# each memeber of an enumeration has *name* and *value* attributes\n",
67+
"# its name is of type string\n",
68+
"# its value is the value assigned in the enumeration\n",
69+
"print(f'Name of the member: {Color.RED.name} and its value: {Color.RED.value}')"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 4,
75+
"id": "5a5ede4b",
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"name": "stdout",
80+
"output_type": "stream",
81+
"text": [
82+
"Color.RED\n",
83+
"Color.RED\n",
84+
"Color.RED\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"# accessing members of enumeration class\n",
90+
"# by dot-syntax using the member\n",
91+
"print(Color.RED)\n",
92+
"# by call-syntax using value of a member\n",
93+
"print(Color(1))\n",
94+
"# by index-syntax using name of a member\n",
95+
"print(Color['RED'])"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 5,
101+
"id": "3f012ff4",
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"name": "stdout",
106+
"output_type": "stream",
107+
"text": [
108+
"Name of the member: RED and its value: 1\n",
109+
"Name of the member: GREEN and its value: 2\n",
110+
"Name of the member: BLUE and its value: 3\n"
111+
]
112+
}
113+
],
114+
"source": [
115+
"# to iterate over the members of an enumeration\n",
116+
"for member in Color:\n",
117+
" print(f'Name of the member: {member.name} and its value: {member.value}')"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 6,
123+
"id": "7a4eb61c",
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"name": "stdout",
128+
"output_type": "stream",
129+
"text": [
130+
"Name of the member: RED and its value: 1\n",
131+
"Name of the member: GREEN and its value: 2\n",
132+
"Name of the member: BLUE and its value: 3\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"# assign value automatically by auto()\n",
138+
"# Hint: auto() returns the highest member value incremented by 1\n",
139+
"class Color2(Enum):\n",
140+
" RED=auto()\n",
141+
" GREEN=auto()\n",
142+
" BLUE=auto()\n",
143+
"for member in Color2:\n",
144+
" print(f'Name of the member: {member.name} and its value: {member.value}')"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 7,
150+
"id": "6b15a78c",
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"name": "stdout",
155+
"output_type": "stream",
156+
"text": [
157+
"purple does not contain green\n",
158+
"purple contains blue\n",
159+
"value of purple is: 5\n",
160+
"color name is: RED, and its value is: 1\n",
161+
"color name is: GREEN, and its value is: 2\n",
162+
"color name is: BLUE, and its value is: 4\n"
163+
]
164+
}
165+
],
166+
"source": [
167+
"#Flag is the same as Enum, but its members support the bitwise operators\n",
168+
"#The bitwise oeprations are: & (AND), | (OR), ^ (XOR), and ~ (INVERT)\n",
169+
"# values with auto() for Flag are given in powers of two\n",
170+
"class ColorF(Flag):\n",
171+
" RED=auto()\n",
172+
" GREEN=auto()\n",
173+
" BLUE=auto()\n",
174+
"yellow=ColorF.RED | ColorF.GREEN\n",
175+
"cyan=ColorF.GREEN | ColorF.BLUE\n",
176+
"purple=ColorF.RED | ColorF.BLUE\n",
177+
"white = ColorF.RED | ColorF.GREEN | ColorF.BLUE\n",
178+
"if ColorF.GREEN in purple:\n",
179+
" print('purple contains green')\n",
180+
"else:\n",
181+
" print('purple does not contain green')\n",
182+
"if ColorF.BLUE in purple:\n",
183+
" print('purple contains blue')\n",
184+
"else:\n",
185+
" print('purple does not contain blue')\n",
186+
"print(f'value of purple is: {purple.value}')\n",
187+
"for color in ColorF:\n",
188+
" print(f'color name is: {color.name}, and its value is: {color.value}')"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": 8,
194+
"id": "75e03eae",
195+
"metadata": {},
196+
"outputs": [
197+
{
198+
"data": {
199+
"text/plain": [
200+
"(False, True)"
201+
]
202+
},
203+
"execution_count": 8,
204+
"metadata": {},
205+
"output_type": "execute_result"
206+
}
207+
],
208+
"source": [
209+
"# we can compare enumeration members in two ways:\n",
210+
"# identity: using *is* and *is not* oeprators\n",
211+
"# equality: using \"==\" and \"!=\" operators\n",
212+
"class Color(Enum):\n",
213+
" RED=auto()\n",
214+
" GREEN=auto()\n",
215+
" BLUE=auto()\n",
216+
" \n",
217+
"Color.RED==Color.BLUE, Color.RED!=Color.GREEN"
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": 9,
223+
"id": "29d32507",
224+
"metadata": {},
225+
"outputs": [
226+
{
227+
"name": "stdout",
228+
"output_type": "stream",
229+
"text": [
230+
"Today is FRIDAY\n"
231+
]
232+
}
233+
],
234+
"source": [
235+
"# example showing the weekday name of the current date\n",
236+
"from datetime import date\n",
237+
"\n",
238+
"class Day(Enum):\n",
239+
" MONDAY=auto()\n",
240+
" TUESDAY=auto()\n",
241+
" WEDNESDAY=auto()\n",
242+
" THURSDAY=auto()\n",
243+
" FRIDAY=auto()\n",
244+
" SATURDAY=auto()\n",
245+
" SUNDAY=auto()\n",
246+
" @classmethod\n",
247+
" def today(cls):\n",
248+
" return f'Today is {cls(date.today().isoweekday()).name}'\n",
249+
" \n",
250+
"print(Day.today())"
251+
]
252+
},
253+
{
254+
"cell_type": "code",
255+
"execution_count": null,
256+
"id": "f0220e8c",
257+
"metadata": {},
258+
"outputs": [],
259+
"source": []
260+
}
261+
],
262+
"metadata": {
263+
"kernelspec": {
264+
"display_name": "Python 3 (ipykernel)",
265+
"language": "python",
266+
"name": "python3"
267+
},
268+
"language_info": {
269+
"codemirror_mode": {
270+
"name": "ipython",
271+
"version": 3
272+
},
273+
"file_extension": ".py",
274+
"mimetype": "text/x-python",
275+
"name": "python",
276+
"nbconvert_exporter": "python",
277+
"pygments_lexer": "ipython3",
278+
"version": "3.11.1"
279+
}
280+
},
281+
"nbformat": 4,
282+
"nbformat_minor": 5
283+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030
28. Functions: We will have a closer look at functions. Calling functions by-reference or by-value are mentioned. Functions may have a variable number of arguments. Also, functions may have a variable number of keyword arguments. Some example are provided for clarification.
3131
29. Generator functions and expressions: By using keyword **yield** in a function, instead of keyword **return**, we get a generator function. We see some examples of how to use generator functions. Also, we introduce *generator expressions* and see their difference with list comprehensions.
3232
30. Handling files: We create, read, write files (text or binary) with **open**. We can also append data to an existing file. Generally, in this post, we mention how to deal with files in Python. We talk shortly about encoding and decoding from character string to byte string and vice versa. Moreove, we can check if a file exists or not.
33-
31. Python Exercise: Compute the probability that a set of integers less than or equal to some certain numbers are all divisible by a common divisor.
33+
31. Python Exercise: Compute the probability that a set of integers less than or equal to some certain numbers are all divisible by a common divisor.
34+
32. Enumeration in Python: We can use symbolic names in Python using enumerations. For this purpose, we employ module *enum* of the Python standard library. Enumeration is reviewed via some examples.

0 commit comments

Comments
 (0)