Skip to content

Commit 682378f

Browse files
lpozobzaczynski
andauthored
Sample code for the article on inner functions (#711)
* Sample code for the article on inner functions * Final QA --------- Co-authored-by: Bartosz Zaczyński <[email protected]>
1 parent 89ef3f8 commit 682378f

File tree

11 files changed

+3519
-0
lines changed

11 files changed

+3519
-0
lines changed

python-inner-functions/NYC_Wi-Fi_Hotspot_Locations.csv

Lines changed: 3322 additions & 0 deletions
Large diffs are not rendered by default.

python-inner-functions/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Inner Functions: What Are They Good For?
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Inner Functions: What Are They Good For?](https://realpython.com/inner-functions-what-are-they-good-for/).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def debug(func):
2+
def _debug(*args, **kwargs):
3+
result = func(*args, **kwargs)
4+
print(f"{func.__name__}(args: {args}, kwargs: {kwargs}) -> {result}")
5+
return result
6+
7+
return _debug
8+
9+
10+
@debug
11+
def add(a, b):
12+
return a + b
13+
14+
15+
print(add(5, 6))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def factorial(number):
2+
if not isinstance(number, int):
3+
raise TypeError("number must be an integer")
4+
if number < 0:
5+
raise ValueError("number must be zero or positive")
6+
7+
def inner_factorial(number):
8+
if number <= 1:
9+
return 1
10+
return number * inner_factorial(number - 1)
11+
12+
return inner_factorial(number)
13+
14+
15+
print(factorial(4))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import csv
2+
from collections import Counter
3+
from io import TextIOWrapper
4+
5+
6+
def process_hotspots(file: str | TextIOWrapper):
7+
hotspots = []
8+
9+
if isinstance(file, str):
10+
with open(file, "r") as csv_file:
11+
for row in csv.DictReader(csv_file):
12+
hotspots.append(row["Provider"])
13+
else:
14+
with file as csv_file:
15+
for row in csv.DictReader(csv_file):
16+
hotspots.append(row["Provider"])
17+
18+
provider, count = Counter(hotspots).most_common(1)[0]
19+
print(
20+
f"There are {len(hotspots)} Wi-Fi hotspots in NYC.\n"
21+
f"{provider} has the most with {count}."
22+
)
23+
24+
25+
if __name__ == "__main__":
26+
process_hotspots("NYC_Wi-Fi_Hotspot_Locations.csv")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import csv
2+
from collections import Counter
3+
from io import TextIOWrapper
4+
5+
6+
def process_hotspots(file: str | TextIOWrapper):
7+
hotspots = []
8+
9+
def extract_hotspots(csv_file):
10+
for row in csv.DictReader(csv_file):
11+
hotspots.append(row["Provider"])
12+
13+
if isinstance(file, str):
14+
with open(file, "r") as csv_file:
15+
extract_hotspots(csv_file)
16+
else:
17+
with file as csv_file:
18+
extract_hotspots(csv_file)
19+
20+
provider, count = Counter(hotspots).most_common(1)[0]
21+
print(
22+
f"There are {len(hotspots)} Wi-Fi hotspots in NYC.\n"
23+
f"{provider} has the most with {count}."
24+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csv
2+
from collections import Counter
3+
from io import TextIOWrapper
4+
5+
6+
def process_hotspots(file: str | TextIOWrapper):
7+
if isinstance(file, str):
8+
file = open(file, "r")
9+
10+
with file as csv_file:
11+
hotspots = [row["Provider"] for row in csv.DictReader(csv_file)]
12+
13+
provider, count = Counter(hotspots).most_common(1)[0]
14+
print(
15+
f"There are {len(hotspots)} Wi-Fi hotspots in NYC.\n"
16+
f"{provider} has the most with {count}."
17+
)

python-inner-functions/mean.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def mean():
2+
samples = []
3+
4+
def inner_mean(number):
5+
samples.append(number)
6+
return sum(samples) / len(samples)
7+
8+
return inner_mean
9+
10+
11+
sample_means = mean()
12+
13+
print(sample_means(100))
14+
print(sample_means(105))
15+
print(sample_means(101))
16+
print(sample_means(98))

python-inner-functions/message.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def add_messages(func):
2+
def _add_messages():
3+
print("This is my first decorator")
4+
func()
5+
print("Bye!")
6+
7+
return _add_messages
8+
9+
10+
@add_messages
11+
def greet():
12+
print("Hello, World!")
13+
14+
15+
greet()

python-inner-functions/point.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def make_point(x, y):
2+
def point():
3+
print(f"Point({x}, {y})")
4+
5+
def get_x():
6+
return x
7+
8+
def get_y():
9+
return y
10+
11+
def set_x(value):
12+
nonlocal x
13+
x = value
14+
15+
def set_y(value):
16+
nonlocal y
17+
y = value
18+
19+
# Attach getters and setters
20+
point.get_x = get_x
21+
point.set_x = set_x
22+
point.get_y = get_y
23+
point.set_y = set_y
24+
return point
25+
26+
27+
point = make_point(1, 2)
28+
print(point.get_x())
29+
print(point.get_y())
30+
point()
31+
32+
point.set_x(42)
33+
point.set_y(7)
34+
point()

0 commit comments

Comments
 (0)