Created
February 27, 2021 14:24
-
-
Save DrJackilD/9a7d180c55aa5240a1e226d0abe5ead6 to your computer and use it in GitHub Desktop.
The usage of new pattern matching in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Response: | |
def __init__(self, content: dict, status: int): | |
self.content = content | |
self.status = status | |
def make_request(content: dict, status: int) -> Response: | |
return Response(content, status) | |
def pattern_check(): | |
r = make_request({"status": "OK", "data": {"value": 10}}, 200) | |
match r: | |
case Response(status=200, content={"status": "OK", "data": {"value": v}}) if v < 10: | |
print("the value within the normal range") | |
case _: | |
print("overcome!") | |
def regular_check(): | |
r = make_request({"status": "OK", "data": {"value": 10}}, 200) | |
if r.status == 200: | |
if r.content["status"] == "OK": | |
if r.content["data"]["value"] < 10: | |
print("the value within the normal range") | |
else: | |
print("overcome!") | |
if __name__ == '__main__': | |
pattern_check() | |
regular_check() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment