Skip to content

Commit 16bb9c6

Browse files
authored
Merge pull request #417 from realpython/working-with-the-python-operator-module
Add code for operators tutorial
2 parents 14b870c + 9375930 commit 16bb9c6

24 files changed

+368
-0
lines changed

python-operator-module/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
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.
8+
9+
## Usage
10+
11+
You can run each individual file with your local Python interpreter:
12+
13+
```bash
14+
$ python filename.py
15+
```
16+
17+
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import operator
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class Musician:
7+
id: int
8+
fname: str
9+
lname: str
10+
group: str
11+
12+
13+
musician_lists = [
14+
[1, "Brian", "Wilson", "Beach Boys"],
15+
[2, "Carl", "Wilson", "Beach Boys"],
16+
[3, "Dennis", "Wilson", "Beach Boys"],
17+
[4, "Bruce", "Johnston", "Beach Boys"],
18+
[5, "Hank", "Marvin", "Shadows"],
19+
[6, "Bruce", "Welch", "Shadows"],
20+
[7, "Brian", "Bennett", "Shadows"],
21+
]
22+
23+
group_members = []
24+
25+
for musician in musician_lists:
26+
group_members.append(Musician(*musician))
27+
28+
get_id = operator.attrgetter("id")
29+
30+
print(min(group_members, key=get_id))
31+
print(max(group_members, key=get_id))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import operator
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class Musician:
7+
id: int
8+
fname: str
9+
lname: str
10+
group: str
11+
12+
13+
musician_lists = [
14+
[1, "Brian", "Wilson", "Beach Boys"],
15+
[2, "Carl", "Wilson", "Beach Boys"],
16+
[3, "Dennis", "Wilson", "Beach Boys"],
17+
[4, "Bruce", "Johnston", "Beach Boys"],
18+
[5, "Hank", "Marvin", "Shadows"],
19+
[6, "Bruce", "Welch", "Shadows"],
20+
[7, "Brian", "Bennett", "Shadows"],
21+
]
22+
23+
group_members = []
24+
25+
for musician in musician_lists:
26+
group_members.append(Musician(*musician))
27+
28+
# Returning a single attribute.
29+
get_fname = operator.attrgetter("fname")
30+
31+
for person in group_members:
32+
print(get_fname(person))
33+
34+
# Returning multiple attributes.
35+
get_id_lname = operator.attrgetter("id", "lname")
36+
37+
for person in group_members:
38+
print(get_id_lname(person))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import operator
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class Musician:
7+
id: int
8+
fname: str
9+
lname: str
10+
group: str
11+
12+
13+
musician_lists = [
14+
[1, "Brian", "Wilson", "Beach Boys"],
15+
[2, "Carl", "Wilson", "Beach Boys"],
16+
[3, "Dennis", "Wilson", "Beach Boys"],
17+
[4, "Bruce", "Johnston", "Beach Boys"],
18+
[5, "Hank", "Marvin", "Shadows"],
19+
[6, "Bruce", "Welch", "Shadows"],
20+
[7, "Brian", "Bennett", "Shadows"],
21+
]
22+
23+
group_members = []
24+
25+
for musician in musician_lists:
26+
group_members.append(Musician(*musician))
27+
28+
# Sorting on a single attribute.
29+
30+
get_id = operator.attrgetter("id")
31+
for musician in sorted(group_members, key=get_id, reverse=True):
32+
print(musician)
45 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Deserialize 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+
# Deserialize 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+
# Deserialize 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import operator
2+
3+
musician_dicts = [
4+
{"id": 1, "fname": "Brian", "lname": "Wilson", "group": "Beach Boys"},
5+
{"id": 2, "fname": "Carl", "lname": "Wilson", "group": "Beach Boys"},
6+
{"id": 3, "fname": "Dennis", "lname": "Wilson", "group": "Beach Boys"},
7+
{"id": 4, "fname": "Bruce", "lname": "Johnston", "group": "Beach Boys"},
8+
{"id": 5, "fname": "Hank", "lname": "Marvin", "group": "Shadows"},
9+
{"id": 6, "fname": "Bruce", "lname": "Welch", "group": "Shadows"},
10+
{"id": 7, "fname": "Brian", "lname": "Bennett", "group": "Shadows"},
11+
]
12+
13+
14+
# Get a single element
15+
get_element_four = operator.itemgetter(4)
16+
print(get_element_four(musician_dicts))
17+
18+
# Get multiple elements
19+
get_elements_one_three_five = operator.itemgetter(1, 3, 5)
20+
print(get_elements_one_three_five(musician_dicts))
21+
22+
# Get values within elements
23+
get_names = operator.itemgetter("fname", "lname")
24+
25+
for musician in get_elements_one_three_five(musician_dicts):
26+
print(get_names(musician))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import operator
2+
3+
musician_lists = [
4+
[1, "Brian", "Wilson", "Beach Boys"],
5+
[2, "Carl", "Wilson", "Beach Boys"],
6+
[3, "Dennis", "Wilson", "Beach Boys"],
7+
[4, "Bruce", "Johnston", "Beach Boys"],
8+
[5, "Hank", "Marvin", "Shadows"],
9+
[6, "Bruce", "Welch", "Shadows"],
10+
[7, "Brian", "Bennett", "Shadows"],
11+
]
12+
13+
get_id = operator.itemgetter(0)
14+
15+
print(max(musician_lists, key=get_id))
16+
17+
print(min(musician_lists, key=get_id))

0 commit comments

Comments
 (0)