Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #33: You should build an Alexa skill

Return to episode page view on github
Recorded on Wednesday, Jul 5, 2017.

00:00 Michael KENNEDY: Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is Episode #33, recorded July 5th, 2017. I’m Michael Kennedy.

00:00 Brian OKKEN: And I’m Brian Okken.

00:00 KENNEDY: We’ve got some pretty sweet stuff lined up for you. But Brian, before we get into that, I just want to say this episode if brought to you by Rollbar. They’ve come on to sponsor a lot of the upcoming episodes, so thank you, Rollbar.

00:00 OKKEN: Thank you, Rollbar.

00:00 KENNEDY: They would actually tell you if you had an error in your code, but wouldn’t it be better if you could actually not have errors in your production code?

00:00 OKKEN: It would be great to not have errors. So, our first topic is an article titled, “Linting as Lightweight Defect Detection for Python”. It’s a decent little tutorial walking you through using flake8. It’s a pretty short discussion. It has a link to the pycodestyle documentation.

00:00 I wanted to bring this up because I’ve been trying to clean up the code examples for the book and using flake8 a lot. I was confused. I’d used it before and I was confused a little bit because something has changed. The change is, there’s a tool called PEP8 and there’s a tool called PEP257. PEP8 checks for the PEP8 style and a few other things. PEP257 checks for docstrings style. But these things are now called pycodestyle and pydocstyle. The other tools still exist but these are some new names that the community if using. One of the reasons why that confused me is the documentation for flake8 says that it used pycodestyle but those letters are so close together. I was confused because I thought it was saying pydocstyle and I couldn’t get it to default.

00:00 KENNEDY: It doesn’t care about these comments at all.

00:00 OKKEN: Yeah, it doesn’t check comments at all.

00:00 The other thing I wanted to bring up regarding this, flake8 is a linters or a static analysis. I think we’ve talked about it a little bit before. It’s highly recommended to run your code through flake8.

00:00 KENNEDY: What I like about this article that you’ve put here is it’s not just your indentation is non-standard or the number of lines is non-standard between functions or something, but actually discovering errors as well, which is really sweet.

00:00 OKKEN: Yeah, definitely. And I’ve found problems. There is some pickiness that I still have. I’m kind of a curmudgeon about having to go in and change code because there’s no extra space at the end of my line. Like, does that really matter that much? So, I definitely recommend that people use it. Also, flake 8 has plug-ins. For instance, to do docstrings, if you install flake8-docstrings, it will check your pydocstyle, as well.

00:00 KENNEDY: Okay, that’s great. I like that article, that’s good. I really do like using linters for error checking, as well. Like, ‘Hey this variable or this parameter you passed through a function is not used. Maybe you intended to use it?’ Things like that.

00:00 So, I want to take it to entirely another end. You’re down here working on source code, I want to let you just speak like you’re in Star Trek to your computer. It would be cool if we could do that, right?

00:00 OKKEN: Yeah, definitely.

00:00 KENNEDY: A friend of the show, Jacqueline Wilson, wrote – she’s a teacher and she teaches people who are new to Python. She put together and article that’s a really friendly introduction to working with Alexa. So, this is the Amazon voice assistant thing. You can create these things called skills, which means you can say new things to them. She says, ‘Let’s put together one of these.’ And I think the walk-through is really simple and really straight-forward, but once you see how easy it is, if you have one of these devices available, you can do anything fun with it. This should really inspire you. What she built in her example was a “What’s for dinner?” bot. So, you can say, ‘Hey, Thing. What’s for dinner?’ And it will tell you. It will pick from things that you like and randomly pick something. So, all you need is an Amazon developer account, you need an AWS account and you need Python 3. You can teach it utterances. Like, ‘What should I have for dinner?’ ‘Do you have an idea for dinner?’ ‘What’s for dinner?’ You can point all of those at the same functionality. And that functionality is just a Python 3 function that lives at AWS Lambda. We talked about Lambda before. You go to AWS and you say, ‘Here I would like to write a function that just takes an input and output.’ The output is a JSON response that goes to Alexa.

00:00 OKKEN: That’s cool. I’ll have to try that.

00:00 KENNEDY: Yeah, I was actually thinking it might be fun. And you could have it more nuanced. One of my daughters is vegetarian, so it could ask, ‘Is your vegetarian daughter here?’ ‘No.’ ‘Well, you should have steak.’ (Laughs) I bet you could do some really cool things with it. If you want to play around with these ideas but you don’t have an Alexa, you can go to a website that will let you test it. You use your voice and just speak to the website instead.

00:00 OKKEN: That’s really cool. I like that.

00:00 KENNEDY: Yeah, it sounds really fun, it’s totally approachable. Basically, if you can drive all the AWS console widget switchers – there’s a lot of them and it’s kind of annoying – but if you could do that and write a single function AWS Lambda, then you are more or less ready to rock.

