#100DaysOfWeb in Python Transcripts
Chapter: Days 85-88: Serverless Python with AWS Lambda
Lecture: Second Lambda: PEP8 Checker (using pycodestyle)

Login or purchase this course to watch this video and the rest of the course contents.
0:01 All right, we made our first calculator lambda. We set up some test events. We detected the AWS lambda raised its exceptions earlier than we thought.
0:14 So we adjusted the Bottle code a little bit. And we set up our AWS gateway API and that allowed to receive post requests
0:22 from our app and to invoke our lambda. So we have an end to end working solution which is cool, but the calculator at lambda is not really excited.
0:31 So let's work on a second lambda to submit code through our web form and run PEP 8 on it. While preparing this lesson I noticed
0:40 that the PEP 8 package was renamed to pycodestyle. And I've been using PEP 8 for a while. I got an error on my terminal that said
0:49 that we should really switch to this new package. So I'm going to pip install that and we're going to write our lambda. All right.
1:02 So lets start building our new lambda. Bear with me for a sec. I'm going to import a couple of libraries we're going to use, and explain to you
1:12 in a bit why. I need some constants.
1:30 And we have our default lambda handler. Actually, I can call this function anything but I'm just going to stick with lambda handler.
1:43 Default signature. All right. So, what's the deal? I order to run PEP 8 checks or pycodestyle as the new package is called, we cannot receive a string.
1:57 So we need to run it on a file. And actually, let me show you that first. For example, if we run it on our app I actually get a message that I have
2:10 one line where it expects two. So let me actually fix that on the fly. Into line 10 and we see here one blank line. And as per PEP 8 we should have two
2:23 blank lines between constants and new classes or functions. So save this, run it again. And no news is good news. This means all is okay.
2:35 So this is run from the terminal and if you want to run PEP checks from Python code, pycodestyle has a checker class we can use.
2:46 However, that checker receives a file not a string. So we're going to set up a temporary file. We cannot just save it in the current directory
2:56 because that's not writable in the AWS environment. So I'm going to make that temporary file in /temp, which is writable.
3:05 So now we're going to run the checker. The other complication is that it's output is redirected to standardout.
3:12 So we need a way to capture the standardout and throw that into a variable we then return. So that's why I'm using these modules.
3:22 Temp files are standard library module to create temporary files. So the nice thing is that files created via this method get automatically
3:32 destroyed when done. So we don't have to worry about that. And while preparing this lesson another interesting thing I found was
3:41 the redirect stdout from the context lib which will help us greatly in redirecting standard output to a variable.
3:52 With that said, lets code up our lambda. So this is the same as last time. We need to retrieve the code from the event date.
4:08 I'll just make it default of empty. And I'm going to write to our temporary file as defined in the constant. You have to give it write mode.
4:24 And give it a file handle. Now, to write the code to the temporary file. And I found that we need to add this final new line.
4:42 Otherwise it wouldn't work. And instead of opening it again for reading we can just... So we could open it again to read over the content.
4:55 But you can also use seek. Remember those tapes from the early days? That you rewind the tape. It's kind of the same.
5:03 So you can rewind it and your pointer is at the start again, and then we can consume the content. So it basically is rewriting the file.
5:16 Now the next thing that we want to do is redirect the standard output. And in the Python 3 documentation I found
5:25 this context manager that makes it pretty easy to redirect the standard output. So I can use this snippet directly in my code.
5:39 Don't use anything you already have as a file handle. So I'm going to use another variable name here. And in this body of this with statement
5:54 we're going to do the actual PEP 8 checking. So we're going to use pycodestyle which comes with a checker class
6:01 which can be initialized with the file name. So f is the file handle which has an attribute name. So f.name will be something like /temp/
6:11 some random string. And we give it showsource = True. And lets call this instance PEP. And on PEP then we can call checkall.
6:27 So this gives a bunch of output that goes to standard output. But as we're using a redirect standard output will be captured in the out variable.
6:37 Then out has a method called getvalue which gives a string of the output and we want to split that by new line. And as we saw earlier, in our web app
6:49 we then consume that content which comes back in the body. And we then join it together on new line. Actually we can just consume the string.
6:59 We might not even have to do the split and join. So let me just make this string. Default is to empty string. And then we don't need to split this.
7:17 We just take the full string. And we need to return from the lambda so we're going to return our status code of 200.
7:29 And I believe we can do that because as we saw in the previous video the lambda will raise exceptions early. So if something is wrong it would already
7:42 bail with the error message and error type we set up earlier. So we're already handling that. Getting here means all went okay.
7:59 And we're going to return the body again as expected in the web app. So we have to return the body and that will
8:09 be the output, and I don't think we really need this temporary variable. So we can just do this here. And lets do some local testing here.
8:27 So if name =. Lets do an okay scenario. So starting with some valid code.
8:48 Return = lambda handler passing in the event in empty context, and print the result.
9:05 Oh, I made a typo here. This actually has to be name.
9:23 And for we get a body empty which is as expected. That's how the command line app worked as well. Let's do a second case where we do have
9:39 a PEP 8 violation. Let's get to some indenting. Cool, so here we have the scenario. We have an E111 indentation is not a multiple of four.
9:56 So body has content, which means that would show up in the web app. I'm going to test that next. So we have a lambda.
10:07 What we now just can do is paste that into AWS. We don't need the name. We just need this body. Let's actually make a new one...
10:23 Called PEP checker. Python 3.7. Existing role. My role. Create function. And let's put this code in here.
10:41 Save. And lets set up a test event.
11:04 Okay, let's fix the quoting. Create. Test. Ah, okay. And I did expect this. There's one last thing we need to work around
11:18 and that's to make our lambda function work with external modules. As we saw at the start of this lesson we pip installed pycodestyle and in AWS
11:28 we don't have this module yet. But AWS perfectly supports uploading lambdas with external packages. And in the next video I'm going to show you
11:37 how you can do that.


Talk Python's Mastodon Michael Kennedy's Mastodon