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


Transcript #38: Hacking Classic Nintendo Games with Python

Return to episode page view on github
Recorded on Monday, Aug 7, 2017.

00:00 Hello, and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.

00:05 This is episode 38, recorded August 7th, 2017.

00:09 I'm Brian Okken, and we have guest hosts filling in for Michael while he's on vacation.

00:14 This week, we have Matt McKay from Full Stack 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, I was having trouble not grinning and laughing during that, because I'm usually waiting for Michael to get done with the intro.

00:36 So, hey, thanks a lot for stepping up and helping us with this show this week.

00:41 Yeah, I'm happy to help while Michael's taking a nice little vacation so he can come back all rested and ready to go.

00:45 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.

00:56 I would unfortunately miss PyCon last year, and I vowed never again, because it's just my favorite event every single year.

01:01 So I caught this talk.

01:03 This is actually my colleague Sam Agnew's talk, which was called Hacking Classic Nintendo Games with Python.

01:09 And the gist here was he was using a Nintendo emulator named FCEUX.

01:15 I actually don't know if that has a better pronunciation or whether people say it by the letters all the time.

01:20 But he used that in order to change old school Nintendo games he was playing and interact with the audience during that PyCon talk.

01:28 It was pretty awesome.

01:29 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 PyNES project.

01:37 And he basically saw that you could use Python to interact with Nintendo games.

01:43 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.

01:51 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:00 And so what Sam did in this talk was he created a little Lewis 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:32 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 a relatively beginner-friendly way to introduce it.

02:45 He basically takes the hex memory values, 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.

03:11 Like there was a game running, and we were watching the time left.

03:15 He paused the game.

03:16 Yeah, in 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, you know, his character died.

03:26 And he said, okay, so that's how you hack it.

03:28 The talk's over.

03:30 Exactly, and he did that just through editing hex memory values directly to change the time, which was pretty nuts.

03:35 So there was Lua involved also.

03:38 So I was confused.

03:40 I haven't watched the whole thing, but is the editing of the game in Lua and the Python was for the Flask app?

03:48 Yeah, so the emulator itself, there's multiple emulators you can use in order to run NES games.

03:54 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:09 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 Python talks are recorded.

04:29 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:38 Yeah.

04:39 So, yeah.

04:40 So, Nick.

04:41 That's awesome.

04:41 Speaking of conferences, I wanted to bring up an article that Eric Holscher put out just recently.

04:48 And apparently, he brings it up at the beginning of his talks a lot, and he calls it the Pac-Man rule at conferences.

04:55 And it's just a short little article, but I really like the visual of the little Pac-Man with a little gap.

05:04 So the idea, and I'm just going to quote exactly what he says, is 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.

05:15 So when standing in a group of people, always leave room for one person to join your group.

05:20 Leaving room for new people when standing in a group is a physical way to show an inclusive and welcoming environment.

05:27 And just the whole idea, I know that I started out totally as an introvert because I got into programming, not because I really enjoy hanging out with people.

05:38 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.

05:45 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.

05:56 Yeah, I agree.

05:57 It's almost like pattern matching for developers.

06:00 When there is a, when there's room for one more person, you feel much more comfortable with just sort of approaching the group and, and asking people what they're talking about.

06:07 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, 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.

06:20 So Eric did a great job on this.

06:22 Yeah, I think it's great.

06:23 That was just a short one.

06:25 And, you're going to talk about something that I really haven't had a chance to play with yet.

06:29 And that's except for, I've had a chance to mispronounce it on this podcast and it's bokeh.

06:36 Yeah.

06:36 So if you take a look at the website, it actually can be pronounced two different ways, bokeh and bokeh.

06:43 So chances are you pronounced it correctly.

06:46 You just thought you incorrect.

06:48 We were incorrect in your pronunciation, but bokeh.

06:51 I usually just say bokeh is a, is a Python data visualization library.

06:55 And the output is designed for presentations in web browsers.

06:59 So you don't have to write any JavaScript.

07:02 So, if you've ever used D3 JS, which is a fantastic data visualization tool, it's got a really steep learning curve.

07:10 So if you really have just spent a lot of time in Python land, you can use bokeh to generate.

07:16 These amazing data visualizations that are rendered in the browser.

07:19 and you don't have to write any JavaScript yourself.

07:21 So that's one thing that I, I love about this, this library.

07:25 And so I, I highly recommend you check it out because it just had a new release, which was, V 0.12.6.

07:34 Just back in June.

07:35 And it had a whole bunch of improvements and they're, this development team is doing an amazing job and they're gearing up for a 1.0 release, which I think is just going to make it.

07:42 I think it's going to make this library very, very popular very soon.

07:46 For visuals in creating visuals in Python.

07:48 The one thing I have used is Matt plot lip.

07:52 So is there a kind of a rule of thumb or of when to use generating plots on the, on the client side versus the server side or.

