Spaces:
Sleeping
Sleeping
changes
Browse files- Python/phase_4/modular_programming.py +115 -153
Python/phase_4/modular_programming.py
CHANGED
@@ -39,71 +39,69 @@ def _(mo):
|
|
39 |
def _(mo):
|
40 |
mo.md(
|
41 |
"""
|
42 |
-
##
|
43 |
-
|
44 |
|
45 |
```python
|
46 |
-
#
|
47 |
-
|
48 |
-
return a + b
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
#
|
54 |
-
import
|
55 |
-
|
|
|
|
|
56 |
```
|
|
|
|
|
57 |
"""
|
58 |
)
|
59 |
return
|
60 |
|
61 |
|
62 |
@app.cell
|
63 |
-
def _(
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
@@ -115,13 +113,13 @@ def _(mo):
|
|
115 |
|
116 |
```python
|
117 |
# Import entire module
|
118 |
-
import
|
119 |
|
120 |
# Import specific functions
|
121 |
-
from
|
122 |
|
123 |
# Import with alias
|
124 |
-
import
|
125 |
```
|
126 |
"""
|
127 |
)
|
@@ -129,45 +127,33 @@ def _(mo):
|
|
129 |
|
130 |
|
131 |
@app.cell
|
132 |
-
def _(
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
135 |
|
|
|
|
|
|
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
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 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
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)
|
@@ -182,71 +168,46 @@ def _(mo):
|
|
182 |
|
183 |
|
184 |
@app.cell
|
185 |
-
def _(
|
186 |
-
|
187 |
-
|
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 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
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)
|
@@ -256,13 +217,14 @@ def _(mo):
|
|
256 |
|
257 |
Next Steps:
|
258 |
|
259 |
-
-
|
260 |
|
261 |
-
-
|
262 |
|
263 |
- Design reusable functions
|
264 |
|
265 |
""")
|
|
|
266 |
mo.callout(callout_text, kind="success")
|
267 |
return (callout_text,)
|
268 |
|
|
|
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)
|
|
|
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 |
)
|
|
|
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)
|
|
|
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)
|
|
|
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 |
|