8000 Merge branch 'master' into python-json · realpython/materials@7d03279 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d03279

Browse files
authored
Merge branch 'master' into python-json
2 parents f3137ea + 96e5467 commit 7d03279

31 files changed

+500
-7
lines changed

python-built-in-functions/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Built-in Functions: A Complete Exploration
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's Built-in Functions: A Complete Exploration](https://realpython.com/python-built-in-functions/).

python-built-in-functions/birds.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Duck:
2+
def fly(self):
3+
print("The duck is flying")
4+
5+
def swim(self):
6+
print("The duck is swimming")
7+
8+
9+
class Pigeon:
10+
def fly(self):
11+
print("The pigeon is flying")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
functions = [
2+
"def add(a, b): return a + b",
3+
"def subtract(a, b): return a - b",
4+
"def multiply(a, b): return a * b",
5+
"def divide(a, b): return a / b",
6+
]
7+
8+
9+
for function in functions:
10+
exec(function)
11+
12+
13+
# Uncomment the following lines
14+
# print(add(1, 2))
15+
# print(subtract(3, 2))
16+
# print(multiply(2, 3))
17+
# print(divide(6, 3))

python-built-in-functions/circle.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from time import sleep
2+
3+
SENTINEL = object()
4+
5+
6+
class Circle:
7+
def __init__(self, radius):
8+
self.radius = radius
9+
self._diameter = SENTINEL
10+
11+
@property
12+
def diameter(self):
13+
if self._diameter is SENTINEL:
14+
sleep(0.5) # Simulate a costly computation
15+
self._diameter = self.radius * 2
16+
return self._diameter
17+
18+
19+
circle = Circle(5)
20+
print(circle.diameter)

python-built-in-functions/commands.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class CommandProcessor:
2+
def __init__(self):
3+
self.commands = {}
4+
5+
def register_command(self, command):
6+
if not callable(command):
7+
raise ValueError("command is not callable")
8+
self.commands[command.__name__] = command
9+
10+
def execute_command(self, name, *args, **kwargs):
11+
if (command := self.commands.get(name)) is None:
12+
raise ValueError(f"command '{name}' not found")
13+
return command(*args, **kwargs)
14+
15+
16+
def add(a, b):
17+
return a + b
18+
19+
20+
subtract = 3 - 2
21+
22+
23+
command_processor = CommandProcessor()
24+
command_processor.register_command(add)
25+
command_processor.register_command(subtract)
26+
27+
print(command_processor.execute_command("add", 1, 2))
28+
print(command_processor.register_command(subtract))

python-built-in-functions/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"DATABASE_URL": "postgres://user:pass@localhost/dbname",
3+
"DEBUG_MODE": true,
4+
"MAX_CONNECTIONS": 10
5+
}

python-built-in-functions/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import json
2+
3+
4+
def load_config(config_file):
5+
with open(config_file) as file:
6+
config = json.load(file)
7+
8+
globals().update(config)
9+
10+
11+
load_config("config.json")
12+
print(globals())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def evaluator(expression):
2+
numbers = [2, 3, 4, 5]
3+
n = len(numbers)
4+
return eval(expression, {}, {"numbers": numbers, "n": n})
5+
6+
7+
print(evaluator("sum(numbers) / n + 100")) # 3.5

python-built-in-functions/factory.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def create_class(name, custom_members):
2+
def __init__(self, **kwargs):
3+
self.__dict__.update(kwargs)
4+
5+
def __repr__(self):
6+
return f"{name}({self.__dict__})"
7+
8+
class_members = {
9+
"__init__": __init__,
10+
"__repr__": __repr__,
11+
}
12+
class_members.update(custom_members)
13+
14+
return type(name, (), class_members)
15+
16+
17+
User = create_class("User", {"name": "", "age": 0, "email": ""})
18+
Product = create_class("Product", {"name": "", "price": 0.0, "units": 0})
19+
20+
john = User(name="John", age=30, email="john@example.com")
21+
table = Product(name="Table", price=200.0, units=5)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Fibonacci:
2+
def __init__(self):
3+
self._cache = [0, 1]
4+
5+
def __call__(self, index):
6+
if index < len(self._cache):
7+
fib_number = self._cache[index]
8+
print(f"{fib_number} id = {id(fib_number)}")
9+
else:
10+
fib_number = self(index - 1) + self(index - 2)
11+
self._cache.append(fib_number)
12+
return fib_number

0 commit comments

Comments
 (0)
0