Skip to content

Commit 9f0533b

Browse files
committed
Downloadable_code
1 parent 14b870c commit 9f0533b

22 files changed

Lines changed: 399 additions & 0 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Working With the Python operator Module
2+
3+
This folder contains supplementary code for the Real Python tutorial on [Working With the Python operator Module](https://realpython.com/python-operator-module/). You can copy the code examples, or continue your learning by experimenting more with them.
4+
5+
## Setup
6+
7+
Create and activate a virtual environment.
8+
9+
```bash
10+
$ python -m venv venv
11+
$ source venv/bin/activate
12+
```
13+
14+
For most of the examples, you'll use the `operator`, `pickle`, 'dataclasses' and `timeit` modules. All of these come built in to Python. No additional installation is necessary.
15+
16+
## Usage
17+
18+
After creating and activating your virtual environment, you should be able to run each individual file normally:
19+
20+
```bash
21+
(venv) $ python filename.py
22+
```
23+
24+
You can find more information and context on the code blocks in [Working With the Python operator Module](https://realpython.com/python-operator-module/).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class Musician:
6+
id: int
7+
fname: str
8+
lname: str
9+
group: str
10+
11+
12+
musicians_list = [
13+
[1, "Brian", "Wilson", "Beach Boys"],
14+
[2, "Carl", "Wilson", "Beach Boys"],
15+
[3, "Dennis", "Wilson", "Beach Boys"],
16+
[4, "Bruce", "Johnston", "Beach Boys"],
17+
[5, "Hank", "Marvin", "Shadows"],
18+
[6, "Bruce", "Welch", "Shadows"],
19+
[7, "Brian", "Bennett", "Shadows"],
20+
]
21+
22+
group_members = []
23+
24+
for musician in musicians_list:
25+
group_members.append(Musician(*musician))
26+
27+
import operator
28+
29+
from dataclasses import dataclass
30+
31+
32+
@dataclass
33+
class Musician:
34+
id: int
35+
fname: str
36+
lname: str
37+
group: str
38+
39+
40+
musicians_list = [
41+
[1, "Brian", "Wilson", "Beach Boys"],
42+
[2, "Carl", "Wilson", "Beach Boys"],
43+
[3, "Dennis", "Wilson", "Beach Boys"],
44+
[4, "Bruce", "Johnston", "Beach Boys"],
45+
[5, "Hank", "Marvin", "Shadows"],
46+
[6, "Bruce", "Welch", "Shadows"],
47+
[7, "Brian", "Bennett", "Shadows"],
48+
]
49+
50+
group_members = []
51+
52+
for musician in musicians_list:
53+
group_members.append(Musician(*musician))
54+
55+
import operator
56+
57+
get_id = operator.attrgetter("id")
58+
59+
print(min(group_members, key=get_id))
60+
print(max(group_members, key=get_id))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class Musician:
6+
id: int
7+
fname: str
8+
lname: str
9+
group: str
10+
11+
12+
musicians_list = [
13+
[1, "Brian", "Wilson", "Beach Boys"],
14+
[2, "Carl", "Wilson", "Beach Boys"],
15+
[3, "Dennis", "Wilson", "Beach Boys"],
16+
[4, "Bruce", "Johnston", "Beach Boys"],
17+
[5, "Hank", "Marvin", "Shadows"],
18+
[6, "Bruce", "Welch", "Shadows"],
19+
[7, "Brian", "Bennett", "Shadows"],
20+
]
21+
22+
group_members = []
23+
24+
for musician in musicians_list:
25+
group_members.append(Musician(*musician))
26+
27+
import operator
28+
29+
# Returning a single attribute.
30+
get_fname = operator.attrgetter("fname")
31+
32+
for person in group_members:
33+
print(get_fname(person))
34+
35+
# Returning multiple attributes.
36+
get_id_lname = operator.attrgetter("id", "lname")
37+
38+
for person in group_members:
39+
print(get_id_lname(person))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class Musician:
6+
id: int
7+
fname: str
8+
lname: str
9+
group: str
10+
11+
12+
musicians_list = [
13+
[1, "Brian", "Wilson", "Beach Boys"],
14+
[2, "Carl", "Wilson", "Beach Boys"],
15+
[3, "Dennis", "Wilson", "Beach Boys"],
16+
[4, "Bruce", "Johnston", "Beach Boys"],
17+
[5, "Hank", "Marvin", "Shadows"],
18+
[6, "Bruce", "Welch", "Shadows"],
19+
[7, "Brian", "Bennett", "Shadows"],
20+
]
21+
22+
group_members = []
23+
24+
for musician in musicians_list:
25+
group_members.append(Musician(*musician))
26+
27+
import operator
28+
29+
# Sorting on a single attribute.
30+
31+
get_id = operator.attrgetter("id")
32+
for musician in sorted(group_members, key=get_id, reverse=True):
33+
print(musician)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# De-serialize the function unsuccessfully.
2+
3+
import pickle
4+
5+
with open("calculate_modulus.pkl", "rb") as f:
6+
unpickled_modulus = pickle.load(f)
7+
8+
print(unpickled_modulus(7, 4))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# De-serialize the function successfully.
2+
3+
import pickle
4+
5+
6+
def calculate_modulus(operand1, operand2):
7+
return operand1 % operand2
8+
9+
10+
with open("calculate_modulus.pkl", "rb") as f:
11+
unpickled_modulus = pickle.load(f)
12+
13+
print(unpickled_modulus(7, 4))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pickle
2+
3+
4+
def calculate_modulus(operand1, operand2):
5+
return operand1 % operand2
6+
7+
8+
print(calculate_modulus(7, 4))
9+
10+
11+
with open("calculate_modulus.pkl", "wb") as f:
12+
pickle.dump(calculate_modulus, f)
13+
14+
# De-serialize the function.
15+
16+
with open("calculate_modulus.pkl", "rb") as f:
17+
unpickled_modulus = pickle.load(f)
18+
19+
print(unpickled_modulus(7, 4))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
musicians = [
2+
{"id": 1, "fname": "Brian", "lname": "Wilson", "group": "Beach Boys"},
3+
{"id": 2, "fname": "Carl", "lname": "Wilson", "group": "Beach Boys"},
4+
{"id": 3, "fname": "Dennis", "lname": "Wilson", "group": "Beach Boys"},
5+
{"id": 4, "fname": "Bruce", "lname": "Johnston", "group": "Beach Boys"},
6+
{"id": 5, "fname": "Hank", "lname": "Marvin", "group": "Shadows"},
7+
{"id": 6, "fname": "Bruce", "lname": "Welch", "group": "Shadows"},
8+
{"id": 7, "fname": "Brian", "lname": "Bennett", "group": "Shadows"},
9+
]
10+
11+
import operator
12+
13+
# Get a single element
14+
get_element_four = operator.itemgetter(4)
15+
print(get_element_four(musicians))
16+
17+
# Get multiple elements
18+
get_elements_one_three_five = operator.itemgetter(1, 3, 5)
19+
print(get_elements_one_three_five(musicians))
20+
21+
# Get values within elements
22+
get_names = operator.itemgetter("fname", "lname")
23+
24+
for musician in get_elements_one_three_five(musicians):
25+
print(get_names(musician))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
musicians_list = [
2+
[1, "Brian", "Wilson", "Beach Boys"],
3+
[2, "Carl", "Wilson", "Beach Boys"],
4+
[3, "Dennis", "Wilson", "Beach Boys"],
5+
[4, "Bruce", "Johnston", "Beach Boys"],
6+
[5, "Hank", "Marvin", "Shadows"],
7+
[6, "Bruce", "Welch", "Shadows"],
8+
[7, "Brian", "Bennett", "Shadows"],
9+
]
10+
11+
get_id = operator.itemgetter(0)
12+
13+
print(max(musicians_list, key=get_id))
14+
15+
print(min(musicians_list, key=get_id))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
musicians = [
2+
{"id": 1, "fname": "Brian", "lname": "Wilson", "group": "Beach Boys"},
3+
{"id": 2, "fname": "Carl", "lname": "Wilson", "group": "Beach Boys"},
4+
{"id": 3, "fname": "Dennis", "lname": "Wilson", "group": "Beach Boys"},
5+
{"id": 4, "fname": "Bruce", "lname": "Johnston", "group": "Beach Boys"},
6+
{"id": 5, "fname": "Hank", "lname": "Marvin", "group": "Shadows"},
7+
{"id": 6, "fname": "Bruce", "lname": "Welch", "group": "Shadows"},
8+
{"id": 7, "fname": "Brian", "lname": "Bennett", "group": "Shadows"},
9+
]
10+
11+
get_lname = operator.itemgetter("lname")
12+
13+
min(musicians, key=get_lname)

0 commit comments

Comments
 (0)