Haleshot commited on
Commit
6b3e45b
·
unverified ·
1 Parent(s): f982dd5
Files changed (1) hide show
  1. 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
- ## 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)
@@ -115,13 +113,13 @@ def _(mo):
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
  )
@@ -129,45 +127,33 @@ def _(mo):
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)
@@ -182,71 +168,46 @@ def _(mo):
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)
@@ -256,13 +217,14 @@ def _(mo):
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
 
 
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