Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,322 changes: 3,322 additions & 0 deletions python-inner-functions/NYC_Wi-Fi_Hotspot_Locations.csv

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions python-inner-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python Inner Functions: What Are They Good For?

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/).
15 changes: 15 additions & 0 deletions python-inner-functions/debugging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def debug(func):
def _debug(*args, **kwargs):
result = func(*args, **kwargs)
print(f"{func.__name__}(args: {args}, kwargs: {kwargs}) -> {result}")
return result

return _debug


@debug
def add(a, b):
return a + b


print(add(5, 6))
15 changes: 15 additions & 0 deletions python-inner-functions/factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def factorial(number):
if not isinstance(number, int):
raise TypeError("number must be an integer")
if number < 0:
raise ValueError("number must be zero or positive")

def inner_factorial(number):
if number <= 1:
return 1
return number * inner_factorial(number - 1)

return inner_factorial(number)


print(factorial(4))
26 changes: 26 additions & 0 deletions python-inner-functions/hotspots_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import csv
from collections import Counter
from io import TextIOWrapper


def process_hotspots(file: str | TextIOWrapper):
hotspots = []

if isinstance(file, str):
with open(file, "r") as csv_file:
for row in csv.DictReader(csv_file):
hotspots.append(row["Provider"])
else:
with file as csv_file:
for row in csv.DictReader(csv_file):
hotspots.append(row["Provider"])

provider, count = Counter(hotspots).most_common(1)[0]
print(
f"There are {len(hotspots)} Wi-Fi hotspots in NYC.\n"
f"{provider} has the most with {count}."
)


if __name__ == "__main__":
process_hotspots("NYC_Wi-Fi_Hotspot_Locations.csv")
24 changes: 24 additions & 0 deletions python-inner-functions/hotspots_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import csv
from collections import Counter
from io import TextIOWrapper


def process_hotspots(file: str | TextIOWrapper):
hotspots = []

def extract_hotspots(csv_file):
for row in csv.DictReader(csv_file):
hotspots.append(row["Provider"])

if isinstance(file, str):
with open(file, "r") as csv_file:
extract_hotspots(csv_file)
else:
with file as csv_file:
extract_hotspots(csv_file)

provider, count = Counter(hotspots).most_common(1)[0]
print(
f"There are {len(hotspots)} Wi-Fi hotspots in NYC.\n"
f"{provider} has the most with {count}."
)
17 changes: 17 additions & 0 deletions python-inner-functions/hotspots_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import csv
from collections import Counter
from io import TextIOWrapper


def process_hotspots(file: str | TextIOWrapper):
if isinstance(file, str):
file = open(file, "r")

with file as csv_file:
hotspots = [row["Provider"] for row in csv.DictReader(csv_file)]

provider, count = Counter(hotspots).most_common(1)[0]
print(
f"There are {len(hotspots)} Wi-Fi hotspots in NYC.\n"
f"{provider} has the most with {count}."
)
16 changes: 16 additions & 0 deletions python-inner-functions/mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def mean():
sample = []

def inner_mean(number):
sample.append(number)
return sum(sample) / len(sample)

return inner_mean


sample_mean = mean()

print(sample_mean(100))
print(sample_mean(105))
print(sample_mean(101))
print(sample_mean(98))
15 changes: 15 additions & 0 deletions python-inner-functions/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def add_messages(func):
def _add_messages():
print("This is my first decorator")
func()
print("Bye!")

return _add_messages


@add_messages
def greet():
print("Hello, World!")


greet()
34 changes: 34 additions & 0 deletions python-inner-functions/point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def make_point(x, y):
def point():
print(f"Point({x}, {y})")

def get_x():
return x

def get_y():
return y

def set_x(value):
nonlocal x
x = value

def set_y(value):
nonlocal y
y = value

# Attach getters and setters
point.get_x = get_x
point.set_x = set_x
point.get_y = get_y
point.set_y = set_y
return point


point = make_point(1, 2)
print(point.get_x())
print(point.get_y())
point()

point.set_x(42)
point.set_y(7)
point()
32 changes: 32 additions & 0 deletions python-inner-functions/powers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# def generate_power(exponent):
# def power(base):
# return base**exponent

# return power


def generate_power(exponent):
def power(func):
def inner_power(*args):
base = func(*args)
return base**exponent

return inner_power

return power


@generate_power(2)
def square(n):
return n


print(square(7))


@generate_power(3)
def cube(n):
return n


print(cube(5))