-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreating_modules.py
More file actions
111 lines (86 loc) · 2.6 KB
/
Copy pathcreating_modules.py
File metadata and controls
111 lines (86 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Module: creating_modules.py
Topic: Creating Your Own Modules
Level: Intermediate
This file teaches you about:
- Creating a module
- Module structure
- __name__ == '__main__'
- Module best practices
"""
# =============================================================================
# MODULE STRUCTURE
# =============================================================================
"""
A Python module is simply a .py file that can be imported.
Best practice structure:
- Module docstring at top
- Imports grouped (stdlib, third-party, local)
- Constants
- Classes
- Functions
- __all__ definition
- if __name__ == "__main__" block
"""
# =============================================================================
# THE __NAME__ VARIABLE
# =============================================================================
print("=" * 60)
print("THE __NAME__ VARIABLE")
print("=" * 60)
print(f"""
When a module is imported, __name__ is set to the module's name.
When a module is run directly, __name__ is set to '__main__'.
Current __name__: {__name__!r}
Example:
if __name__ == "__main__":
# This only runs when file is executed directly
main()
""")
# =============================================================================
# PRACTICAL EXAMPLE: MATH UTILITIES
# =============================================================================
import math
# Constants
PI = math.pi
E = math.e
GOLDEN_RATIO = (1 + math.sqrt(5)) / 2
def is_prime(n: int) -> bool:
"""Check if a number is prime."""
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
def fibonacci(n: int) -> int:
"""Calculate the nth Fibonacci number."""
if n <= 0:
return 0
if n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
def gcd(a: int, b: int) -> int:
"""Calculate greatest common divisor."""
while b:
a, b = b, a % b
return a
# Define public API
__all__ = ['PI', 'E', 'GOLDEN_RATIO', 'is_prime', 'fibonacci', 'gcd']
print("=" * 60)
print("MATH UTILITIES MODULE DEMO")
print("=" * 60)
print(f"Is 17 prime? {is_prime(17)}")
print(f"Fibonacci(10) = {fibonacci(10)}")
print(f"GCD(48, 18) = {gcd(48, 18)}")
# =============================================================================
# MAIN EXECUTION
# =============================================================================
if __name__ == "__main__":
print("\n" + "=" * 60)
print("✅ You've learned about creating modules!")
print("=" * 60)