08:01 Yeah, no, I, I think that's a good question.

08:02 And, and I haven't used Matt plot plot lip as much as okay.

08:06 I've only used it a bit in Jupyter notebooks, my rough rule of thumb, but I could be totally wrong.

08:12 So I would love to get emails if, if it's different.

08:14 My general rule of thumb is okay.

08:16 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 Matt plot live as being sort of the quick and dirty solution to visualizing data in Jupyter notebook.

08:32 scripts.

08:33 So I would typically use Matt plot live.

08:35 If I'm doing something that's like super early, I'm doing exploratory data analysis.

08:39 Whereas bouquet is sort of the building a beautiful dashboard and you want bar charts.

08:44 So you want some sort of interactive visualization that you can then have end users be working with.

08:50 Okay.

08:51 I think that's a great explanation.

08:53 So yeah, I'll have to check this out.

08:55 Yeah.

08:55 So, and that's, and that's bouquet.

08:57 And, and I actually, what's, what's funny is we, it always goes back to PyCon.

09:01 I found out about this library at PyCon in 2015.

09:03 Sarah Bird, who works on the core library, gave a fantastic talk where she essentially built an entire data visualization map, geomapping data visualization, like during her talk.

09:14 And I was like, this is, 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 bouquet.

09:22 And that's why I've been sort of, I don't want to say evangelizing, but I've been telling people how, how enjoyable it is to use this library.

09:29 Yeah.

09:29 That's great.

09:30 Cool.

09:30 I'll have to check it out more.

09:31 Definitely.

09:32 Well, next I've got another, 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, it's a mobile shell.

09:45 And the, 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 you, now we live in a time where we've often work on laptops.

10:00 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, you know, there's other problems with, with SSH, with 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:31 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:40 I'm just kind of wondering if people use, are using it.

10:44 And that's just why I brought it up is it looks cool.

10:47 I want to try it.

10:48 I wonder if anybody else has used it.

10:50 And 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:00 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, 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, there's, I don't know who's in the presentation, but there's a person presenting and they, they're connected to another system.

11:26 And right in the middle of typing a command, he changes his wifi connection to a different connection.

11:33 And it just works.

11:35 It like fixes itself.

11:36 So it's cool.

11:37 It's great.

11:37 This is great having you on because you're talking about just about a bunch of things I've never, I've never used, but definitely heard about.

11:45 And well, hopefully I get you excited enough to use them because there's some of my favorite tools.

11:49 And the next up is a Pelican is full stack on use doing, do you use Pelican for full stack?

11:55 I do.

11:55 Yeah.

11:56 So a full stack Python, people luckily noticed that it's pretty snappy and that's because it's not, it has no database backend.

12:02 It's all 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.

12:50 That is a file format, but I output HTML and then you can take those HTML files and you can host them anywhere.

12:57 So the power is 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:22 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 some multiple files that are basically just flat files.

13:34 Yeah.

13:35 There may be some Pearl on the back end, but.

13:37 One of the things that's great about Pelican is, so it's, it's in under active development.

13:44 And so the, the latest releases version 3.7.1, 3.7.0 was released at the end of 2016.

13:52 And that was like a major bump with a lot of good Python three compatibility.

13:57 In fact, I only use Python three six with Pelican now.

14:00 It works fantastic.

14:01 So it's ready to go Python three out of the box and it's had a bunch of major work done to it.

14:08 A lot of bug fixes recently.

14:09 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.

14:17 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, it's a stable foundation.

14:31 Yeah, definitely.

14:32 I don't know if you remember what it was like when you first started using it though.

14:36 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 going to 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 just kind of for blogs, which is sort of the original purpose of a static site generator.

15:07 When they first, when some of the first ones came out, they were mostly used with like a blog format, but you can create any type of website with a static site generator.

15:16 And you can even, even create like single page applications if you combine it with some sort of JavaScript framework on the front end.

15:22 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:30 And, that would actually get you up and running probably within, I don't know, 20, 30 minutes, 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, 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:54 Well, that's definitely fast enough.

15:56 And is your, is the tutorial, have, talk about, I guess it probably doesn't matter which Markdown you use.

16:02 If you use Markdown or restructured.

16:04 Yeah, I actually, I use Markdown on full stack Python, but on my personal site, mattmckai.com, I use restructured text.

16:10 So, I kind of use each one interchangeably.

16:13 in fact, you can use them interchangeably on the same, in the same site.

16:17 So Pelican doesn't, yeah, Pelican doesn't really care.

16:20 It'll just say, Oh, you've got five Markdown files, five restructured text.

16:23 I think the ASCII doc is another format.

16:25 You can kind of just take anything as input and it'll use that.

16:28 It doesn't have to be segmented by projects.

16:31 Does it tell by the extent file extension or something?

16:34 Do you know?

16:35 Yeah, you can, well, in the configuration settings, you can tell, you can tell Pelican which file formats you want and also which extensions.

