- Podcasts
- Python Bytes
- #249 All of Linux as a Python API
About this episodeTopics covered in this episode: Fickling Python Project-Local Virtualenv Management Testcontainers jc What is Python's Ellipsis Object?…
- Fickling
- Python Project-Local Virtualenv Management
- Testcontainers
- jc
- What is Python's Ellipsis Object?
- PyTorch Forecasting
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#249 All of Linux as a Python API
Hey there, thanks for listening. Before we jump into this episode, I just want to remind you that this episode is brought to you by us over at Talk Python Training and Brian through his pytest book. So if you want to get hands-on and learn something with Python, be sure to consider our courses over at Talk Python Training. Visit them via pythonbytes.fm/courses. And if you're looking to do testing and get better with pytest, check out Brian's book at pythonbytes.fm slash pytest. Enjoy the episode. Hello and welcome to Python Bytes, where we deliver news and headlines directly to your earbuds. This is episode 249, recorded September 8th, 2021. And I am Brian Aukin. Hey, I'm Michael Kennedy. And I am Eric Costanz. Hey, Eric, thanks for joining us today.
Yeah, thank you so much for having me. So tell us a little bit about who you are. So first of all, I'm a longtime listener to the show. I just told Michael who I'm listening since episode one of this podcast, actually. Also listening to Michael's podcast, obviously. And then once I get to know it, I started listening to your podcast as well. So basically everything that's out there, I'm listening. What I'm doing, I'm currently leading the competence center for AI and data science at Data Drivers, which is a consultancy firm from Hamburg, Germany. Our focus is mainly on building big data platforms and applications, mostly using cloud native services.
And we try to apply best DevOps and MLOps practices to wherever we are. That's super cool. Do you have a favorite cloud? In all honesty, probably Google Cloud. Gotta say it. Yeah. Yeah, nice. Well, Michael, why don't you kick us off with our first item? Yeah. This one's a little fickle. Comes to us from Ollie. He sent that in. Thank you, Ollie.
And sort of indirectly from Patrick Gray over at Risky Business, which is a cool security focused podcast. Python supports security. They talk about it over there. So you've heard of pickles, obviously pickling in Python. It's like, I want to take this binary, this binary Python object graph and turn it into a blob that I can stash away and then later get it back.
Sometimes it's real simple. Stash it in Reddit and other systems can pull it out real quick as a cache, maybe save it to a file. But where it's become really popular as a means of data exchange is actually in machine learning. So the people who built this thing I'm going to tell you about were really built it around focusing on the machine learning use case because people are handing around these models, these pre-trained models and like, here's the model loaded up and roll and loaded up and roll may mean you have an amazing artificial intelligence that drives a car, or it may mean that you have a virus because pickles can contain all sorts of bad things. All right. So this thing I'm going to tell you about is called fickling like pickling. It's a decompiler, a static analyzer and a bytecode rewriter for Python pickle object serializations. So you take these pickle files, these object graphs of Python things, and you can pull them apart and look at them. You can ask questions like,
is it a virus? And you can even say things like, let's put a virus in it. So all of these are possible with this tool. And it's made by a security pen testing company called trail of bits for basically that purpose. Right. So it's kind of either side, the attacking pen testing side or the defensive side of the story. So it works on three, six and above, and you can see it's super simple. You say, you basically do pickle stuff and you say from fickling dot pickle import pickled. And then you can kind of, as if you would use the dis module to disassemble Python code, you can do that with this pickled library.
And it'll print out something that's kind of like an abstract syntax tree of the pickle. And they've got a real simple example on the GitHub repo. It's like a list of four numbers, one, two, three, four. And then it just shows you, look, we're assigning the results of creating a list and setting these constants in it.
Another thing that is nice about this is it's not specifically built for Python developers. So it's also kind of something you can integrate into other tooling and say continuous integration and stuff like that. So you can run it off the command line as well. You can just on the, you know, terminal to type fickling and give it the data and then outcomes from answer. The one that people might want to do is the --check safety. And that will try to look and see if it's doing bad things, like for example, talking to OS dot system or doing other malicious stuff like that. So that's good, but I wouldn't trust that entirely. Like how well is it checking, right? If you, for example, were to encode Python code and then decode it and then take that decoded stuff and it did OS something, right? Feed that to a val or whatever. There's all sorts of layers here, right? So it can check for obvious things, but you know,
Yeah, exactly. So maybe I'm imagining something like a little thing that counts that like prints out in, in like flashing bright colors. We told you you shouldn't unpickle untrusted data. Don't do it. It's like a little bit of a beginning hard drive format. It has like a loud beeping sound. It was three, two, one. And just like, obviously not really do it, but like that would get your attention, right? That'd be a mean trick. But absolutely.
This is interesting. And you know, I didn't really put it together with the ML data exchange model exchange story until I heard the folks talking about it over on risky business. So it seems like, especially in the ML story, you want to have a look at these kinds of things. Yeah. So I've heard about the use case before, actually, but I didn't know that somebody would, would solve it in this way. So pretty nice.
Yeah. Well, is there better ways to store it? Yeah. Is there better ways to store it? Like JSON or something else? Models don't have to exist that way, do they? Yeah. I mean, even if there were there, there are some projects that focus on building like some reusable interface across all these different frameworks and stuff. But in reality, people just use pickle.
Yeah, really? Yeah, they do. I just didn't know anybody was really using it for much. No, it's absolutely common. So within like, say, scikit-learn, which is probably most used library ever, you just use pickle on the build, store your files. Yeah. All right. Well, cool. So this is a useful library from Trello Bits. People can check out and we're going to start with everything is fine and we'll end with everything is fine as well, Brian. But over to you.
Okay. Well, this is something it's a blast from the past a little bit, about a year ago. Anyway, I want to talk about virtual environments and directories. So, and there's an article from Hinnick that's called Python Project Local Virtual Envy Management. That's a mouthful. But the idea and we've talked about wanting this before is to be able to still want it. Yeah. So just to go if I've got several projects going on, whenever I like CD into the into a directory with this project, I just want the virtual environment to activate automatically.
And then when I leave it and go to another one, it's just automatically switched. Apparently that already works and we've already covered it, but I missed it. So actually in episode 185, you brought up Durinv and in part of it, it's the ability to, you can have per project isolated development environments.
Yes. But I didn't pick that up yet, but Hinnick just said, this is how you do it. And how you do it really is just, you just have, you have to have, you have to install Durinv first. And then you put a .env RC file in a directory and say layout Python and then what Python version. So like layout Python, Python 3.9. And then that's it. That's all you got to do.
Oh, okay. I mean, that sort of semi solves it. Yeah. Yeah. So I really, I probably have this need more within a windows than I have in, in, on my, my Mac, but I have it in both places. So I'm, I'm going to start using it. It's great. Plus like you covered last time you can also have a bonus. You can put environmental variables in there too. So that in the project, you've got your, like your, perhaps your secrets or, or just different environmental settings you want to use.
Yeah. I think people will look in your .RC, whatever your bash RC, ZSHRC, whatever files for your secrets. But I suspect it's much less likely to go hunting through virtual environments and looking for their activate scripts and see what's in them. Yeah. People, people know, but fewer people know that stuff gets stashed in there. So that's probably good.
Right. So, I guess mainly the story is, I knew that you could do it, but I didn't realize how easy it was. So this is, it's super simple. It just took a little bit. and then I, my, my second thought was it isn't, it's not that hard to create virtual environments though. So this is saving any time. I still got to create this file and put this stuff in it. It actually is more typing a little bit more, but it didn't take me long to realize that it's when you're switching between different directories, you save a ton of time. So.
Yeah. So going back and forth between projects, right? Yeah. So that's it really just kind of neat. Yeah. Brett out in the live stream has got a comment for us. If you use pyenv, you can run pyenv local env name in your project folder and get this behavior as well. How do you do that? How do you get it to, activate by just changing directory into it? Is what I'm not totally sure. Yeah.
Yeah. I think you get the Python version that way, right? But not the actual virtual environment. Yeah. Possibly if you, if you've installed Python through pyenv as well. Yeah. And then David has a comment back. The first topic out there in the live stream. Hey David, the irony of legacy object serialization being used on cutting edge machine learning.
Like that one. Yeah. And then Teddy at the live stream. Hey Teddy says, does it work with an IDE? I changes the interpreter based on the folder you're in within a workspace in this coast, for example, that I don't know, but I was going to add the personal comment that I don't need this nearly as much as I felt like I used to, because the way I jump between projects is usually jump, open them up in PyCharm and jump between them there.
Pyenv adds a shim that intercepts the calls to Python. So yeah, very good. So it must be that you have to install Python through Pyenv, but then it'll also do this. Very cool. Good to know. I didn't know that. Me too. Yeah. Nice. All right. Eric, first one is for you. Yeah. So I brought with me the test containers Python library, which, and let me quote this one from the description, because I think it's a pretty good summarization.
So test containers Python is a port for test containers Java that allows Docker containers for functional integration testing. It provides capabilities to spin up Docker containers, such as databases, Selenium web browsers, and any other containers for testing. So maybe not that many new things in here, but we use this in a project lately.
And especially we use this in integration pipelines using cloud native services. So there's a container for Google Cloud Pub/Sub, for example, which is pretty amazing. Also for like your Kafka. This is originally a Java project. So there's still a lot to do for the Python community in order to catch up on a bunch of interfaces that need to be implemented and stuff.
One example, it is here. Let me just show you that one. So there's in the repo, you can find an example of how to use this within your CI pipeline. So what's happening here is actually that if you have like a standard CI pipeline for your integration test, which consists of Docker containers that we use Docker in Docker to actually run the integration test.
Yeah. Yeah. I can't wait to try to play with something like this. Yeah. We talked about this way long ago. I brought this up, I believe. But I'm glad you brought it back, Eric, because it's really useful and it's really neat. And there's more stuff than actually is listed on the readme for some reason.
It's definitely challenging. So stuff like this helps. Yeah. You know, to me, it's an interesting trade-off because on one hand, sure, you can mock out your database and then just test against your test data. But then if your data model and the database changes, but you don't think to update the test data, well, then your code's going to, like SQLAlchemy, for example, will freak out and crash if the scheme is not a perfect match.
Whereas you wouldn't find that in testing if you weren't letting it talk a little bit to the database. I think there's just interesting things like this. Brian, you even had an episode about not mocking out your database, didn't you? Yeah. I think as little as you can, I guess, let's do it the reverse.
As close as you can have to the real environment, the better. And this is when people are deploying on containers. Testing with containers makes total sense. Yeah. Absolutely. Absolutely. All right. Want to talk a little more infrastructure? Yeah. All right. So I have the one, it's got to be the shortest named thing for a featured item.
JC. Two letters. JC. So JC comes to us from Garrett. Thank you, Garrett, for sending that in. And at first I was like, I don't know if this is relevant to me or if this is interesting. But the more I looked at it, I'm like, yeah, this is actually pretty awesome. To me, let me, I'll read what JC describes itself as in a moment.
But to me, what this is, is it is basically what web scraping is to the web. JC is to Linux. So there's not a nice API for it, but I'd like to somehow wrap a little Python magic around it and then have an API for it. Okay. So it's official story is it's a CLI tool in Python library that converts the output of popular command line tools and file types to JSON.
And it allows piping one thing to the next, obviously, because it's Linux like. So the idea is, you know, the example they have on their site there is dig. So dig is a command that'll give you information about a domain. So you could do something like dig example.com pipe JC, and then you tell JC what it's expecting output from just whatever the print output to the terminal is in dig.
So think of all of these different Linux commands you may run. You find a whole bunch of them. They're like a huge list down here. So airport, ARP, crontab, date, CSV, free, DU, hash, history, hosts, IP config, netstat, all those types of commands, syscontrol. So for example, if you're automating daemons and stuff like that, you can now do that from Python.
I suspect it's not extraordinarily hard to do to add another one. Yeah. Yeah. But you can also run it on the command line. You don't have to use it in Python, which is what I was scrolling around looking for. So if you want to, like, let's suppose I want to go and run dig and I just want to go to the answers and get the data, which would be the IP address of some domain.
You can say JC, run this thing, and then JQ-R, or there's like a way to just pass over a string. And basically the string you pass in is the object dereferencing, the traversal of the dictionary. So dot, bracket, dot answer, bracket, dot data, and it'll go and pull that all apart, which is pretty neat.
Yeah, yeah, yeah. Exactly. Eric, what do you think? Yeah. So it sounds like I found something that I can put my usual Sunday afternoon time into. So I'll play around with it. Yeah, yeah, yeah. Exactly. Yeah, because every now and then I want to do some subprocess thing and it needs to call some kind of Linux command.
Like you can parse pip list and pip show and YAML and XML with this as well. So that's pretty cool. Yeah, yeah, very cool. All right. How about some ellipses or I don't know how else to say it. Dot, dot, dot. The next thing. Do say more. So this was a surprise to me. I guess I haven't run into this yet.
But at the same time, it's like, that's really what you wanted to put down there. It's like, I just don't want to put anything, but Python won't work unless I kind of close this off. So here's a pass, right? Well, also, one of the things I was thinking about is, no, I would probably use pass all the time in that case.
But when writing documentation and you really want to have a working code example, but you want to just indicate there's going to be more code there, that's a cool thing to put in. Anyway, so there's that. And then there's also using it in type information. So with type information, for instance, apparently, like, let's say I've got a function that returns a tuple or tuple.
Apparently, it's used also within FastAPI and Typer, but it's there. And if you want to use to implement a certain feature where that might make sense, it is a thing that's available to you. Like, maybe you could have an operator, a dot, dot, dot operator on your something. So I learned this just the other day from a tweet from Raymond HedJinja, where he was asking people, like, how would you do this?
I imagine. I don't know. It should work, right? It should work. Yeah, it should work. Yeah. Nice. I'll try it out while we go on to the next topic. Yeah. That one surprised me. Well done, Florian. Yeah. So the last one that I brought with me, actually, since I lead the data science and AI team, I got to bring something with me that has to do with it.
So I brought with me the PyTorch forecasting library. So, Michael, you just used this analogy a couple of minutes ago. So I'm going to use an analogy now. So for me, PyTorch forecasting looks like what a fast AI does for computer vision and natural language processing. It does for time series forecasting.
Because there was like a lack of deep learning for type series forecasting. And actually, I think that PyTorch forecasting is going to close this gap. So it comes in with a bunch of important features, actually. So it's built on top of PyTorch Lightning, which allows training on CPUs, single and multiple GPUs, basically out of the box.
So there's been a lot of software engineering involved for the data scientists in the past. And this library just makes it pretty simple. So you have to work very hard in order to mess things up. with this library, I guess. So what it also brings is an implementation of a model that is called the temporal fusion transformers.
So this is from Google Researcher. Actually, there's also a TensorFlow-based implementation. I'm going to put the link to the paper in the show notes. This is a very interesting model that has performed pretty well on a dozen prominent benchmarks very lately. And it has a very huge benefit, which is that it is pretty interpretable.
So it does actually calculate feature importance for you. So this is, in the real world applications, very important. Because whenever you stick your data into these models and something good comes out, people will always ask you, so what was the important part of the data? How does it influence the model and the outcome?
Yeah. Yeah, this looks really good. So if you want to predict the future about sales, home prices, heart rate, whatever, right? It comes up all the time. It comes up all the time. And I know from a couple of guys who work for the Google Clouds of this world and the AWSs, that within these software as a services or these APIs that they provide for, let's say, a demand forecast, they use this temporary fusion transformers under the hood.
So, yeah. Yeah, and also, yes, you can return it from a function. Nice. Just fine. And then, let's see, someone out in the live stream asked if it has methods. Does it have methods or anything that you can do to it? That was Teddy. Yes, but only the built-ins, right? I don't think it, from object, I don't think it does anything interesting besides just be dot, dot, dot.
Yeah. And then Anderson, hey, Anderson, it's a pity the ecosystem is moving towards PyTorch lightning. The separation of concerns there is not very nice. In my opinion, PyTorch ignite does a better job in that aspect. Eric, that's all you. Yeah, fair enough. Fair enough. Still, I mean, one thing that you've got to keep in mind.
So, speaking of separation of concerns, right? There's so many data scientists out there that if you throw like separations of concerns at them, they just answer like, yeah, here's my model. So, what is separation of concerns in this sense, right? So, if this works, if people use it, it's probably good.
Very exciting. Nice. Awesome. All right. I got a couple to throw out there. Really? What a surprise. Can you imagine? What a surprise. Can you imagine? So, remember we talked about several things. I talked about how I turned off all of the tracking stuff and all those things on the website, which I think is good because so many people run ad blockers.
I built some automation that will download all the Intent X log files, some of which are text, some of which are gzipped, and then run this thing across it and it will build like one giant monthly log thing. And then goaccess can then turn into nice, beautiful reports. So, very excited to have goaccess working well.
And instead of running on the server, I actually just download and then run it on like a monthly report locally, which I think is kind of cool. Yeah. All right. One, we had some feedback about Caffeinate. Remember Caffeinate? You can type Caffeinate on the macOS terminal and it'll keep your system alive.
So, you could reverse it if that script doesn't use keep awake or I think that's what it was. Right? So, you could apply Caffeinate to your Python code and just say, no, stay awake while you're doing this. Or you can even apply it to a running process using a PID. So, it just stays awake while that process is running then?
Yeah. And then it'll go away. Yeah. Oh, okay. Nice. Yeah. So, it's like the reverse of what we talked about then. Then Sean Tabor from Teaching Python said, isn't this what we were asking for? Remember, we were talking about the keyboards? Keyboards. And here's a Python one. This is a M60 mechanical keyboard, the open source USB, BLE, Bluetooth, low energy, five, hot swappable, 60% keyboard powered by Python.
So, this one comes with Python built in, which is pretty excellent. So, if people want to play with that, they definitely can. The next one I want to throw out there real quick comes to us from Mark Little, a friend of mine here in Portland. And basically, the subtitle is that, this is an article from CNBC Finance News, that open source is booming.
So, the headline has to do with MongoDB, but it's more broad. So, if people are interested in kind of following up on that, it's kind of cool. So, MongoDB surged on Friday, which was last Friday. It's now worth as much as IBM paid for Red Hat. Databricks raised private financing around at $30 billion valuation.
And just, you know, these are the mega open source companies, but it's pretty interesting. To just give you a sense, like, I read this article, I got it. It's pretty interesting. These numbers kind of just like bounce off me. But the one that made it stick for me was MongoDB was a private company for a while.
Then it became, then it IPO'd, right? It had VC money, then it IPO'd. Do you have a sense? Either of you have a sense for how much it IPO'd for? It seemed crazy, right? Like, like a $1.2, $1.4 billion. MongoDB is worth $30 billion now, right? So, even after like the crazy IPO, you know, $1.2 billion to start and now over $30 billion.
I use FFMPEG for some of the audio processing and other types of things around both the podcasts and the courses. So, attribution here. This is from Jim Anderson. Sent this over. Thanks, Jim. FFMPEG.wasm. So, here's FFMPEG, which is a very popular tool in that world, but as a WebAssembly thing, which is pretty awesome.
And I'm trying to remember what the name of the library was. But over in, we did talk about on Python Bytes, I think with Cecil Phillip on one time. Maybe it was even him that brought it up. But there's a Python library that will run WebAssemblies. So, not run WebAssembly in their browser or put Python in their browser, but reverse it.
Like, I have a WebAssembly library that does cool stuff. Put it in my Python code and run it here. So, you could take FFMPEG.wasm and pure Python and have like a no dependency sort of audio video processing tool in Python, which I think is pretty cool. Cool. All right. Last one. I told you we'd start with everything is fine.
That one makes me way more nervous than typosquadding or their weirdness. And it was a vulnerability in the way that they were doing GitHub actions with PyPI, which allowed a malicious pull request to execute arbitrary code over there, which is not ideal. Nice. Yeah. But I'm glad to hear that's fixed.
And yeah. Awesome. We'll put a link in the show notes for your Twitter. No, we are done. Are we, Ryan? No, we need to. One thing is missing. Yeah, yeah, yeah. It's important. So this one is more of a, not an ML one, is more of a web API type thing. So, so often people will write web APIs and just return some kind of message in a JavaScript dictionary that says things like bad response or whatever.
But you're supposed to use HTTP status codes, right? Like if there's a bad request, you should return the status code 400. If it's not found as an entity, you should return 404 or whatever. So here's like two kids at school exchanging messages and it has server on one of them, client on the other, and 200 on the message exchange here.
Bye. Thanks for listening to Python Bytes. Follow the show on Twitter via at Python Bytes. That's Python Bytes as in B-Y-T-E-S. Get the full show notes over at Pythonbytes.fm. If you have a news item we should cover, just visit Pythonbytes.fm and click submit in the nav bar. We're always on the lookout for sharing something cool.
If you want to join us for the live recording, just visit the website and click live stream to get notified of when our next episode goes live. That's usually happening at noon Pacific on Wednesdays over at YouTube. On behalf of myself and Brian Okken, this is Michael Kennedy. Thank you for listening and sharing this podcast with your friends and colleagues.
