Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Matching a Pattern on a Deeply Nested Data Structure

00:00 And now you’re ready to define the filter_comments() function with all the pattern matching logic. def filter_comments( ) takes in an argument events. events will be what’s returned from the fetch_ github_events() function. for event in events: match event case and here we have a large mapping pattern.

00:23 The key type, the value IssueCommentEvent.

00:28 The key created_at capture pattern when

00:33 the key actor storing the key display_login capture pattern user, the key payload storing the key action with the value created.

00:49 The key issue, storing the value state with the value open. The key title, capture pattern, issue_title, the key comment.

01:05 The key body capture pattern body. The key html_url capture pattern url. The key user storing the key html_url, capture pattern user_url.

01:34 yield comment_template.format(when=when,` body=body, url=url, user=user, user_ url=user_url, issue_title=issue_title).

01:53 Here you’re using a mapping pattern to match some very specific key-value pairs. A type key matching IssueCommentEvent, and a payload key holding a nested dictionary.

02:04 Within that dictionary, an action key with the value created and an issue key containing a further nested dictionary itself, holding a state key with the value open.

02:16 This complex pattern matches specifically to comments made on open issues. Then you use capture patterns to bind the values of interest from the matched event.

02:26 When was the comment made, the comment body, the URL to the comment, the user who made the comment, the user’s URL, and the title of the issue, all of which are then passed to the format() method of the comment_template you defined earlier.

02:40 Using the yield keyword here makes this a generator function, meaning you’ll be able to iterate over these comments.

Become a Member to join the conversation.