Haleshot commited on
Commit
9988d70
·
unverified ·
1 Parent(s): 770a2df

Refactor function design according to comments

Browse files
Files changed (1) hide show
  1. Python/phase_4/function_design.py +43 -54
Python/phase_4/function_design.py CHANGED
@@ -73,49 +73,40 @@ def _(mo):
73
 
74
 
75
  @app.cell
76
- def _(default_age):
77
- default_age
78
- return
79
-
80
-
81
- @app.cell
82
- def _(mo):
83
- # Interactive function (default parameter demo)
84
- default_age = mo.ui.number(value=18, start=0, stop=120, label="Default Age")
85
- return (default_age,)
86
-
87
-
88
- @app.cell
89
- def _(default_age):
90
- def create_profile(name, age=default_age.value):
91
  return f"{name} is {age} years old"
92
 
 
93
  example_name = "Alex"
94
- return create_profile, example_name
95
-
96
-
97
- @app.cell
98
- def _(create_profile, example_name):
99
- create_profile(example_name)
100
- return
101
 
102
 
103
  @app.cell
104
- def _(first_param, mo, second_param):
105
- mo.hstack([first_param, second_param])
106
- return
 
 
 
107
 
 
 
 
 
108
 
109
- @app.cell
110
- def _(mo):
111
- # Multiple parameters interactive function demo
112
- first_param = mo.ui.number(value=10, start=0, stop=100, label="First Number")
113
- second_param = mo.ui.number(value=5, start=0, stop=100, label="Second Number")
114
- return first_param, second_param
115
 
116
 
117
  @app.cell
118
- def _(first_param, second_param):
119
  def calculate(a, b):
120
  """
121
  Perform multiple calculations on two numbers.
@@ -134,8 +125,11 @@ def _(first_param, second_param):
134
  "max": max(a, b)
135
  }
136
 
137
- result = calculate(first_param.value, second_param.value)
138
- return calculate, result
 
 
 
139
 
140
 
141
  @app.cell(hide_code=True)
@@ -169,20 +163,7 @@ def _(mo):
169
 
170
 
171
  @app.cell
172
- def _(temperature):
173
- temperature
174
- return
175
-
176
-
177
- @app.cell
178
- def _(mo):
179
- # Multiple return values and how they are captured
180
- temperature = mo.ui.number(value=25, start=-50, stop=50, label="Temperature")
181
- return (temperature,)
182
-
183
-
184
- @app.cell
185
- def _(temperature):
186
  def weather_analysis(temp):
187
  """
188
  Analyze weather based on temperature.
@@ -202,17 +183,25 @@ def _(temperature):
202
  else:
203
  return "Hot", "Stay hydrated", "High"
204
 
205
- analysis = weather_analysis(temperature.value)
206
- return analysis, weather_analysis
 
 
 
 
 
 
 
 
207
 
208
 
209
  @app.cell(hide_code=True)
210
- def _(mo, weather_analysis):
211
  mo.md(f"""
212
- ## Multiple Return Demonstration
213
 
214
- Current Temperature Analysis:
215
- {weather_analysis(25)}
216
  """)
217
  return
218
 
 
73
 
74
 
75
  @app.cell
76
+ def _():
77
+ # Function with default parameter
78
+ def create_profile(name, age=18):
 
 
 
 
 
 
 
 
 
 
 
 
79
  return f"{name} is {age} years old"
80
 
81
+ # Example usage
82
  example_name = "Alex"
83
+ example_profile = create_profile(example_name)
84
+ example_profile
85
+ return create_profile, example_name, example_profile
 
 
 
 
86
 
87
 
88
  @app.cell
89
+ def _():
90
+ # Show closure over values
91
+ base_multiplier = 2
92
+ def multiplier(x):
93
+ """
94
+ Create a function that multiplies input by a base value.
95
 
96
+ This demonstrates how functions can 'close over'
97
+ values from their surrounding scope.
98
+ """
99
+ return x * base_multiplier
100
 
101
+ # Example of using the closure
102
+ sample_numbers = [1, 2, 3, 4, 5]
103
+ multiplied_numbers = [multiplier(num) for num in sample_numbers]
104
+ print(multiplied_numbers)
105
+ return base_multiplier, multiplied_numbers, multiplier, sample_numbers
 
106
 
107
 
108
  @app.cell
109
+ def _():
110
  def calculate(a, b):
111
  """
112
  Perform multiple calculations on two numbers.
 
125
  "max": max(a, b)
126
  }
127
 
128
+ # Example usage with concrete values
129
+ first_number = 10
130
+ second_number = 5
131
+ result = calculate(first_number, second_number)
132
+ return calculate, first_number, result, second_number
133
 
134
 
135
  @app.cell(hide_code=True)
 
163
 
164
 
165
  @app.cell
166
+ def _():
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  def weather_analysis(temp):
168
  """
169
  Analyze weather based on temperature.
 
183
  else:
184
  return "Hot", "Stay hydrated", "High"
185
 
186
+ # Example temperature analysis
187
+ temperature = 25
188
+ status, recommendation, warning_level = weather_analysis(temperature)
189
+ return (
190
+ recommendation,
191
+ status,
192
+ temperature,
193
+ warning_level,
194
+ weather_analysis,
195
+ )
196
 
197
 
198
  @app.cell(hide_code=True)
199
+ def _(mo, recommendation, status, warning_level):
200
  mo.md(f"""
201
+ ## Function Results
202
 
203
+ Calculation Results:
204
+ {status}, {recommendation}, {warning_level}
205
  """)
206
  return
207