structural-pattern-matching Questions

4

Solved

I'm trying to use SPM to determine if a certain type is an int or an str. The following code: from typing import Type def main(type_to_match: Type): match type_to_match: case str(): print(&quot...
Rambling asked 26/1, 2022 at 11:26

6

Solved

I have a string that I'm trying to validate against a few regex patterns and I was hoping since Pattern matching is available in 3.10, I might be able to use that instead of creating an if-else blo...
Corrosion asked 12/1, 2022 at 10:58

6

Solved

In my code, I need to distinguish a list of records from a list of lists of records. The existing code does it like so: if isinstance(input_var, list): if len(input_var) > 0: if all(isinstance...

4

Solved

Python recently has released match-case in version 3.10. The question is how can we do a default case in Python? I can do if/elif but don't know how to do else. Below is the code: x = "hello&q...
Burrows asked 16/8, 2021 at 14:6

2

Solved

Why does this code fail: OKAY = 200 NOT_FOUND = 404 INTERNAL_SERVER_ERROR = 500 match status: case OKAY: print('It worked') case NOT_FOUND: print('Unknown') case INTERNAL_SERVER_ERROR: print...

6

Solved

I'm trying to understand the new structural pattern matching syntax in Python 3.10. I understand that it is possible to match on literal values like this: def handle(retcode): match retcode: case...

2

Solved

I want to convert this existing code to use pattern matching: if isinstance(x, int): pass elif isinstance(x, str): x = int(x) elif isinstance(x, (float, Decimal)): x = round(x) else: raise Type...
Miyamoto asked 13/5, 2021 at 18:51

2

Solved

I've read about and understand floating point round-off issues such as: >>> sum([0.1] * 10) == 1.0 False >>> 1.1 + 2.2 == 3.3 False >>> sin(radians(45)) == sqrt(2) / 2 ...

1

Solved

I need to add gettext translation to all the string literals in our code, but it doesn't work with literals in case statements. This failed attempt gives SyntaxError: Expected ':': from gettext imp...
Hollyanne asked 13/5, 2022 at 6:17

2

Solved

I've been playing around with Structural Pattern Matching in Python 3.10 and can't figure out how to get it to match a set. For example I've tried: a = {1,2,3} match a: case set(1,2,3): print('m...
Kalisz asked 3/6, 2021 at 20:11

1

Solved

I need to match cases where the input is iterable. Here's what I tried: from typing import Iterable def detector(x: Iterable | int | float | None) -> bool: match x: case Iterable(): print('I...
Involucrum asked 1/3, 2022 at 19:41

1

Solved

I'm trying to run an example from the docs, but get the error: Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: Point() accepts 0 positional ...
Gaddi asked 19/10, 2021 at 8:39

1

This has to do with the new Python 3.10 beta and the new match syntax. Is there any way to check if a pattern is simply contained in an iterable? the most obvious solution, to simply put two wildca...

1

Solved

This example is being discussed as likely "gotcha" when using pattern matching: NOT_FOUND = 400 retcode = 200 match retcode: case NOT_FOUND: print('not found') print(f'Current value ...

1

Solved

I have code that checks for named tuples and dataclasses by looking for a _fields attribute: if hasattr(candidate, '_fields'): do_action() How can I express this with Python 3.10's match/case str...
Hershelhershell asked 13/5, 2021 at 16:50

4

I'm not able to run this code: match shape: case Point(x, y): ... case Rectangle(x, y, _, _): ... print(x, y) I can't find the match keyword in Python. I found it here: https://www.python.org/...
1

© 2022 - 2024 — McMap. All rights reserved.