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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "marimo",
|
| 5 |
+
# ]
|
| 6 |
+
# ///
|
| 7 |
+
|
| 8 |
+
import marimo
|
| 9 |
+
|
| 10 |
+
__generated_with = "0.10.16"
|
| 11 |
+
app = marimo.App()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.cell
|
| 15 |
+
def _():
|
| 16 |
+
import marimo as mo
|
| 17 |
+
return (mo,)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.cell(hide_code=True)
|
| 21 |
+
def _(mo):
|
| 22 |
+
mo.md(
|
| 23 |
+
"""
|
| 24 |
+
# 🧩 Modular Programming in Python
|
| 25 |
+
|
| 26 |
+
Unlock the power of organized, reusable, and maintainable code!
|
| 27 |
+
|
| 28 |
+
## Why Modular Programming?
|
| 29 |
+
- Break complex problems into smaller, manageable pieces
|
| 30 |
+
- Improve code readability
|
| 31 |
+
- Enhance code reusability
|
| 32 |
+
- Easier debugging and maintenance
|
| 33 |
+
"""
|
| 34 |
+
)
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@app.cell(hide_code=True)
|
| 39 |
+
def _(mo):
|
| 40 |
+
mo.md(
|
| 41 |
+
"""
|
| 42 |
+
## Standard Library Imports
|
| 43 |
+
Python's standard library provides powerful, pre-built modules:
|
| 44 |
+
|
| 45 |
+
```python
|
| 46 |
+
# String manipulation
|
| 47 |
+
import string
|
| 48 |
+
|
| 49 |
+
# Operating system interactions
|
| 50 |
+
import os
|
| 51 |
+
|
| 52 |
+
# Date and time handling
|
| 53 |
+
import datetime
|
| 54 |
+
|
| 55 |
+
# Mathematical operations
|
| 56 |
+
import math
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
For more details, check the [Python Standard Library Documentation](https://docs.python.org/3/library/)
|
| 60 |
+
"""
|
| 61 |
+
)
|
| 62 |
+
return
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@app.cell
|
| 66 |
+
def _():
|
| 67 |
+
# importing and using standard library modules
|
| 68 |
+
import string
|
| 69 |
+
import os
|
| 70 |
+
import datetime
|
| 71 |
+
import math
|
| 72 |
+
|
| 73 |
+
# Example of using imported modules
|
| 74 |
+
def demonstrate_standard_library_usage():
|
| 75 |
+
# String module: get all punctuation
|
| 76 |
+
punctuation_example = string.punctuation
|
| 77 |
+
|
| 78 |
+
# OS module: get current working directory
|
| 79 |
+
current_dir = os.getcwd()
|
| 80 |
+
|
| 81 |
+
# Datetime module: get current date
|
| 82 |
+
today = datetime.date.today()
|
| 83 |
+
|
| 84 |
+
# Math module: calculate square root
|
| 85 |
+
sqrt_example = math.sqrt(16)
|
| 86 |
+
|
| 87 |
+
return {
|
| 88 |
+
"Punctuation": punctuation_example,
|
| 89 |
+
"Current Directory": current_dir,
|
| 90 |
+
"Today's Date": today,
|
| 91 |
+
"Square Root Example": sqrt_example
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
# Run the demonstration
|
| 95 |
+
module_usage_examples = demonstrate_standard_library_usage()
|
| 96 |
+
module_usage_examples
|
| 97 |
+
return (
|
| 98 |
+
datetime,
|
| 99 |
+
demonstrate_standard_library_usage,
|
| 100 |
+
math,
|
| 101 |
+
module_usage_examples,
|
| 102 |
+
os,
|
| 103 |
+
string,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@app.cell(hide_code=True)
|
| 108 |
+
def _(mo):
|
| 109 |
+
mo.md(
|
| 110 |
+
"""
|
| 111 |
+
## Import Strategies
|
| 112 |
+
Multiple ways to import and use modules:
|
| 113 |
+
|
| 114 |
+
```python
|
| 115 |
+
# Import entire module
|
| 116 |
+
import math
|
| 117 |
+
|
| 118 |
+
# Import specific functions
|
| 119 |
+
from math import sqrt, pow
|
| 120 |
+
|
| 121 |
+
# Import with alias
|
| 122 |
+
import math as m
|
| 123 |
+
```
|
| 124 |
+
"""
|
| 125 |
+
)
|
| 126 |
+
return
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
@app.cell
|
| 130 |
+
def _():
|
| 131 |
+
def demonstrate_import_strategies():
|
| 132 |
+
"""
|
| 133 |
+
Demonstrate different import strategies using the math module
|
| 134 |
+
"""
|
| 135 |
+
# Strategy 1: Import entire module
|
| 136 |
+
import math
|
| 137 |
+
entire_module_result = math.sqrt(25)
|
| 138 |
+
|
| 139 |
+
# Strategy 2: Import specific functions
|
| 140 |
+
from math import pow, sqrt
|
| 141 |
+
specific_import_result = pow(2, 3)
|
| 142 |
+
|
| 143 |
+
# Strategy 3: Import with alias
|
| 144 |
+
import math as m
|
| 145 |
+
alias_result = m.sqrt(16)
|
| 146 |
+
|
| 147 |
+
return {
|
| 148 |
+
"Entire Module Import": entire_module_result,
|
| 149 |
+
"Specific Function Import": specific_import_result,
|
| 150 |
+
"Alias Import": alias_result
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
# Run the import strategy demonstration
|
| 154 |
+
import_strategy_examples = demonstrate_import_strategies()
|
| 155 |
+
import_strategy_examples
|
| 156 |
+
return demonstrate_import_strategies, import_strategy_examples
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
@app.cell(hide_code=True)
|
| 160 |
+
def _(mo):
|
| 161 |
+
mo.md(
|
| 162 |
+
"""
|
| 163 |
+
## Code Reusability
|
| 164 |
+
Create functions that can be used across different parts of your project
|
| 165 |
+
"""
|
| 166 |
+
)
|
| 167 |
+
return
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@app.cell
|
| 171 |
+
def _():
|
| 172 |
+
def generate_reusable_functions():
|
| 173 |
+
"""
|
| 174 |
+
Demonstrate different types of reusable functions
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
def process_text(text):
|
| 178 |
+
'''Reusable text processing function'''
|
| 179 |
+
return text.strip().lower()
|
| 180 |
+
|
| 181 |
+
def normalize_number(value, min_val=0, max_val=100):
|
| 182 |
+
'''Normalize a number to a specific range'''
|
| 183 |
+
return max(min_val, min(max_val, value))
|
| 184 |
+
|
| 185 |
+
def validate_input(value, type_check=str, min_length=1):
|
| 186 |
+
'''Validate input based on type and minimum length'''
|
| 187 |
+
if not isinstance(value, type_check):
|
| 188 |
+
return False
|
| 189 |
+
return len(str(value)) >= min_length
|
| 190 |
+
|
| 191 |
+
# usage
|
| 192 |
+
return {
|
| 193 |
+
"Text Processing": {
|
| 194 |
+
"Example 1": process_text(" John Doe "),
|
| 195 |
+
"Example 2": process_text(" [email protected] ")
|
| 196 |
+
},
|
| 197 |
+
"Number Normalization": {
|
| 198 |
+
"Oversized Input": normalize_number(150),
|
| 199 |
+
"Negative Input": normalize_number(-10, min_val=-20, max_val=50)
|
| 200 |
+
},
|
| 201 |
+
"Input Validation": {
|
| 202 |
+
"Username Validation": validate_input("john"),
|
| 203 |
+
"Age Validation": validate_input(25, type_check=int)
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
# Run the reusable functions demonstration
|
| 208 |
+
reusable_function_examples = generate_reusable_functions()
|
| 209 |
+
reusable_function_examples
|
| 210 |
+
return generate_reusable_functions, reusable_function_examples
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@app.cell(hide_code=True)
|
| 214 |
+
def _(mo):
|
| 215 |
+
callout_text = mo.md("""
|
| 216 |
+
## Your Modular Programming Journey!
|
| 217 |
+
|
| 218 |
+
Next Steps:
|
| 219 |
+
|
| 220 |
+
- Explore Python's standard library
|
| 221 |
+
|
| 222 |
+
- Practice different import strategies
|
| 223 |
+
|
| 224 |
+
- Design reusable functions
|
| 225 |
+
|
| 226 |
+
""")
|
| 227 |
+
|
| 228 |
+
mo.callout(callout_text, kind="success")
|
| 229 |
+
return (callout_text,)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
if __name__ == "__main__":
|
| 233 |
+
app.run()
|