- Podcasts
- Python Bytes
- #304 Build your own text adventure language in Python
#304 Build your own text adventure language in Python
Python Bytes
- October 5, 2022
- 38 min
- Episode 304
About this episodeTopics covered in this episode: Ten tasty ingredients for a delicious pull request textX Reasoning about asyncio.Semaphore Turnstile…
- Ten tasty ingredients for a delicious pull request
- textX
- Reasoning about asyncio.Semaphore
- Turnstile
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#304 Build your own text adventure language in Python
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 304, recorded October 5th, 2022. I'm Michael Kennedy. I'm Brian Okken. Hey, Brian. Hey, you threw me for a wrench with the whole date thing. I know. Well, in our notes it has yesterday, but then we had to move it.
Yeah, because of meetings. Some of us have meetings. Yeah, that's right. I've moved plenty for me. Plenty of moving. Yeah, very cool. Well, great to be back with you. Also, we have a sponsor that sponsored before, but has been a while, a compiler from Red Hat. Excellent show. Tell you more about that later.
For now, Brian, I'm kind of hungry. What can you do for me here? I've got tasty ingredients for, let's see, add to stream. Tasty ingredients for a delicious pull request. So it's October now. So that means Hacktoberfest for some people, right? And actually, I didn't really want to just highlight Hacktoberfest. What I really like about this. So this is an article, 10 tasty ingredients for a delicious pull request.
It's on the Wagtail blog and from LB on the Wagtail core team member. So thanks, LB. But this, it's kind of neat because there's, it's not, yeah, it's kind of clickbaity, but also it's just, it's actually nice information. So whether you're starting open source project or starting, want to start helping out with an open source project, or I think these tips are great, or at least majority of them are great.
Even for people that are used to developing solo and are moving into developing with a team of people or a group of people. And there's, I've had questions about that, even from companies of like, yeah, we want to kind of work better as a team. How do we do that? So I'm going to run through these quickly. There's, there's 10 of them. First one is, is great. It's read the documentation or read the development instruction.
So a lot of projects have contributing guidelines. So actually read those and understand them when we're used to it. It's just a, Hey, fork it and go. But that's not trivial for some people. So, right. Well, you may have a non-obvious details. Like for example, maybe you just fork the repo, you do your work there and you contribute, but well, you should obviously create a branch in your repo so that if they decide to not take it, then how do you stay in sync? Right. That's one of them.
Another one is, well, maybe when you do the PR, it doesn't go back to the main branch. It goes to some other like dev branch or something, right? Like you need to know, here's where you put the PR back to you. And just like those kinds of details for, Hey, we're a team. Yeah. And then also there's details like, how do you run the tests and are there extra stuff that you have to install or any settings or for a lot of projects, it's just a, you know, do a virtual environment and clone it and go. But there's, there's, there's some that have like extra bits that you have to set up. So good to read the instructions, but then we're going into things like read the issues and comments. So if you want to help out, this is a great way is to read the issues, but don't just jump in and try to solve it right away.
Read the comments around the issue. Also PRs that are related to the issue. If there's any PRs linked, because there's somebody that might be started on a fix and you want to, you want to help either help them get unstuck or just help out. You don't want to just take over and say, Oh, I could solve this. If somebody else is working on it already.
But maybe you do. There's, there's different ways, but pay attention to that. So that's good. Create a French branch for your contribution. Like you mentioned it, it isn't always obvious because I'm already forking. What do we need a branch for? But it does help with the branch model. When it comes back in, it just looks better.
It's a, it's not necessary until your change is not accepted or you want to make two changes, right? Yeah. If you ever want to get back in sync with the main repo, you need to do somewhere else. And it might be very much a possibility that you want to make several changes. You're really excited about a project and you're making several. And like you said, there, there might be a lot of discussion around some, and some of them are trivial things like fix and read me or something.
And so yeah, multiple do a branch so that you've got multiple names and that's where we come to the next tip for is keep the change focused. So if a project would much rather have several focused pull requests, then a single pull request with like four different changes in it that are completely unrelated.
So keep them focused. And this is great for personal or, you know, closed source projects too. It just makes the code review easier. And when you, when you get in the habit of doing focused changes, it just, it's not harder. It's just a workflow you got to get used to. Next is right unit tests. Definitely for the changes. But one of the comments that I loved in this is a bonus tip is it, there might be some either untested features in the project or under tested and adding tests around those features that you care about is a great way to contribute to, to a code base. So keep that in mind.
That's a really good point because people often think, well, what can I do to contribute? I'm not an expert. Well, if it's missing tests, that's, that's pretty easy. And there could be even like, if you, so there's an idea of like testing is documentation and some tests work well like that. And some tests do not. So a great way also just, just to walk through, make tests around the onboarding documentation, like the getting starting guide, making sure that those features are tested is a good thing to throw in there.
Give your pull request name, request a name with context. So naming well is good. And don't be surprised if you're, if your pull request name gets changed by one of the, one of the core contributors. That's not, it's just so that everybody can track what's going on inside better. So just learn from that.
If mine is associated with an issue, I try to put the issue number as part of the branch. Yes. That I'm like, you know, it'd be like add such and such. If that add such and such is actually to fix, you know, issue 221, it'd be like the branch should be, you know, add such and such 221 or whatever for the branch name. Just so you're like, where the heck did this come from? Why is it here? Oh yeah, that's right. I can go find it.
Yep. And reference the issue being fixed or resolved in the pull request. So referencing the issue in a way that, and you kind of got to get used to this with GitHub or GitLab or other things. They'll link to the thing. It links things up if you, if you name it correctly. So just play with that. And then some again, and then hopefully there's CI, CI going on and tests. So if your pull request has a break, has a break, the tests fail, address those. This is nothing more frustrating than a fairly decent pull request coming in, but the tests fail or the coverage fails or something.
And the person that issued the pull request doesn't deal with it. They just leave it for you. And that's, that's not cool. So yeah, I know address those. only a couple more push to the same branch with fixes and do not open a new pull request. So if the CI breaks, just push to the same branch again. it'll work. and then the lastly is be patient. I know that you've worked really hard on, on a change and you want to see it go into the main project right away, but these are volunteer projects mostly. So, it might take a while.
To, to, to review it and be comfortable with the changes. So. Yeah. That's a bit of a balance. yeah. It's frustrating when you work on a project, you work, try to enhance some project and then you put something out there and there's not even a response after, you know, like two weeks. You're like, well, yeah, I guess, I don't know. I don't know what to do with that, you know? but at the same time, it's absolutely volunteer. So there you go. All right. Let me tell you about something pretty cool for this next one, Brian. I learned about this from Rhett Turnbull. He and I had a chat over on Talk Python and built building native Mac apps in Python. recall we talked about TechSniper and he built a Python only equivalent and we actually covered on the show a little bit, but during our conversation there, this is one of the recommendations he had for something really cool. So imagine somebody comes to you and says, Hey, Michael, I have a Raspberry Pi or Arduino thing in my lab.
And it controls, let's say this is a biology lab, right? It controls experiments and you can, you know, build up something with like a UI or whatever, but if people want to script it, wouldn't it be cool if there's a way to express in like, some form of syntax. Here's how somebody who is just a biologist can make it go. It could just be like, fill pipette seven, you know, mix, mix pipette seven and 10, wait 10 minutes, you know, heat to such and such, right? You could, sure. You could come up with a programming language for them. You say, well, here's your language. You know, we're going to give you C++. Good luck.
Anyway, what if you could come up with the words that I used to like introduce this idea? You just have like lines like fill pipette seven, mix pipette seven and eight or whatever. And so this thing I'm talking about here is called text X and text X is a Python library that allows you to build these kinds of grammars and map them Python implementations in a super easy way.
Wow. Okay. So, so, let's see, it's a meta language for building domain specific languages, DSLs in Python. Basically it'll help you build a textual language in an easy way. And you can invent your own language, as I said, or build support for an existing format. So they have an example here. So they have this grammar and this is about, moving things around and basically drawing something in a language.
So you come up with a grammar, you'd say, there's a shape command, which is just line two or circle. There's a move command, which is a move to or move by. And then you just have these words move. And then two that runs the move to command. If the word, the line is move space by, that's the move by command.
And then you map to the additional information to these classes. So these are all Python classes, right? So you just come up, this is what is this? 10 lines, eight lines. and then you create the class, which is a point and other pieces. And you just say meta model from string, give it the grammar and the classes involved.
And then here's the new language we just created. Move space to five comma 10 line to 10 comma 10. How cool is that? That's pretty interesting actually. Yeah. You just create the model from there and then you can, you can just basically parse it for command in the model. Start doing the things. If the commands move to or move by, right? It's already done the parsing. It has the command elements like the vector or the point already parsed and validated.
And it just executes Python. Okay. What do you think? Well, so how do you use it? So do you call, like, do you, do you put all these commands in a, the, the, somebody that put all these move things in a file and then run? Yeah, exactly. So you would, you would implement the code that defines the grammar.
You would implement the grammar classes. So like point and, and vector and those sorts of things. And then somebody would write the little, the grammar. So move space to five comma 10. And then they'd probably have either a CLI or they save it in a file in some location and like you auto discover it or however it runs.
So basically from a, an external perspective, you now allow people to just program in this simple way. Like another way you could do it is like, you could write a, like a text adventure game, right? You could like look around, look at, move, right. Move, move east, west, north, right. Attack creature run. Like you could really easily in Python build like a pretty interesting text adventure game based on this. I would imagine probably it already exists.
yeah, well, yeah. So the adventures around, so we could probably re-implement it with this. yeah, might be fine. Yeah, I'd be fine. But the, this, or like a complete, like burn way to burn up a summer. the, this, this looks cool though. There's a whole bunch of cases where DSL is appropriate, especially when you're interacting with non-programmer type people.
exactly. You want to give them some kind of text ability to interact with what you've created, but you don't want to force them to do full on super precise programming. Yeah. Or maybe it is programmers and it's just easier to think about stuff in this manner. So neat. Yeah. I just have a real high level way to talk about what you want.
Yeah. Indeed. Cool. Anyway, so text X, people can check it out. Another thing that's pretty awesome. Compiler from Red Hat, our sponsor this week. Yeah. Let me tell folks about that before we move on. So this episode, as I said, is sponsored by Compiler. Just like you, Brian and I are fans of podcasts, and I'm happy to share this new, highly respected one from an open source company. I'm sure you know, Compiler, an original red podcast from Red Hat.
And if you want to stay on top of tech without dedicating tons of time to it, Compiler presents perspectives, topics, and insights from the tech industry free of jargon and judgment. They want to discover where technology is headed beyond the headlines and create a place for new IT professionals to learn, grow, and thrive.
Compiler helps people break through barriers and challenges, turning code into community at all levels of the enterprise. One recent interesting episode is their The Great Stack Debate episode. I love, love, love talking to people about how they architect their code, the trade-offs and conventions they chose, and the costs, challenges, and smiles that result.
This one is like that, The Great Stack Debate episode. Check it out and see if software is more like an onion or more like lasagna, or maybe it's more complicated than food. This is actually the first episode in Compiler's Software Stack series, and I think it'll be pretty interesting. So check that one out.
Learn more about Compiler at pythonbytes.fm/compiler. The link is in your podcast player show notes. Yes, I know you can open up your podcast player and just search for Compiler, but please visit pythonbytes.fm/compiler and click on your podcast player's icon. That way they know it came from us. Our thanks to Compiler for keeping our podcast going strong.
He doesn't blog often, but there's usually some interesting bits when he wants to. So this is a post called Reasoning About AsyncIO Semaphore. So it starts off with a kind of a story about a restaurant. So there's an analogy here about, okay, so you got a restaurant. It's a fast food place, but it only has one table and there's a line waiting for it.
But you don't want to just wait in line. So there's a way for you to get a buzzer. So somebody hands you a buzzer and you go wait. And when the table's open, your buzzer rings and you go get seated and you can eat. This sort of analogy, he talks about this as a lock. And then so with concurrent programming, so only one person getting it at a time.
But then if you've got multiple tables, things get a little more interesting. So that's where semaphores come in. And I love this mind shift of, and this is often difficult for people when they're doing asynchronous programming, to have this lock makes sense. You're either a thread or some code is using a resource and it can and nobody else can.
But then you unlock it and somebody else can grab it. But what if there's like three people can at a time? Those are some of the different, some a little bit of a mind shift. So I love this idea of a three table restaurant as a way to think about this analogy. And so he goes through the analogy, but then talks about locks and semaphores.
And this is apparently to help himself even with the implementation of the semaphore in the new ACIO code. So in making sure that things like work correctly and using an analogy to try to figure out how things are supposed to work and like an error. So if you've got an exception happening, one of the problems can be it's like somebody gets, but they go, they're waiting and they decide they don't want to wait anymore.
So they go turn their buzzer in. Well, what happens if while they're when they're just as they've decided to turn it in, but it's their turn and they get buzzed and they but they turned it in anyway and they don't want to run. So they're never going to call the release code. And so with the common the interaction between cancel and release is difficult.
So that's pretty cool. Right on. Yeah. Semaphores are I would say these are the things that many people don't really didn't really think to grab and use, but they're so incredibly useful. Like so one example, like let's imagine your job is to go scan a bunch of URLs and ingest a bunch of data and you're going to run that like 8 a.m.
Every day or whatever. If you've got 10,000 URLs, it would be way, way, way faster to use asyncio and HTTPX to kick off those requests. But if you just say for URL in URLs, start a task, got 10,000 pending requests and all that data is coming back into memory and it's going to you're going to have a bad time.
Right. Yeah. So one thing you could do is you could create a semaphore and say, you know, I only want to allow 10, 20, 100 in-flight requests at a time. And you just create a semaphore of 100 and you just pass it off to every task and they just have to start at the beginning and, you know, acquire it and release it.
Boom, you're done. You've now created like I only I'm going through 10,000 with a straight for loop and I'm doing nothing more than 100 at a time guaranteed. Yeah. Beautiful. Right. That's a really good analogy. I was trying to think of a good one where because I was actually even looking through the Python code of any cases where they use more than one.
One thing I know that you point this out, I want to just give a quick shout out to any IO. I interviewed Alex. I believe it was Alex from there. I've worked on it and it's got a really cool idea. It's similar to the task groups and stuff that you talked about. And it has this thing called a capacity limiter, which is much like a, it's very much like a semaphore, but a little more async native.
Because you can use it in a async context manager, like a width block, async width. So here I could create just a capacity limiter of like my example is 100. And you just say async width create a group for all the items, you know, tasks start soon, pass the limiter. And all the tasks just do like async width this limiter, do whatever you're going to do.
And boom, now you only get, you know, N of them running at a time. Super easy. Very cool. Cool. Okay. That's not actually what I wanted to cover though. I want to talk about some annoying stuff that got slightly less annoying. Security and preventing people from doing just bad stuff to your, your things online, right?
Web apps and mostly web apps, you know, think about a forum, people come in there, just like bots come in and start posting junk. Like, I don't know why YouTube can't fix it, but I would say about one out of every three shows gets some sort of like disgusting. Come see the, come see the ladies over here sort of post with clearly text.
That was like, why is this still allowed? Right. And, you know, if you run a website, you're going to run into those kinds of problems. So like we had big problems at Talk Python training with a bunch of bots creating free accounts and they're just free. You don't get anything by creating an account.
I don't know why they were doing it, but they, they were. And so I, but they were doing it from hundreds or thousands of different IPs. It was super annoying. So we ended up having to put a recapture on there. And every time I go to the site and I've got to sign in, I'm like, oh gosh, here come the crosswalks and the stoplights.
And that's not a bus. There's no buses. What do you want me to do? You know, like how frustrating is this? So Cloudflare, they're doing a bunch of interesting branching out of just being a CDN. And they've announced this thing called TurnStyle, a user-friendly privacy preserving alternative to CAPTCHA or recapture.
Like recapture is owned by Google. And like a lot of the stuff that you do there gets sent back over there. And it's, it's not ideal. So also finding all the crosswalks, not ideal. So this thing is something they announced and it's free to use. Even if you don't have some kind of other recapture thing, like you're not a customer, you just create an account for free, get an API key for this and use it.
So here, if you go to the page that I'm linking to, the blog.cloudflare.com, all that happens is you go there and it just spins for a second. It does the work and the magic that Cloudflare is already using to verify that you're a human and you're a real machine. And, you know, maybe do some sort of like cost.
Basically, you drop some JavaScript onto the page and a little widget. And then you put the widget in your form. If somebody submits it, it comes with that validation, like some sort of validation code that was created by Cloudflare. On the server side, you want to verify that because its presence isn't enough because anybody could just send like garbage with the right form ID, right?
Over on Talk Python, I switch the things over to it and we'll see how it goes. So for example, if you want to get notified about our mailing list, now you've got to wait about three quarters of a second before typing in your email address. Unless you're like some kind of beast and you can top it in, like select the form, type your email address and hit subscribe before like a second is up, right?
So I created for the moment a GitHub gist that has the server side implementation for anybody who wants to do this in Python, modern Python anyway. Nice. Cool, right? So over here, you just need this one file, this one module you can use here. This one's synchronous, but you could just as well use HTTPX and async if you want to do this validation in like an async view.
So it has Pydantic models, like here's what you got to send over and here's what you'll expect to get back from the API endpoints, little examples. And then just has one function called validate, given the thing they submitted in the form and their user IP, just so you have that as like part of your information of what happened.
They need to know that. And that's it. And it's kind of a cool example of seeing how to do like request response with Pydantic to APIs as well. That's pretty cool. Nice. Awesome. Well, that's a really good stuff. So here's the thing. Right now, it's not a package. I'm not sure I need more open source projects to manage.
However, let's say if I was curious why you didn't make a package out of that. Well, because I only did this like Monday, right? So it's only I kind of wanted to stabilize a little. If sufficiently many people shoot me a text on Twitter, like a at mention, M. Kennedy, says, hey, please make this a package so I can use it too, more directly, or we could all contribute.
Yeah. So one of the things for extras, I wanted to pop up just briefly. I was looking at a, so I was looking at, oh, let's go yours too. Do you have any extras? I do have some extras. Sweet. You go first, though. I have a ton. Okay. So I just have a couple. So this article says it's from testdriven.io, Heroku alternatives for Python-based applications.
And to be fair, it's pretty fair to Heroku also. So really, it isn't don't use Heroku. It is, it's kind of like, what are all the things you can use? So it goes through Heroku, of course, but then it also goes through, does the pros and some description and pros and cons for DigitalOcean app platform and render and fly.io.
And even some that we don't hear too much about, like Engine Yard, Railway.app. Railway looks pretty cool. Yeah, Railway does look cool. So really having one person compare, look at all of these and compare them. It's just kind of a nice thing to just hear what somebody has to say about all these different platforms that you might want to try out.
Yeah. And so that, and that's actually one of the fair comments. It says, like, for instance, Heroku, one of the things that says is the price can go up as you, it's not too bad at the start, but it can go up as you grow. And same with all of these really. But the, and it's how many times more than running it just directly on AWS.
So the other thing I wanted to bring up was we brought it up last time and maybe the time before the pytest course is going well. So I'm glad people are getting use, use out of the pytest course. But I wanted, I've been kicking around the idea of doing a, a cohort based thing, something like the pytest course, not the exact same content, but similar sort of things.
It'll get to me. So yeah, let me know. Those live events are really nice, right? It's, it's, it's a cool experience. So hopefully that, that goes off. Yeah. That'd be neat. Indeed. All right. Would you believe that just last night I was writing some C code? No. I typically do. I'm like, come on, another semi-colon.
I forgot. Are you kidding me? Been a while. But I actually did a pretty good job without pulling up any references or anything. So more on that later, but I just want to give a quick shout out to this project called CPP Front from Herb Sutter. It's an experimental compiler for an alternative syntax to C++.
That would make the language 10 times simpler, safer, and more coolable. I think it basically is, as the name would say, like CPP Front is like a slightly different language that compiles to C++. So it has an interoperability and whatnot. People do a lot of C++, like, you know, for low-level integration with Python and things might find this interesting.
TypeScript is definitely something that's going somewhere. Matt Kramer pointed out that Jeff Glass has been doing a bunch of things with iScript lately. So I want to highlight one thing and then I'm going to dive into it more next week. I just want to put this on people's radar. So PiScript September edition is out with lots of changes on it.
So probably the easiest way to see that is to click on this blog post they got down there. So if you've been doing any PiScript, you kind of want to know about this because it's like super breaking changes. So for example, you used to have to say, use this thing Pi ENV for configuring like what libraries you would use and how you execute it.
Which is good because like my TV can no longer do bad things that it was doing. You know, my kid gets ad blocking without knowing it. Even visitors, I suppose. Right. And I was like, all right, well, let's see what breaks. Who goes, Michael, I can't do this anymore. Like nothing broke, which is great.
It's like 80% everything is just killed before it ever gets through the network. Oh, I was curious what the number was. So it's not 80 get through. It's 80 get blocked. It's 80 blocked. So you can actually scroll through. It'll show you like the wild cat Giphy wasn't blocked. The static image wasn't blocked, basically.
But, you know, pretty much everything else was blocked. So, yeah, pretty awesome. Yeah. Yeah. And last thing here, one of the things you got to do to make this work, this is like a new extra, but it is like one flows into another. One of the things you got to do to make this work is if your IP address changes and you have a crap router, I have like an insanely good router that has crap configuration.
Let's put it that way. Where I can't do IP over, sorry, DNS over HTTPS or set up any details. I can just say, here's the four numbers for the DNS server. Yeah. Right. Just the IP address. Well, if that's the way you do it and you have some account that does blocking, you've got to go into your DNS account and associate your IP address with your account by just like clicking a button or calling an API endpoint.
But what do you do when your IP address changes? How do you remember? Like all your ad blocking stops every time, you know, maybe the power goes out and you've got to like your things come back on and they get a new IP address. So I was like, well, what could I do? I've always wanted to have like a little circuit Python project that would be fun.
So check this thing out. See, I got on the screen here. This Adafruit ESP32-S2 feather is a full-blown circuit Python in Arduino, basically, for $17.50. And it's in stock. Nice. So I got that yesterday. And just to give you a sense of how small it is, like if you put it in your hand, it's probably two-thirds the size of your hand.
And it's a full 240 megahertz computer, four megs of RAM, Wi-Fi, Bluetooth, temperature. But the most important thing is Wi-Fi, make no doubt. And I see Paul Cutler out there. Yes, thank you, Paul. I was actually thinking of you last night when I was working on this. It took a bit of time to set up and it was a little bit painful, like getting it configured from originally it was Arduino, which is C.
So I was writing. I tried to do all this stuff in C, which basically tried to like hold that endpoint and pull out my IP address. And everything seemed fine except for nothing I could find supports HTTPS in C. All the REST frameworks only supported HTTP unencrypted. I'm like, what endpoint is unencrypted these days?
What is this madness? Give me some requests. So I got CircuitPython going on it. And now it's happily sitting over there in the corner, sending me a little rake reports through like sort of like Morse code through its LED. And then, yeah, it's beautiful. I just plug it into the power and it just goes constantly, checks my IP address, updates that DNS setting periodically with an API.
Okay. So it's monitoring. And if your IP address changes, it uploads it? Yeah. Basically, it just has to call the API from the network and that'll like go, well, wherever you're coming from, that's where we're going to treat you as being from. Okay. Nice. Yeah. You might say, well, why don't you just run that Python code on your computer?
Oh, yeah. It was still fun. Yep. All right. Well, those are all my items. I know I had a bunch of extras, but there they are. That's all right. Well, do we have a joke? Oh, you know that we do. So following on, like one of the things I had last night, I was like, God, why, why can I get CircuitPython installed in this?
And it was, when it came out of the box from Adafruit, maybe I'm missing something, but it didn't show up as a drive. It only could be communicated with over the serial port, right? And the way that you normally install CircuitPython is you drag this bootloader just over the drive and it just restarts and off it goes.
I'm like, well, how do I get this on? It just wouldn't work. Wouldn't work. And I'm like, I'm trying all these things. And eventually I figured there's like a sequence of little clicks I got to do on like some of the buttons to like put it into a mode. Then I could do it. I could have used some help.
That's pretty funny. Yeah. I bet it works too. Yeah. Right. If you went in there, like what you need to do is just type dash R, you know, RM dash RF on forward slash. No, no, no, don't do that. Yeah. It's a take on a different take on it. So I used to do the similar thing of just a post, post a blog post with the wrong answer and, and have, or a bad tutorial and leave comments on and, and then let people tell me what, how much of an idiot I am and then delete that and write the real post.