16:41 So if you don't want to pick up your restructured text files, you can just have it pick up the Markdown ones, but you can, you can configure all that stuff in the, in the configuration files.

16:49 It's pretty, pretty extensible.

16:51 Nice.

16:51 Okay.

16:52 So the last topic I want to talk about was something that just helped me just now.

16:56 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.

17:07 because right after I, 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 did, I had just recent, just like 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, 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 not a framework, but I'm going to build a set of tests that check the, all of the examples.

17:48 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, is a tool called pytest watch.

18:02 It's a pytest plugin.

18:03 It's great because I've just got two windows open in, in two terminal windows while I'm editing these tests.

18:10 One of the terminals is running pytest 3.1.3.

18:14 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.

18:21 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:32 But for now, interactively, pytest watch is pretty cool.

18:36 Wow.

18:37 That's amazing.

18:37 How, how much like 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:50 And it's a, it's a plugin that is used for the intended uses for testing plugins.

18:58 But it's also used by the test code for pytest itself.

19:01 And it allows you to run a pytest session and capture the output and ask things about it.

19:08 Like was the string in there and how many passes, fails, skips, things like that are in there.

19:14 And so I'm, I'm writing the tests just as if I was writing like a plugin or something.

19:19 And it's taking me, it's, it's about, it's not quick, but the format of each is pretty similar.

19:25 And it takes me about a minute or so per example to get a test in place, but there's like 170 examples.

19:33 So, you know, 170 minutes, but.

19:36 Any way to speed that up or you're, you're, you're happy.

19:40 You're happy with it right now.

19:41 There probably is.

19:43 The, the slow part really is just the, the me looking at it.

19:46 I could probably automate the whole thing and get it done faster.

19:51 But I also want to be reading the book and while I'm putting these together to make sure that, because there's sometimes there, 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.

20:12 Right.

20:13 But I want the, the gist of it to be correct.

20:15 So.

20:16 Wow.

20:16 Nicely done.

20:17 Anyway.

20:18 So that's that.

20:19 And, we're done.

20:21 Thanks a lot.

20:22 Yeah.

20:22 I want to, 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 work at Twilio.

20:31 Yeah, I do.

20:32 So Twilio for folks who, who don't know, it, makes, makes it easy for developers to add communications, like phone calling, messaging and video to their applications.

20:42 so if you've ever wondered, you've gotten, you know, 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 like, well, how do I do that?

20:53 How do I like interact with this global telecom network?

20:58 Well, you could do that really easy with Twilio is API.

21:00 So yeah.

21:01 So that's my, my day job.

21:02 So in addition to working on full stack Python, I, I work at Twilio, which has been, 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, there, a project called Twilio voices.

21:13 Yeah.

21:13 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 posts that they, they have on the Twilio blog.

21:27 So if anyone's listening and they, you want to try pie test watch and you want to learn, or 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 bucks.

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:04 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 script that he wrote, which was 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:20 Tell us a story about it and show us how show readers, how to build what you built.

22:25 It doesn't have to use Twilio can be basically just any, any code that you've created, as long as it's a, if it's a cool story or a good tutorial.

22:31 So it doesn't have to be pushing Twilio.

22:34 It can be just about anything.

22:36 no, cause we're a company for developers by developers for developers.

22:40 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.

22:46 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:11 That's pretty cool.

23:12 I mean, you usually have to like pay money to interact with the editor.

23:17 So that's nice.

23:19 Cool.

23:20 Yeah.

23:20 You should, you should write something on testing.

23:21 Maybe I should write a blog post.

23:23 Yeah.

23:24 We would, we would love to have it.

23:25 So what are you, what are you been up to, Brian?

23:26 Well, I, I'm not actually writing the book anymore, which is good, but I'm still working.

23:31 Thanks.

23:32 But I am working on book related activities, right?

23:35 For this month, because it still is going through, it has to go through copy editing and figuring out marketing plans and all that, that good stuff.

23:46 So I've got a couple of podcasts for testing code.

23:49 One came out last week and I'll probably get, I'll do weekly ones, get them out, 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:03 So you, hopefully you get some time to sleep then.

24:06 Yeah.

24:07 So I've been fascinated hearing about Matt and I've convinced him that I'm going to, 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:21 Thank you for listening to Python Bytes.

24:23 Follow the show on Twitter via at Python Bytes.

24:27 That's Python Bytes as in B-Y-T-E-S.

24:30 And get the full show notes and links to things we talked about on the show at Python Bytes.fm.

24:36 If you have a new story you'd like featured, just visit Python Bytes.fm and send it our way.

24:43 We're always on the lookout for sharing something cool.

24:47 This is Brian Okken.

24:48 On behalf of myself, Michael Kennedy, and Matt McKay, Thank you for listening and sharing this podcast with your friends and colleagues.

24:55 Thanks.

24:55 Thank you.

Back to show page