00:00 OKKEN: That’s great. I’ll have to check this out. Cool.

00:00 Well, next we’ve got a tool called RISE, which is Reveal IPython Slide Extension. It is a pip-installable tool for using IPython or Jupyter notebooks to create slideshows. There’s a couple videos out for how to do it. But the effect and the amount of work you have to put into it the slideshow versus how nice it looks afterwards, it’s definitely something for people to look into if they use IPython or Jupyter a lot, and need to do a slideshow for something.

00:00 KENNEDY: Cool. Awesome. Great recommendation. You know if you’re writing your research papers and presentation and written presentation already in IPython, it’s cool to just go, ‘And now it’s a presentation, too.’

00:00 OKKEN: Yeah, I haven’t had a chance to play with it too much. It looks like there’s drop-downs for different slide transitions and stuff, too. That’s neat.

00:00 KENNEDY: Yeah, it looks really cool.

00:00 Before we get to the next one, I want to tell you about Rollbar. We talked about linting. We don’t want errors in our code and if your run any sort of web application or web service, you guys should check out Rollbar.com/pythonbytes. Basically, a couple lines of code to innovate it with whatever your web framework is. It will send you notifications if there’s any sort of error with things like full stack trace, what was the user, who was running your code when that happened, how many occurrences are there, all sorts of stuff. Real time notifications for errors on your sites. I use it on all my sites and I’m totally a fan, so check it out. Rollbar.com/pythonbytes.

00:00 OKKEN: Wonderful.

00:00 KENNEDY: Do you ever run processes on remote machines, Brian? Wouldn’t it be cool if you forget to close them down and they could just fade away and not pile up on those remote servers?

00:00 OKKEN: I guess I hadn’t really thought about that. Is that an issue?

00:00 KENNEDY: Well, if you start up a bunch. What if you write some Python code and it itself used basically subprocess-type things to create a bunch of processes. If those don’t properly get closed down then that could be a problem, if one of them hangs or something.

00:00 The next one I want to cover is called Closer. Closer will let you run and basically control the remote processes over SSH. The guy who created it said it was born out of the trouble of killing the processes that he had started remotely via SSH. So, he could start them pretty easily but then they would keep running and he’s want to shut them down. So, this Closer, you can use this within Python and basically say, ‘I would like the lifetime of the subprocesses to be tied to the lifetime of my local Python program.’

00:00 OKKEN: Neat.

00:00 KENNEDY: Yeah, so the program runs and you can say, ‘Clean up.’ When I started this process remotely, you can get the output of that process back to your local machine. But you can also say, ‘When this process exits, shut that down. Automatically clean up or forcibly kill them or whatever you need to do.’ It will remotely kill all the processes either by choice or at the end of the calling process. It will capture the output. You can do live monitoring, so you can start a remote process and have it be doing a thing, and basically set up a callback for its output, a callback for when it dies. So, if it crashes before you expected somewhere along the way, things like that.

00:00 OKKEN: Yeah. And the examples show that it works within Jupyter notebooks, also.

00:00 KENNEDY: Exactly. So, yeah, it’s basically in the process, so if you exit that one then it’s gone. Closer is definitely cool.

00:00 Honestly, I don’t have that much of a use for it because I don’t remotely run processes over SSH. But I can imagine if I was doing dev ops or something, this would be a big one.

00:00 OKKEN: I don’t often have a need for that and that’s part of the problem, that I’m not doing it enough to where I’m really good at it. When I do have a need to have a couple computers working together or do a remote process, making sure that I get it all cleaned up correctly, that will be a good tool.

00:00 KENNEDY: It makes me think as well, I should probably be thinking how I could use these types of features and functionalities. (Laughs) I should probably be doing that but I’m not.

00:00 OKKEN: Speaking of things that you probably should be doing, we had this recommendation from a lot of people and I’m surprised we haven’t covered it yet. Correct me if I’m wrong, if we have covered it and I forgot.

00:00 KENNEDY: I don’t think so.

00:00 OKKEN: Okay, to there’s a checklist for Python library APIs. I’ve not taken a look at this for a while because I thought it was mostly a REST API thing and it isn’t. It’s a topic for any library API.

00:00 KENNEDY: I see. They’re actually check boxes, I can check them.

00:00 OKKEN: Yeah, but I don’t think it does anything when I check them off.

00:00 KENNEDY: It makes me feel good.

00:00 OKKEN: (Laughs) But it talks about things like simplicity, and making sure you have a README and going through some consistency thing with name conventions, looking at flexibility, looking at how you’re doing, what your abstractions are like, type safety to make sure that if somebody passes a wrong type into your library function, to make sure you raise a value error or something like that. There’s quite a few checklist things to go down. One of them we talked about before is making sure you fuse PEP8 and flake8. I, initially, was somebody that had a whole bunch of exceptions to PEP8 and flake8 but I’m doing more vanilla flake8 now.

