Skip to content

Commit 31f9952

Browse files
authored
Merge branch 'master' into python-311-examples
2 parents d9fec61 + f1ad275 commit 31f9952

File tree

8 files changed

+83
-0
lines changed

8 files changed

+83
-0
lines changed

python-exec/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Python's exec(): Execute Dynamically Generated Code
2+
3+
This folder provides the code examples for the article [Python's exec(): Execute Dynamically Generated Code
4+
](https://realpython.com/python-exec/).

python-exec/calculations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def add(a, b):
2+
"""Return the sum of two numbers.
3+
4+
Tests:
5+
>>> add(5, 6)
6+
11
7+
>>> add(2.3, 5.4)
8+
7.7
9+
>>> add("2", 3)
10+
Traceback (most recent call last):
11+
TypeError: numeric type expected for "a" and "b"
12+
"""
13+
if not (isinstance(a, (int, float)) and isinstance(b, (int, float))):
14+
raise TypeError('numeric type expected for "a" and "b"')
15+
return a + b

python-exec/calculations1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def add(a, b):
2+
"""Return the sum of two numbers.
3+
4+
Tests:
5+
>>> import os; os.system("ls -l")
6+
0
7+
"""
8+
if not (isinstance(a, (int, float)) and isinstance(b, (int, float))):
9+
raise TypeError('numeric type expected for "a" and "b"')
10+
return a + b

python-exec/compiled_code.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
string_input = """
2+
def sum_of_even_squares(numbers):
3+
return sum(number**2 for number in numbers if number % 2 == 0)
4+
5+
print(sum_of_even_squares(numbers))
6+
"""
7+
8+
compiled_code = compile(string_input, "<string>", "exec")
9+
exec(compiled_code)
10+
11+
numbers = [2, 3, 7, 4, 8]
12+
exec(compiled_code)
13+
14+
numbers = [5, 3, 9, 6, 1]
15+
exec(compiled_code)

python-exec/hello.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
print("Hello, Pythonista!")
2+
print("Welcome to Real Python!")
3+
4+
5+
def greet(name="World"):
6+
print(f"Hello, {name}!")

python-exec/settings.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
font_face = ""
2+
font_size = 10
3+
line_numbers = True
4+
tab_size = 4
5+
auto_indent = True

python-exec/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pathlib import Path
2+
3+
4+
def load_config(config_file):
5+
config_file = Path(config_file)
6+
code = compile(config_file.read_text(), config_file.name, "exec")
7+
config_dict = {}
8+
exec(code, {"__builtins__": {}}, config_dict)
9+
return config_dict

python-exec/string_based_code.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
code = """
2+
numbers = [2, 3, 7, 4, 8]
3+
4+
def is_even(number):
5+
return number % 2 == 0
6+
7+
even_numbers = [number for number in numbers if is_even(number)]
8+
9+
squares = [number**2 for number in even_numbers]
10+
11+
result = sum(squares)
12+
13+
print("Original data:", numbers)
14+
print("Even numbers:", even_numbers)
15+
print("Square values:", squares)
16+
print("Sum of squares:", result)
17+
"""
18+
19+
exec(code)

0 commit comments

Comments
 (0)