Transcript #38: Hacking Classic Nintendo Games with Python
Return to episode page view on github00:00 Hello, and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 38, recorded August 7th, 2017. I'm Brian Okken, and we have guest hosts filling in for Michael while he's on vacation. This week, we have Matt McKay from Fullstack Python.
00:17 Hey, Brian.
00:18 Hey. Now, you also worked with Michael on the Python Entrepreneur Course, right?
00:22 Yeah, exactly. And I've been on Michael's podcast a couple of times. So he's always a great interviewer.
00:28 Yeah, it was hard having trouble not grinning and laughing during that because it's you I'm usually waiting for Michael to get done with the intro So hey, thanks a lot for stepping up and helping us with this show this week Yeah, I'm happy to help while Michael's taking a nice little vacation so you can come back all rested and ready to go and first off I've got you starting because why not this is from a talk at PyCon. Is that right?
00:52 Yeah, so I was at PyCon this year. I try never to miss it I unfortunately missed PyCon last year and I vowed never again because it's just my favorite event every single year. So I caught this talk. This is actually my colleague Sam Agnew's talk, which was called Hacking Classic Nintendo Games with Python. And the gist here was he was using a Nintendo emulator named FCEUX. I actually don't know if that has a better pronunciation or whether people say it by the letters all the time. But he used that in order to change old school Nintendo games he was playing and interact with the audience during that PyCon talk. It was pretty awesome.
01:30 I missed it. So tell us a little bit more about this hacking Nintendo games.
01:33 Yeah, sure. So Sam was inspired by the Pi NES project and he basically saw that you could use Python to interact with Nintendo games. And since he grew up on playing the old school NES Nintendo system, he wanted to use some of the old games that he had and basically emulate the games themselves, but also the way that the old Game Genie system worked, which was by hex editing the memory of the game itself.
02:01 And so what Sam did in this talk was he created a little Lua script, which would change the hex values in memory of a game.
02:09 And then he created a Python application that attendees at the talk could text in different hex codes.
02:15 And he was playing the game while attendees were changing the game on him.
02:19 So it was kind of like adding cheat codes to the game while he was playing.
02:22 So it was really cool to see sort of how he was able to interact with the audience, but also play some games that kind of brought out a little bit of the nostalgia factor.
02:31 - Oh, that's cool.
02:33 Yeah, I'll have to check that out.
02:34 - And the one other thing I really liked about it was he made certain topics that sound pretty intimidating, like hex editing memory values into what I think is like a relatively beginner friendly way to introduce it.
02:46 It basically takes the hex memory values, shows you, explains what they do, and then you can see the results when you edit the values.
02:53 So definitely a topic or a talk worth checking out.
02:56 And he also wrote a companion blog post that explains everything about what he did throughout the talk.
03:02 - Okay, I admit this morning I watched like just a small portion of it, and I just got to the point where he like changed the time, like there was a game running and we were watching the time left, he paused the game.
03:16 Super Mario Brothers, the first one.
03:18 - Yeah, Super Mario Brothers.
03:20 And then he changed the time back down to like zero and suddenly his character died and he said, "Okay, so that's how you hack it, the talk's over." - Exactly, and he did that just through editing hex memory values directly to change the time, which was pretty nuts.
03:36 - So there was Lua involved also.
03:38 I haven't watched the whole thing, but is the editing of the game in Lua And the Python was for the flask app or?
03:48 - Yeah, so the emulator itself, there's multiple emulators you can use in order to run NES games, but the one that he was most comfortable with actually had embedded Lua in order to script editing the hex memory values.
04:00 So really Lua was only being used to input the hex values into the memory and then read from a file that Python was writing to.
04:08 So you can think of, like there was a file on the system where his Python application was inputting into that file and then Lua was reading from it and just shoving it into memory to change the game.
04:18 So it was basically a file was being used as a bridge between Python and Lua.
04:22 Okay, great.
04:23 Cool.
04:24 I love that the PyCon talks are recorded and to be honest, when I was there, I didn't go to very many talks, but I just spend the year between PyCons just watching all of the old ones.
04:39 Yeah.
04:40 Yeah.
04:41 So, Nick.
04:42 That's awesome.
04:43 to bring up an article that Eric Holscher put out just recently and apparently he brings it up at the beginning of his talks a lot and it's he calls it the Pac-Man rule at conferences and it's just a short little article but I really it's really I like the the visual of the the Pac-Man little Pac-Man with a little gap so the idea and I'm just going to quote exactly what he says is when you're when you've got a bunch of people in a group like when you're meeting new people at conferences or something to make sure that there's a gap. So when standing in a group of people always leave room for one person to join your group. Leaving room for new people when standing in a group is a physical way to show an inclusive and welcoming environment. And just the whole idea, I know that I'm kind of, I started out totally as an introvert in because I got into programming, not because I really enjoy hanging out with people. So it is a stretch for a lot of people, including myself, to try to go up to new groups of people and join conversations. And I thought this was a great tip to, when you're having a conversation in a group, to make sure that you're physically showing and visually showing that you're welcoming new members. That's great. - Yeah, I agree. It's almost like pattern matching for developers. When there's room for one more person, you feel much more comfortable with just sort of approaching the group and asking people what they're talking about. So I think it's really good advice, both on the side where you're in a group already, but also when you're not in a group that, hey, there's some people that may want to talk to you and they've left you a spot at the table or where they're standing. So Eric did a great job on this.
06:21 - Yeah, I think it's great. That was just a short one. And you're going to talk about something that I really haven't had a chance to play with yet. And that's except for I've had a chance to to mispronounce it on this podcast.
06:34 And it's Bokeh?
06:36 - Yes. - Is that how you pronounce it?
06:37 - So if you take a look at the website, it actually can be pronounced two different ways, Bokeh and Boka.
06:43 So chances are you pronounced it correctly.
06:46 You just thought we were incorrect in your pronunciation.
06:50 But Bokeh, I usually just say Bokeh, is a Python data visualization library.
06:55 And the output is designed for presentations in web browsers.
07:00 So you don't have to write any JavaScript.
07:03 So if you've ever used D3JS, which is a fantastic data visualization tool, it's got a really steep learning curve.
07:11 So if you really have just spent a lot of time in Python land, you can use Bokeh to generate these amazing data visualizations that are rendered in the browser, and you don't have to write any JavaScript yourself.
07:22 So that's one thing that I love about this library.
07:25 And so I highly recommend you check it out it just had a new release, which was v0.12.6, just back in June.
07:35 And it had a whole bunch of improvements.
07:37 And this development team is doing an amazing job.
07:39 And they're gearing up for a 1.0 release, which I think is just going to make it--
07:43 I think it's going to make this library very, very popular very soon.
07:46 For creating visuals in Python, the one thing I have used is matplotlib.
07:52 So is there a rule of thumb of when to use generating plots on the client side versus the server side?
08:00 Yeah, I think that's a good question.
08:03 I haven't used Matplotlib as much as Bokeh.
08:06 I've only used it a bit in Jupyter Notebooks.
08:09 My rough rule of thumb, but I could be totally wrong, so I'd love to get emails if it's different.
08:15 My general rule of thumb is Bokeh is really fantastic for creating interactive visualizations in web applications.
08:21 You can certainly use it in Jupyter Notebooks as well, but I see Matplotlib as being sort of the quick and dirty solution to visualizing data in Jupyter Notebook scripts.
08:33 So I would typically use Matplotlib if I'm doing something that's like super early, I'm doing exploratory data analysis, whereas Bokeh is sort of the building a beautiful dashboard and you want bar charts, so you want some sort of interactive visualization that you can then have end users be working with.
08:51 - Okay, I think that's a great explanation.
08:53 So I'll have to check this out.
08:55 - Yeah, so, and that's Bokeh.
08:57 And I actually, what's funny is we, it always goes back to PyCon.
09:00 I found out about this library at PyCon in 2015.
09:04 Sarah Bird, who works on the core library, gave a fantastic talk where she essentially built an entire data visualization map, geo mapping data visualization, like during her talk.
09:14 And I was like, this is incredible.
09:16 So then I got a chance to use it a little bit.
09:18 I wrote a Flask tutorial on full stack Python that uses Bokeh.
09:22 And that's why I've been sort of, I don't wanna say evangelizing, but I've been telling people how enjoyable it is to use this library.
09:29 - That's great, cool.
09:30 I'll have to check it out more, definitely.
09:33 Well, next I've got another tool that I actually haven't used yet, but I just heard about it recently.
09:40 And it's a tool called Mosh, which is a mobile shell.
09:45 And the idea around it, it's kind of replacing SSH.
09:50 So if you have to have a connection to another instrument to run command line commands, and now we live in a time where we often work on laptops, and then you close your laptop and go home or go somewhere else and open it up again, that connection's gone, and you've got to reestablish your SSH.
10:09 And there's also other problems with SSH, with a keystroke when you're displaying the interactiveness.
10:18 And MOSH is an attempt to basically go down all the way to the protocol layer and redefine what gets transferred back and forth.
10:26 There's a YouTube video that we're going to link to that-- it's actually-- it's back from 2012.
10:32 And it's very convincing as to why we would want to do mobile applications differently than we've done them in the past at just the byte level.
10:41 I'm just kind of wondering if people are using it.
10:44 And that's just why I brought it up is it looks cool.
10:48 I want to try it.
10:49 I wonder if anybody else has used it.
10:51 - I haven't used Mosh myself, but it seems like SSH is a protocol that was designed to disconnect every single time I'm trying to write some code in front of an audience.
11:01 So I will definitely have to check this one out, especially when I'm doing live code demos, because when your SSH connection drops in front of an audience and you've got to reconnect, it can be a real pain.
11:14 So this is pretty awesome.
11:15 - And the video actually is of a, he's doing a, I don't know who's in the presentation, but there's a person presenting and they're connected to another system.
11:26 And right in the middle of typing a command, he changes his Wi-Fi connection to a different connection and it just works.
11:35 It like fixes itself.
11:36 So it's great.
11:38 This is great having you on because you're talking about just about a bunch of things I've never used, but definitely heard about.
11:45 Well, hopefully I get you excited enough to use them because they're some of my favorite tools.
11:49 And the next up is Pelican. Is Fullstack on use doing... Do you use Pelican for Fullstack?
11:55 I do, yeah. So, Fullstack Python, people luckily notice that it's pretty snappy and that's because it has no database backend.
12:03 just a bunch of flat files that are served up by a content delivery network.
12:07 And that's all generated.
12:09 I don't handwrite HTML, at least not for fun.
12:12 I generate that with Pelican, which is a static website generator.
12:16 So the way that I think about it is like static website generators basically have, I would say like three parts.
12:22 You have your content, which might be written in restructured text or markdown, some sort of markup format.
12:28 The second part would be a template engine.
12:31 So most likely Jinja, a lot of people who use Django are used to the Django template engine, but Pelican's kind of built out of the box with Jinja.
12:39 And then you have some Python code that puts the two together and then outputs typically HTML.
12:45 It can also be other formats like XML or JSON, really any sort of output that you want that is a file format, but I output HTML.
12:53 And then you can take those HTML files and you can host them anywhere.
12:57 So the power is you have these markup, you have all your content in the markup format.
13:01 So it's not like you're again, modifying HTML directly.
13:05 The static site generator does all the work for you with its, well, what are called generators.
13:10 The Python code creates, takes in the input and then outputs those, those file formats.
13:15 And then you can just, you can host them anywhere.
13:17 So the way that I think about it is that it's a, it's kind of like a throwback to the early days of the web when everything was like really snappy.
13:23 It was because you weren't connecting to databases via web application.
13:28 It was just because you were being served up a single file or multiple files that are basically just flat files.
13:35 Yeah, there may be some Perl on the back end.
13:38 One of the things that's great about Pelican is it's in under active development.
13:45 The latest release is version 3.7.1.
13:49 3.7.0 was released at the end of 2016 and that was like a major bump with a lot of good Python 3 compatibility.
13:57 In fact, I only use Python 3.6 with Pelican now.
14:00 It works fantastic.
14:02 So it's ready to go, Python 3 out of the box, and it's had a bunch of major work done to it, a lot of bug fixes recently.
14:10 So under active development, that's one of the things, there's so many static site generators that are out there.
14:14 It's relatively easy to create one as sort of a side project, but this one has been around for a long time.
14:20 So I started using Pelican probably six, seven years ago now, and I still highly recommend it, which is if you're a developer and you've been using a tool for that long, I feel like that's a pretty good sign that it's a stable foundation.
14:31 - Yeah, definitely.
14:33 I don't know if you remember what it was like when you first started using it though.
14:37 So if I wanted to pick it up and maybe say, like, let's say I've got like some, I wanted to create internal to a company.
14:43 I wanted to create a little site to, I don't know, document some process or something.
14:48 And wanted to spin up a Pelican site with a bunch of documents, I guess, in it.
14:55 Is that something that's gonna take a long time to learn how to use, or is it pretty quick to pick up?
15:00 - No, it should be pretty quick.
15:01 I mean, a lot of people think that static site generators are just kind of for blogs, which is sort of the original purpose of a static site generator.
15:08 When some of the first ones came out, they were mostly used with like a blog format.
15:13 But you can create any type of website with a static site generator.
15:16 And you can even create like single page applications if you combine it with some sort of JavaScript framework on the front end.
15:23 So yeah, so there's, I would say it's actually relatively easy to get started.
15:26 I wrote a tutorial on this, how to create your first static site with Pelican and Jinja.
15:31 And that would actually get you up and running probably within, I don't know, 20, 30 minutes.
15:36 Do pip install Pelican.
15:37 - Wow, okay.
15:38 - And then Pelican will actually generate the boilerplate that you need with the Pelican Quick Start program.
15:44 And then you have your first static site.
15:46 Actually, you could probably have your first static site within five minutes, and then you can start customizing it and have something that you could deploy very quickly after that.
15:55 - Well, that's definitely fast enough.
15:56 And is the tutorial have talk about, I guess it probably doesn't matter which Markdown you use, if you use Markdown or Restructured.
16:04 - Yeah, I actually, I use Markdown on full stack Python, but on my personal site, mattmckay.com, I use Restructured Text.
16:10 So I kind of use each one interchangeably.
16:14 In fact, you can use them interchangeably on the same, in the same site.
16:18 So Pelican doesn't, yeah, Pelican doesn't really care.
16:20 It'll just say, oh, you've got five markdown files, five restructure text.
16:23 I think the ASCII doc is another format.
16:26 You can kind of just take anything as input and it'll use that.
16:29 It doesn't have to be segmented by projects.
16:31 - Does it tell by the file extension or something?
16:34 Or do you know?
16:35 - Yeah, you can, well, in the configuration settings, you can tell Pelican which file formats you want and also which extensions.
16:42 So if you don't wanna pick up your restructured text files, you can just have it pick up the markdown ones, but you can configure all that stuff in the configuration files.
16:50 It's pretty extensible.
16:51 - Nice, okay.
16:53 The last topic I wanna talk about was something that just helped me just now.
16:57 So in the last episode, I announced that I'm no longer writing the book, the book being Python Testing with pytest.
17:04 It's what I've been working on for like a year, But I was wrong because right after we recorded that and I handed in all my documents to my editor, the pytest team came out with another version, the pytest 3.2.0.
17:19 And I had just, the week before, retested all of my examples in 3.1.3.
17:28 So what do I do?
17:30 And what I'm doing is just making sure they all work.
17:34 So right now I'm going through all of the book again.
17:38 And instead of having to do this every time, I've decided I'm going to build a test framework to--
17:44 not a framework, but I'm going to build a set of tests that check all of the examples and make sure that the output is similar enough to the output I describe in the book so that it doesn't confuse somebody when they have a new version.
17:57 And one of the things I'm using is a tool called pytest Watch.
18:02 It's a pytest plugin.
18:04 It's great because I've just got two windows open in two terminal windows while I'm editing these tests.
18:11 One of the terminals is running pytest 3.1.3 and one of them is running pytest 3.2 in two different virtual environments.
18:18 And I've got pytest Watch just looking at the directory and every time I hit save on the tests, both of them go off and run.
18:26 And eventually, when I get all done with this, I'll probably convert it to talks or something so that I can run them all the time.
18:33 for now, interactively, pytestWatch is pretty cool.
18:36 - Wow, that's amazing.
18:38 How much code did you have to write in order to get that to be working with both versions?
18:42 - I'm actually using the, there's a part of pytest that I do cover briefly in the book that's called the pytester.
18:51 And it's a plugin that is used for the intended uses for testing plugins, but it's also used by the test code for pytest itself.
19:02 and it allows you to run a pytest session and capture the output and ask things about it, like was this string in there and how many passes, fails, skips, things like that are in there.
19:14 And so I'm writing the test just as if I was writing like a plugin or something.
19:20 And it's taking me, it's about, it's not quick, but the format of each is pretty similar and it It takes me about a minute or so per example to get a test in place.
19:31 But there's like 170 examples, so 170 minutes.
19:37 Any way to speed that up?
19:39 You're happy with it right now?
19:42 There probably is.
19:43 The slow part really is just the me looking at it.
19:47 I could probably automate the whole thing and get it done faster, but I also want to be reading the book and while I'm putting these together to make sure that...
19:58 Because there's sometimes there'll be you know something minor like I guess not really minor differences but I don't really want to just say I want the exact same output because if they added you know a period here or there that's not that big of a deal. But I want the the gist of it to be correct.
20:15 - Wow, nicely done.
20:17 - Anyway, so that's that.
20:19 And we're done.
20:21 Thanks a lot.
20:22 - Yeah.
20:23 - I wanna, usually at the end, we touch bases with each other to say what's going on.
20:27 So I just learned today that Matt, that you worked at Twilio.
20:31 - Yeah, I do.
20:32 So Twilio for folks who don't know, it makes it easy for developers to add communications, like phone calling, messaging, and video to their applications.
20:43 So if you've ever wondered, You're working on a feature and you're in the middle of a sprint and your user story says, "Okay, send a text message to somebody." You're like, "Well, how do I do that?
20:54 How do I interact with this global telecom network?" Well, you could do that really easy with Twilio's API.
21:01 So yeah, that's my day job.
21:02 So in addition to working on full stack Python, I work at Twilio, which has been really great.
21:07 I've been there for almost four years now.
21:08 That's great.
21:09 And you're also working on a project called Twilio Voices?
21:13 Yeah, so Twilio Voices is something, a new project I've been working on, and it's probably of interest to listeners because we pay developers $500 for each published technical blog post that they have on the Twilio blog.
21:27 So if anyone's listening and you want to try pytest Watch, and you want to learn a new open source project or something like that, if you just write some code and then you want to write a blog post about it, you can do that through Twilio Voices and get paid $500.
21:42 The way that I see it is like the 500 bucks is a good way to get past the, the procrastination stage of writing a blog post.
21:49 And then we promote that blog post to everyone in our different channels.
21:53 So a lot of times these blog posts can be read by tens of thousands of people.
21:57 For example, there was one called a wedding at scale, how I automated my, with Python and Twilio and Google.
22:05 I mean, basically it was a developer who was talking about how he automated all the text messaging and communication for his wedding through a Python that he wrote, which is a really cool story.
22:14 So the whole idea behind Twilio Voices is people have all these awesome hacks and applications that they've built.
22:21 Tell us a story about it and show readers how to build what you built.
22:25 Doesn't have to use Twilio, can be basically just any code that you've created as long as it's a cool story or a good tutorial.
22:32 - So it doesn't have to be pushing Twilio, it can be just about anything?
22:36 - No, 'cause we're a company for developers, by developers for developers, and we've always been that way.
22:42 So whether you're using Twilio or not, we want to see the code that people have written and, one thing that's really great about this is every post goes through a rigorous review process, like an outline review, voice review, tech review, to make sure all the code works.
22:55 So that like people who write these blog posts, make sure that their output is like the best that they've ever created, which has been really cool to see people say like, I never could have written a blog post that was like this polished.
23:07 So we, we kind of put it through the, through the ringer to make sure that It's the highest quality post.
23:12 - That's pretty cool.
23:13 I mean, you usually have to pay money to interact with an editor, so that's nice.
23:19 - Cool.
23:20 - Yeah, you should write something on testing.
23:22 - Maybe I should write a blog post, yeah.
23:24 - We would love to have it.
23:25 So what have you been up to, Brian?
23:26 - Well, I'm not actually writing the book anymore, which is good, but I'm still working.
23:31 - Congratulations.
23:32 - Thanks.
23:33 But I am working on book-related activities for this month, because it still is going through, It has to go through copy editing and figuring out marketing plans and all that good stuff.
23:46 So I've got a couple of podcasts for testing code.
23:50 One came out last week and I'll probably get, I'll do weekly ones, get them out.
23:54 But it's still, that's still slow while I'm spending most of my free time on the book still.
23:59 So other than that and my full time job, yeah, that's what I'm up to.
24:04 So.
24:05 Hopefully you get some time to sleep then.
24:07 Yeah.
24:08 So I've been fascinated hearing about Matt, and I've convinced him that I'm going to put him on the spot on the air and tell him that he's got to get on the testing code and talk about full stack sometime.
24:19 So sure.
24:20 Happy to do it.
24:22 Thank you for listening to Python Bytes.
24:24 Follow the show on Twitter via @pythonbytes.
24:27 That's Python Bytes as in B-Y-T-E-S.
24:31 And get the full show notes and links to things we talked about on the show at pythonbytes.fm.
24:37 If you have a new story you'd like featured, just visit pythonbytes.fm and send it our way.
24:44 We're always on the lookout for sharing something cool.
24:48 This is Brian Okken.
24:49 On behalf of myself, Michael Kennedy, and Matt Mackay, thank you for listening and sharing this podcast with your friends and colleagues.
24:55 [BLANK_AUDIO]