00:00 It's a good checklist if you are deciding to put a library up. Go through this and make sure you’re doing some of the right things first.

00:00 KENNEDY: Sure. This sort of goes along with some of your guidance on your Open Source package, organization, and some of those other types of things as well. Nice.

00:00 Alright. I almost told you about Fades before, but I’m going to tell you about Fades now. Fades is not Closer. Fades is really cool. The problem of Python packaging, actually, I think is still not solved. How do we get our Python programs to people and get them to run those things reliably? There’s a lot of interesting takes. There’s pipenv from Kenneth Reitz, there’s standard pip, there’s requirements.txt, there’s a ton of different things trying to solve this problem and Fades in one of them.

00:00 Fades is a cross-platform thing. What is does is basically, given any script, it will auto-discover – with a caveat – auto-discover the dependencies of that script, see if it has a virtual environment that was previously run or create a new virtual environment if it hasn’t for the script, install all those dependencies and then run it. So, basically you can give a script to somebody and say, ‘Run this with Fades and run it with Python’ and it will handle all the pip install, make sure it’s that kind of version, this one has to be greater than that, and so on. It’s really cool.

00:00 So, if you’re going to be passing around scripts and you’ve got dependencies, this is an interesting way to take it on. I said it will auto-discover the tendencies, but with a caveat. There’s two ways to tell it what it needs to run a script. You can use a comment; if you said, ‘Import somemodule #fades ==3, it’s >=2.1’ or whatever. It will look through all the source code, find all the little Fades annotations, I guess you could call them, and then it will create a virtual environment with the modules, pip installed the way you set it up and then it will just run. You could ship your code with requirements.txt but then that’s a whole other level of experience that the person running your code has to have.

00:00 Another thing I think is cool about it is, it creates these virtual environments but you can basically say, ‘Run this and upgrade your virtual environment.’ You can say, ‘I’m using requests, import requests #fades,’ no version number specified there and a new version of requests is released. You can tell it, ‘Go and look for updates to the dependencies’ that you know about before you run it.

00:00 It’s pretty interesting. And you can create these configuration files. Create a configuration file and say, ‘My default version of Python is this…’ ‘My default update behavior is that…’ and so on. You can even clean up these old environments. There’s a way to say, ‘Delete all the ones that haven’t been accessed for a couple of days,’ things like that. It’s pretty neat.

00:00 OKKEN: It looks like it has some support for other repositories as well.

00:00 KENNEDY: Yeah, exactly. So, if you’re pulling out of a private git repo or something like that to execute your code, for a pip install you can pass it a git repo and you can also pass that as well, as part of your dependencies statement.

00:00 OKKEN: Pretty cool.

00:00 KENNEDY: You can even use a different index for your pip argument, so you can come off a private PyPI server.

00:00 That’s it for the news, but I wanted to cover one quick thing. So, many people know they can add comments to the show, just go to pythonbytes.fm/the episode number. For example, this one is pythonbytes.fm/33. There’s a comments/discussion section, also powered by Python. Last time we got a really cool comment from Jan Oglop. He said:

00:00 “Hey, Michael and Brian. I wanted to thank you for the amazing work you do and let you know that you’ve helped me find the working place of my dreams. My colleagues have similar hobbies and love Python as much as I do. Thank you again!”

00:00 OKKEN: That’s really cool.

00:00 KENNEDY: Yeah, congratulations, Jan. That’s fantastic.

00:00 If you guys want to leave a comment, we might read it on the show as well.

00:00 Alright, Brian. Anything else you want to add before we put a wrap on this week’s Python announcements?

00:00 OKKEN: Just that next time there’s a holiday, we need to not schedule super early recording times right after the holiday. (Laughs)

00:00 KENNEDY: Yeah, to be honest, last night was the Fourth of July in the U.S. and I was out with my kids. I don’t know about your neighborhood, but my neighborhood just goes off the hook. There was such an insane fireworks display down at the elementary school. People just descended and set off all kinds of stuff. I thought, from all the yelling with my kids, I might now actually have a voice this morning.

00:00 OKKEN: Yeah, I’m acting like I’m blaming Michael in this, but I’m the one that picked the 7 a.m. time, so it’s kind of my fault. Sorry.

00:00 KENNEDY: I’m still partly on Dublin time.

00:00 OKKEN: Yeah, welcome back.

00:00 KENNEDY: Thanks, it’s good to be back.

00:00 OKKEN: Well, thanks again for another episode.

00:00 KENNEDY: You bet. Thanks, Brian and thanks everyone for listening.

00:00 Thank you for listening to Python Bytes. Follow the show on Twitter via @pythonbytes and get the full show notes at pythonbytes.fm. If you have a news item you want featured, just visit pythonbyes.fm and send it our way. We’re always on the lookout for sharing something cool. On behalf of myself and Brian Okken, this is Michael Kennedy. Thank you for listening and sharing this podcast with your friends and colleagues.

Back to show page