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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 |
+
## Custom Modules
|
43 |
+
Create your own Python modules to organize code:
|
44 |
+
|
45 |
+
```python
|
46 |
+
# math_utils.py
|
47 |
+
def add(a, b):
|
48 |
+
return a + b
|
49 |
+
|
50 |
+
def multiply(a, b):
|
51 |
+
return a * b
|
52 |
+
|
53 |
+
# main.py
|
54 |
+
import math_utils
|
55 |
+
result = math_utils.add(5, 3)
|
56 |
+
```
|
57 |
+
"""
|
58 |
+
)
|
59 |
+
return
|
60 |
+
|
61 |
+
|
62 |
+
@app.cell
|
63 |
+
def _(module_name):
|
64 |
+
module_name
|
65 |
+
return
|
66 |
+
|
67 |
+
|
68 |
+
@app.cell
|
69 |
+
def _(mo):
|
70 |
+
# Module naming approaches
|
71 |
+
module_name = mo.ui.text(
|
72 |
+
value="math_utils",
|
73 |
+
label="Module Name"
|
74 |
+
)
|
75 |
+
return (module_name,)
|
76 |
+
|
77 |
+
|
78 |
+
@app.cell
|
79 |
+
def _(mo, module_name):
|
80 |
+
def generate_module_content(name):
|
81 |
+
"""Generate a sample module based on the name"""
|
82 |
+
return f"""
|
83 |
+
# {name}.py
|
84 |
+
def add(a, b):
|
85 |
+
'''Add two numbers'''
|
86 |
+
return a + b
|
87 |
+
|
88 |
+
def multiply(a, b):
|
89 |
+
'''Multiply two numbers'''
|
90 |
+
return a * b
|
91 |
+
|
92 |
+
def power(a, b):
|
93 |
+
'''Raise a to the power of b'''
|
94 |
+
return a ** b
|
95 |
+
"""
|
96 |
+
|
97 |
+
module_content = generate_module_content(module_name.value)
|
98 |
+
|
99 |
+
mo.md(f"""
|
100 |
+
## Module: {module_name.value}.py
|
101 |
+
|
102 |
+
```python
|
103 |
+
{module_content}
|
104 |
+
```
|
105 |
+
""")
|
106 |
+
return generate_module_content, module_content
|
107 |
+
|
108 |
+
|
109 |
+
@app.cell(hide_code=True)
|
110 |
+
def _(mo):
|
111 |
+
mo.md(
|
112 |
+
"""
|
113 |
+
## Import Strategies
|
114 |
+
Multiple ways to import and use modules:
|
115 |
+
|
116 |
+
```python
|
117 |
+
# Import entire module
|
118 |
+
import math_utils
|
119 |
+
|
120 |
+
# Import specific functions
|
121 |
+
from math_utils import add, multiply
|
122 |
+
|
123 |
+
# Import with alias
|
124 |
+
import math_utils as mu
|
125 |
+
```
|
126 |
+
"""
|
127 |
+
)
|
128 |
+
return
|
129 |
+
|
130 |
+
|
131 |
+
@app.cell
|
132 |
+
def _(import_strategy):
|
133 |
+
import_strategy
|
134 |
+
return
|
135 |
+
|
136 |
+
|
137 |
+
@app.cell(hide_code=True)
|
138 |
+
def _(mo):
|
139 |
+
# Import strategy selector
|
140 |
+
import_strategy = mo.ui.dropdown(
|
141 |
+
options=[
|
142 |
+
"Import entire module",
|
143 |
+
"Import specific functions",
|
144 |
+
"Import with alias"
|
145 |
+
],
|
146 |
+
label="Choose Import Strategy"
|
147 |
+
)
|
148 |
+
return (import_strategy,)
|
149 |
+
|
150 |
+
|
151 |
+
@app.cell
|
152 |
+
def _(import_strategy, mo):
|
153 |
+
def demonstrate_import(strategy):
|
154 |
+
if strategy == "Import entire module":
|
155 |
+
return "import math_utils\nresult = math_utils.add(5, 3)"
|
156 |
+
elif strategy == "Import specific functions":
|
157 |
+
return "from math_utils import add, multiply\nresult = add(5, 3)"
|
158 |
+
else:
|
159 |
+
return "import math_utils as mu\nresult = mu.add(5, 3)"
|
160 |
+
|
161 |
+
import_example = demonstrate_import(import_strategy.value)
|
162 |
+
|
163 |
+
mo.md(f"""
|
164 |
+
## Import examples with code
|
165 |
+
|
166 |
+
```python
|
167 |
+
{import_example}
|
168 |
+
```
|
169 |
+
""")
|
170 |
+
return demonstrate_import, import_example
|
171 |
+
|
172 |
+
|
173 |
+
@app.cell(hide_code=True)
|
174 |
+
def _(mo):
|
175 |
+
mo.md(
|
176 |
+
"""
|
177 |
+
## Code Reusability
|
178 |
+
Create functions that can be used across different parts of your project
|
179 |
+
"""
|
180 |
+
)
|
181 |
+
return
|
182 |
+
|
183 |
+
|
184 |
+
@app.cell
|
185 |
+
def _(input_type):
|
186 |
+
input_type
|
187 |
+
return
|
188 |
+
|
189 |
+
|
190 |
+
@app.cell(hide_code=True)
|
191 |
+
def _(mo):
|
192 |
+
# demo of reusability types
|
193 |
+
input_type = mo.ui.dropdown(
|
194 |
+
options=[
|
195 |
+
"String Processing",
|
196 |
+
"Number Manipulation",
|
197 |
+
"Data Validation"
|
198 |
+
],
|
199 |
+
label="Choose Reusability Scenario"
|
200 |
+
)
|
201 |
+
return (input_type,)
|
202 |
+
|
203 |
+
|
204 |
+
@app.cell
|
205 |
+
def _(input_type, mo):
|
206 |
+
def generate_reusable_function(func_type):
|
207 |
+
if func_type == "String Processing":
|
208 |
+
return """
|
209 |
+
def process_text(text):
|
210 |
+
'''Reusable text processing function'''
|
211 |
+
return text.strip().lower()
|
212 |
+
|
213 |
+
# Can be used in multiple contexts
|
214 |
+
username = process_text(" John Doe ")
|
215 |
+
email = process_text(" [email protected] ")
|
216 |
+
"""
|
217 |
+
elif func_type == "Number Manipulation":
|
218 |
+
return """
|
219 |
+
def normalize_number(value, min_val=0, max_val=100):
|
220 |
+
'''Normalize a number to a specific range'''
|
221 |
+
return max(min_val, min(max_val, value))
|
222 |
+
|
223 |
+
# Consistent number handling across the application
|
224 |
+
age = normalize_number(150) # Returns 100
|
225 |
+
temperature = normalize_number(-10, min_val=-20, max_val=50)
|
226 |
+
"""
|
227 |
+
else:
|
228 |
+
return """
|
229 |
+
def validate_input(value, type_check=str, min_length=1):
|
230 |
+
'''Validate input based on type and minimum length'''
|
231 |
+
if not isinstance(value, type_check):
|
232 |
+
return False
|
233 |
+
return len(str(value)) >= min_length
|
234 |
+
|
235 |
+
# Reusable validation across different input types
|
236 |
+
valid_username = validate_input("john")
|
237 |
+
valid_age = validate_input(25, type_check=int)
|
238 |
+
"""
|
239 |
+
|
240 |
+
reusable_code = generate_reusable_function(input_type.value)
|
241 |
+
|
242 |
+
mo.md(f"""
|
243 |
+
## Reusability Example: {input_type.value}
|
244 |
+
|
245 |
+
```python
|
246 |
+
{reusable_code}
|
247 |
+
```
|
248 |
+
""")
|
249 |
+
return generate_reusable_function, reusable_code
|
250 |
+
|
251 |
+
|
252 |
+
@app.cell(hide_code=True)
|
253 |
+
def _(mo):
|
254 |
+
callout_text = mo.md("""
|
255 |
+
## Your Modular Programming Journey!
|
256 |
+
|
257 |
+
Next Steps:
|
258 |
+
|
259 |
+
- Create your own custom modules
|
260 |
+
|
261 |
+
- Experiment with different import strategies
|
262 |
+
|
263 |
+
- Design reusable functions
|
264 |
+
|
265 |
+
""")
|
266 |
+
mo.callout(callout_text, kind="success")
|
267 |
+
return (callout_text,)
|
268 |
+
|
269 |
+
|
270 |
+
if __name__ == "__main__":
|
271 |
+
app.run()
|