Skip to content

Commit bd41f5b

Browse files
committed
Move inner functions to outside scope
1 parent 36d4361 commit bd41f5b

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

ultimatepython/advanced/pattern_matching.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,42 @@ def describe_shape(shape) -> str:
166166
return "Unknown shape"
167167

168168

169+
def analyze_nested(data) -> str:
170+
"""Analyze nested structures using pattern matching."""
171+
match data:
172+
case [["pair", x, y], ["pair", a, b]]:
173+
# Match nested structure
174+
return f"Two pairs: ({x},{y}) and ({a},{b})"
175+
case [["single", val]]:
176+
return f"Single value: {val}"
177+
case _:
178+
return "Unknown structure"
179+
180+
181+
def check_value(val) -> str:
182+
"""Check value using OR patterns."""
183+
match val:
184+
case 0 | 1 | 2:
185+
# Match any of these values
186+
return "small"
187+
case 3 | 4 | 5:
188+
return "medium"
189+
case _:
190+
return "large"
191+
192+
193+
def process_range(data) -> str:
194+
"""Process range data with AS patterns."""
195+
match data:
196+
case [x, y] as pair if x < y:
197+
# Capture the entire matched value with 'as'
198+
return f"Valid range: {pair}"
199+
case [x, y]:
200+
return f"Invalid range: [{x}, {y}]"
201+
case _:
202+
return "Not a pair"
203+
204+
169205
def process_json_data(data) -> str:
170206
"""Process JSON-like dictionary data with pattern matching.
171207
@@ -275,47 +311,17 @@ def main() -> None:
275311
assert process_json_data(invalid) == "Invalid data"
276312

277313
# Pattern matching with OR patterns
278-
def check_value(val) -> str:
279-
match val:
280-
case 0 | 1 | 2:
281-
# Match any of these values
282-
return "small"
283-
case 3 | 4 | 5:
284-
return "medium"
285-
case _:
286-
return "large"
287-
288314
assert check_value(0) == "small"
289315
assert check_value(2) == "small"
290316
assert check_value(3) == "medium"
291317
assert check_value(10) == "large"
292318

293319
# Pattern matching with AS patterns (walrus-like capture)
294-
def process_range(data) -> str:
295-
match data:
296-
case [x, y] as pair if x < y:
297-
# Capture the entire matched value with 'as'
298-
return f"Valid range: {pair}"
299-
case [x, y]:
300-
return f"Invalid range: [{x}, {y}]"
301-
case _:
302-
return "Not a pair"
303-
304320
assert process_range([1, 5]) == "Valid range: [1, 5]"
305321
assert process_range([5, 1]) == "Invalid range: [5, 1]"
306322
assert process_range("not a pair") == "Not a pair"
307323

308324
# Nested pattern matching
309-
def analyze_nested(data) -> str:
310-
match data:
311-
case [["pair", x, y], ["pair", a, b]]:
312-
# Match nested structure
313-
return f"Two pairs: ({x},{y}) and ({a},{b})"
314-
case [["single", val]]:
315-
return f"Single value: {val}"
316-
case _:
317-
return "Unknown structure"
318-
319325
assert analyze_nested([["pair", 1, 2], ["pair", 3, 4]]) == "Two pairs: (1,2) and (3,4)"
320326
assert analyze_nested([["single", 42]]) == "Single value: 42"
321327
assert analyze_nested("invalid") == "Unknown structure"

0 commit comments

Comments
 (0)