- Podcasts
- Python Bytes
- #99 parse - the regex antidote in Python
About this episodeTopics covered in this episode: parse fman Build System fastjsonschema IPython 7.0, Async REPL molten A Python love letter Extras Joke See…
- parse
- fman Build System
- fastjsonschema
- IPython 7.0, Async REPL
- molten
- A Python love letter
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#99 parse - the regex antidote in Python
Hey everyone, Michael here. I want to take a quick moment for an editorial comment before we get to this week's show. On the last episode, we covered a story from Bloomberg about China implanting hardware hacking devices into motherboards for servers. Since that article came out, there's been a lot of pushback from the organizations named Amazon, Apple, and others.
And there's been a few articles that raised some doubts about the veracity of the original Bloomberg article. I'm linking to an article in Forbes called Doubt Swirl About Bloomberg's China Tip Hack Report. This doesn't mean the original article is false or implausible, but it may be. And because of that, I felt like I should add this disclaimer and warning about the coverage we had on episode 98.
Yeah, thank you, DigitalOcean. Check them out at pythonbytes.fm/DigitalOcean. Get $100 credit for new users. I think I had said this before as a joke to you, and you didn't necessarily agree with it, but your first item here may belial some agreement that if you have a problem and you solve it with a regular expression, you now have two problems.
Yes. Yeah. Well, definitely. Within code, at least you have two things to support. Yeah, that's right. But you've run across some library that's actually really awesome for simple, what you might think of as red jacks problems. I got this from a tweet from Kenneth Reitz. He said, oh, yeah, everybody, by the way, parse is a thing or something like that.
Parse is a library that the tagline is it's the opposite of format. So in the general sense of it, there's a bunch of things you can do. You can parse strings. You can search inside strings. You can find all the element patterns or whatever from a string. But you give it both the string that you're searching and then also a instead of a regular expression for what to search for, basically the same string, but with parts of it replaced with the curly braces or something like that to say, if I were to have printed it with format using this string, I would have gotten this output and reverse that out.
And then you can get the results out to see all the stuff. That's awesome. So you could say like, this is episode curly curly of Python bytes. And then you could actually parse it. And that little curly curly would say, give you the number. Well, I guess the first example would give you a string and you could put a colon D and it would actually give you an integer 99.
And the thing I like about it is it's more readable than a regular expression. So if you've got something simple like that, that you've got multiple people that have to be able to support it, I think this is a good choice. Yeah, I love it. It's a really cool example. And you can tell that it's probably written by somebody who understands Regex well under the covers, but you don't have to think about it because it has like a compiled mode and things like that, which Regex often do.
Can you believe it? Yeah, we've covered that a few times. I think we have. So this one is called the Fman Build System, and it comes from the project, which is like a dual pane file explorer for Windows and Mac and so on from Michael Herman. So it's a pretty cool project, but I'm not interested so much in covering the desktop app that he built per se, but the tool to build it.
So the Fman Build System, right? So what it lets you do is it lets you create GUI apps for Windows, Mac, and Linux, as in here is my .app file or my .exe. Go click it. In fact, it gives you an installer, right? And like a proper installer for Windows, one of those, you know, drag here, and it has like the applications folder for a .app file, disk image, and macOS.
I mean, this is nice. Wow, that's kind of like one of the missing pieces that we've had for packaging and sending out things. It really is, right? It's quite cool. So like I said, Windows, Mac, and Linux works really well. It's what he uses for his project. It's open source, so you can use it for free on open source projects.
It's licensed under the GPL for commercial stuff, so you can buy, basically buy a license for it. Now, if you're using Qt, you also have to buy a license for Qt, and that's kind of a complicated story. Looking to figure out a little bit more about that, honestly. I don't really know the full story there.
Yeah, and if the point is to package Qt apps, it's almost probably unavoidable. Yeah, definitely. And I also like, I got to give a shout out to Michael Herman. It's not trivial to say, take a piece of your project and pull it off, as he did with the build system, so that it can be usable for other people.
That's pretty cool. Yeah, absolutely. So, JSON, not a whole lot of validation there, is there? Well, I think there's a lot of ways to validate JSON, but I don't know if everybody does. I don't think I've ever, in all the times where I've used JSON to talk with different parts of an application, I usually just kind of assume it's all working right.
Yeah, for sure. But there are validators out there, and this one, the one I want to cover, has been recommended by a few people. It's, the documentation is a little light, so I think it's called Fast JSON Schema. But the name is descriptive. Yeah, definitely. And I'm not sure what the, so one of the articles that I'm going to link to has got four different libraries, including Fast JSON Schema.
And I'm not sure what they were validating. It's like way faster than everything else. So, JSON Schema takes five seconds. JSON Spec takes seven. And then his was the Fast JSON Schema, 250 milliseconds. And I'm not sure how big of a data set this is to have anything take five seconds or seven seconds.
And I think there's some optional keywords and stuff like that you can throw in there. And then you compile it, you compile that into your own validator. So, this is a, like we were talking about with Regex, you compile it so that it runs faster. And that's what this does. It just comes up, you compile your own validator.
So, if I have like an address, I could say, okay, here my schema is. This is an object that has properties like post office box and extended address. Those are strings and so on. You can even have like dependencies and stuff like so. The post office box must be a valid street address, which is defined, you know, elsewhere.
So, this is pretty cool. You take those files, you feed it to this validator, and it'll take anything you get back from, say, a web service or something and say, yeah, this is valid or not. And this project's been around for a while, but the big news lately is that there's multiple drafts of this JSON Schema.
And the tool we're talking about covers drafts 4, 6, and 7. Right, which is pretty nice. Cool. Yeah, very cool. So, there was other ones, and they were apparently kind of stale, didn't cover the latest drafts and things. So, nice find. And it's way, way faster. Now, before we get to the next one, I want to tell you about a cool feature at DigitalOcean.
So, at DigitalOcean, you can, of course, log in and say, create me what they call a droplet, a new virtual machine, or various other things, load balancers, firewalls, and so on. And it'll spin up your machine, and off you go. And you get some choices, like various versions of Ubuntu and other stuff.
But what you can do, if you'd like, is you can create your own local virtual machine, whatever you want, some kind of Linux, as long as you can install a few dependencies that it needs to interact with the DigitalOcean infrastructure, and upload that. And from then on, you can just click a button and say, create me my super special private server, as many as you want, with your API or whatever.
Very cool. Definitely. Yeah, pretty cool. So, if you want more control over how your virtual machines get created and what they even look like, check them out at pythonbytes.fm/digitalocean. New users get $100 credit. And they've got a bunch of cool stuff that you can do with all their infrastructure.
If I have an async library that I want to use, basically, the only way to use it in IPython previously, I believe, was to spin up all the infrastructure to sort of host the async loop yourself, which is like five or six lines to just call the function. So now in IPython, you can just say, oh, wait, give it a function, and it just runs it automatically.
And talked about referring to it as legacy Python, which I think is great. So he also wrote this, and he works on IPython and whatnot quite a bit. Talks about how when IPython drops support for Python 2, how are they able to sort of make these features possible, right? If you want to support these types of things, it was much harder to do so if, you know, you want to use a Python 3.5 feature, but you also want to support Python 2.
So it's cool how they talk a lot about that. Yeah, and I also think the open source community is sort of changing. We had this idea that kind of from, I think, other commercial applications that you should support as many platforms as possible or, like, your library should support as many versions of Python as possible.
And the easiest way to maintain code is to not have it. Yeah. So, yeah, it's a great point. And here's another example. A lot of people are using IPython to teach Python. And whether or not that there's a debate as to whether or not that's a good thing. But at least now they'll be able to teach async.
It's an API framework. So, the link we're going to include has a little video demo on it. But it's like a REST API framework used similar to like API Star. And in fact, the kind of the motivation, there's a motivation page that talks about how API Star is kind of awesome. But there's some of the implementation like a hook model for middleware that this author didn't quite like.
But there's also built in... Speaking of schema validation, there is schema validation built into this system so that you know the code that you're writing to deal with requests or the coming in. They're already going to be valid before it even hits you. So, you won't be hit with invalid data, which is pretty cool.
Yeah. There's a lot of cool validation. So, for example, the hello world type method for a web view method is just like def hello and then name colon stir age colon int. And it actually, you know, grabs the value, say, out of the URL or somewhere, puts it in there, converts it to an integer, and passes it.
And so, you don't have to figure out, you know, how do I go get that from the route match data or other weird data sources like that. So, that's really cool. And then they take it farther. You say, okay, well, you could have like a string and an integer in the function. Or if you've got something more interesting, you could define an actual class that has a little decorator.
It takes a to-do item and it automatically pulls it out, does the validation and check-in. Yeah, I'm loving this. This is great. Yeah, and also, you can define the schema on the output as well to make sure that you're complying. I think it's kind of neat. And the other, there's a couple other neat features of it.
It's, or at least features of it, whether or not you like it. The middleware is a functional programming-based middleware. And a lot of the different pieces, like if you want to have a database management, they're all set up to allow them to be isolated easily. So using dependency injection, it's a thing.
Yeah. So I want to round out this episode with something a little fluffy but nice to just remind everybody why we're here and why we use the tools and the technology that we do. So this last one is called a Python love letter. Well, I love Python. Yeah, I love it too. So this was actually a thing posted to a Reddit thread by a guy who's pretty new to Python.
And he posted, it said, Dear Python, where have you been all my life? Right? And the thing he posted was pretty interesting, but also the comments, right? There are many, many comments and just all the people either agreeing or disagreeing or whatever. So the guy says, look, I'm not a developer, but I've been teeing with programming for, you know, Basic and Perl and whatnot.
And for some reason, he decided, you know, he's done with shell scripts. So we've heard that before, right? He's going to go write some Python. And he said, look, I went and I learned Python. And no, I didn't go from zero to production in a day. But if my coworkers will leave me alone, I might be in production tomorrow.
I came up on C++. My job highly trained me in C and assembly. And every project I touch, can't we do 90% of this, 95% of this in Python? And we do, right? We don't need inline assembly most of the time. Another person said, I have a chip on my shoulder. I want to do things the hard way and understand them.
So I went C++ because that's real programming. Dang it. But later, after suffering a lot, I kind of learned that, you know, doing things smarter was way better than doing the hard way and whatnot. So he loves, you know, sort of found his way to Python. I guess one other person said, I felt exactly the same way.
I decided to learn it. What a breath of fresh air. Sadly, there are a few things in my life that make me feel like this. Python and Bitcoin give me the same levels of enjoyment. I've used Java, Groovy, Scala. Objective C, C++, etc. And nothing feels as good as Python does. So definitely, definitely cool.
And then this person, this is what was notable to me, Brian. Closed out his comment is, hell, my next two planned tattoos are Bitcoin and Python logos on my wrists. Way to go. That's some commitment. The Python, fine, but you're probably going to regret the Bitcoin one. Is there an abstract cryptocurrency that is going to encapsulate like whatever comes next?
And it's power mode. And I'm linking to something called Power Mode 2, which is a plugin for PyCharm. But there's power modes in a couple different. And it started in Atom, I think. And people have probably done it other places. It just makes programming more fun. This is funny. You introduced this to me.
So let me just sort of give people a little description. So imagine as you start typing, it's kind of like a bit like a comic book. The faster you type, the more excited your editor gets. If you copy and like duplicate a method, like a big bam, pow, thing will pop out. Sparks shoot off of your cursor.
The faster you type, the more intense it gets. Yeah. It's super, super productive and awesome. I've left it, I've turned off the shaking screen, which is a little unsettling to me. And the flames. But the rest of it, the sparks flying and everything like that. I've been using it for like a week or a week and a half.
And get the full show notes at Pythonbytes.fm. If you have a news item you want featured, just visit Pythonbytes.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.
