- Podcasts
- Python Bytes
- #391 A weak episode
About this episodeTopics covered in this episode: Vendorize packages from PyPI A Guide to Python's Weak References Using weakref Module Making Time Speak…
- Vendorize packages from PyPI
- A Guide to Python's Weak References Using weakref Module
- Making Time Speak
- How Should You Test Your Machine Learning Project? A Beginner’s Guide
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#391 A weak episode
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 391, recorded July 9, 2024. And I am Brian Ock. And I am Michael Kennedy. This episode is sponsored by Code Comments, an original podcast from Red Hat. Listen to their segment later in the show.
Or if you just want to see the show later, it's all there. It's usually 10 a.m. Pacific time on Tuesdays, but sometimes it changes. But if you go to that live thing, it'll tell you when the next one is. And finally, I'd really like to thank a lot of the people that have subscribed to the mailing list, the newsletter.
However, there are issues that you run into if you use third party packages. Not the least of which is you now probably should have a virtual environment. You definitely require some pip install commands. Just stuff all along those lines, right? Just the management of setup before you can even run your app.
Plus then any potential changes. If people don't pin their versions, then you're at the whims of what potentially can happen there. So what some people do is something called vendoring or vendorizing dependencies. So for example, if I depend on some functionality from requests, I don't know if that's a super good example, but let's say it is.
I could just download the source code of requests, stick it into my code and use it. It's not probably not a great example because it itself has a bunch of dependencies, but stuff that's kind of like pure Python, no other dependencies. You could have it as a third party package, or you could just stick the code of that somewhere to your app and refer to it as a relative import within your app.
Right. Yeah. Then people just pip install your package, pip x install your package, or even just get a script and run it or a set of scripts. So there's this project from M Williamson called Python vendorize that I want to talk about. So it'll vendorize packages from PyPI, as I just described, right?
So it allows you pure Python dependencies to be vendorized. That is, it'll copy that into your project best use for small pure Python dependencies to avoid version conflicts with other packages, require different versions and so on. So what you do is you set up a vendorize.toml file and in there, you basically set up, what is your module where, what sub section of your module do you want it to go into what sub module and then what.
PyPI packages you want. So example here is like a hello package. So they create a dunder, just not an underscore vendor folder. And then they say the packages are six. And once you run it, it'll create that underscore vendor folder and it'll put the six dist info, the dunder and nit pie, the six up high, all the stuff it needs to basically have that there.
So then in your app, you can say from dot underscore vendor import six rather than having an external dependency. What do you think? kind of neat. How do you keep up? Yeah, I've got questions. Like how do you keep up with, updates and things like that? Well, I believe that you just run a command line.
Yeah. You want to like kind of freeze it in a place and things like six that don't really change. I mean, or have gotten kicked out of the. standard library. Yeah, exactly. Yeah, exactly. A lot of stuff that's really super stable. and pretty small, right? You don't want to, because if, if you do something that's got a bunch of dependencies, you've then got to start doing their dependencies and then it gets really wonky.
Right? Yeah. Yeah. For small things. Yeah, actually. it's pretty cool. yeah. Neat. Neat. Well, that's what I got for you. The first one, I got a bunch of other stuff, as you can see. No, I like it. there's, there's projects that I've, open source that it really wasn't intended for somebody to actually use as a dependency.
some weak, so weak, so some weak references. so this is a, article from Martin Heinz, a guide to Python's weak references using weak ref modules. So weak ref is a, built in standard library module. And, I actually have never played with it. And I kind of knew that Python must have weak references, but I just didn't really explore it before now.
And this is a great introduction to just talk about what they are. So if, I mean, we, the term weak reference might be new to somebody that's, that's like, I don't know, maybe new to you. it's a, it's a term that we talk about in, in like C++ and other things a lot, because of the, using, and I use it a lot in C++, but, using strong references and weak references.
Python also has strong references and weak references. A strong reference is just sort of a copy of something. but a weak references is a way to point to something else, but not, not muck up the garbage collection. So this is a great article. It talks about, starts out with, talking about, sorry, starts out talking about garbage, dark garbage collection and how, how weak references are used with garbage, how garbage collection and weak references and strong references affect that.
So why do we care? Well, it is used in things like log, the logging module for instance. so you have named, this is a cool example cause you have, you have named, logging modules. oh, where was it? This is an example. Anyway, some named modules that, you, you, you named a logging module or a named logger.
Sorry. it's been a rough weekend. And, and then like, if you want another one of the same name, it might be there. It might not. So it's a caching sort of thing that how logging uses it, but there's also ways to use it as a, like for trees. If you're building a data structure, where you might want, bi-directional, links between objects.
one of the objects, it shouldn't be really hard links in both directions. So one of those links should be, a weak reference, like the link between a parent and a child and a tree, structure would be good like that. Or other things like he talks about, he talked about using an observer, building an observer pattern from the design pattern book, using weak references, just some really cool stuff.
I don't know. I don't build a lot of data structures. Like there's enough data structures in Python already. But if, especially if you're in a, in a CS class or you have, some special needs for data structure, weak references are built in and they might help you a lot. So. Yeah. And they're pretty interesting.
but if you have a, a pointer to it, then obviously it's never going away. So weak references allow you to ask questions like that. Like, I think you can do interesting stuff with caching too. For example, like if you've got a cache and you've handed out an instance of the object and it's still alive and people are still using it, the parts of the app are still using it.
So it's like sort of a self-managing cache type of structure could be fun to make too. Yeah. But that said, I was thinking just like you, I don't usually make data structures these days. Python's pretty much got something for you. Right. But, you know, people are building, well, there's some third-party library data structures I use too.
Do you know what else is cool? Code comments from Red Hat. Yeah. This episode is brought to you by Code Comments, an original podcast from Red Hat. You know, when you're working on a project and you leave behind a small comment in the code, maybe you're hoping to help others learn what isn't clear at first.
Sometimes that code comment tells a story of a challenging journey to the current state of the project. Code Comments, the podcast, features technologists who've been through tough tech transitions and they share how their teams survived that journey. The host, Jamie Parker, is a Red Hatter and an experienced engineer.
In each episode, Jamie recounts the stories of technologists from across the industry who've been on a journey implementing new technologies. I recently listened to an episode about DevOps from the folks at Worldwide Technology. The hardest challenge turned out to be getting buy-in on the new tech stack rather than using that tech stack directly.
Thank you to Code Comments for supporting the show. This one is Make Time Speak from Preycin. Preycin's been on the show before, a friend of the show and also a former co-guest, co-host. And the idea is it's a little bit of a human-friendly way to refer to time. You might know about things like, I think it's Arrow, that has a humanized thing that says, you know, five minutes from now or in ten minutes or just now.
Nice. Yeah. I am going to cover a topic that I get asked all the time. So I talk about testing a lot and machine learning and AI is kind of a big thing now. So I get questions like, how do I test machine learning projects? And I got an answer. I have no idea. So I'm excited that somebody made an attempt at this.
It's essentials of testing with a machine learning pipeline focusing on fine tuning BERT for text classification on an IMDB data set. So that's just what he's using. He's using pytest and pytest Cove, which are awesome things to start with. And so it kind of goes through some of the easy stuff right away is starting with some of the simple things like has a clean text function.
So a function within within the source that takes a string and makes it all lowercase and strips it. But it might do other things, too. But these are these are great examples. You've in a lot of machine learning stuff, you've got a lot of little helper functions along the way. May as well go test those and it'll get in the habit of writing tests, too.
And what how how it should handle the empty string. And this is actually a good point. One of the things they test with interviews a lot is the edge cases for testing. So like with what test examples are like derivative small cases that you wouldn't possibly think about. And it's important to test those, too.
So he's got a tokenized text, tokenized text function that uses a lot of sub pieces, uses the tokenizer with certain input. And how you test that? Well, this is a great example of just figuring out really some examples, some example input and what how you would expect it to be tokenized on the output.
Looking at the length and the shape of the result. And then, you know, making sure that that not all values or I don't know what this would be. Oh, he's making sure that all values are torched by torched tensors. I don't even really know what that means. But, you know, thinking about what the output should be.
If even if you don't know the specifics, you can have some way to describe how it should sort of look. And these are good enough tests or they possibly are good tests to have anyway. So I think this is a good, good starting point to to start a discussion on your team for how to how to add testing to a machine learning project.
So after you kind of have a sense of some of the easy stuff, some of the middle level stuff of testing examples and shapes and whatnot. What's left? Well, that's where a quick introduction to how code coverage works and looking at what other what what the rest of your code is doing and that maybe you want to add tests to.
You know, Authy, the 2FA password thing that you can get for multi-factor authentication. Super nice because, you know, so many of the devices are locked to or some of the apps are locked to one platform like Google, a Google authenticator. You lose your phone or you have to reformat it or something.
Sorry. Sorry. Good luck. Now. You know, there's no sinking things like that. But with Authy, you have an account. It syncs it across your different devices. One device can authenticate another. If you want to add a new one. It's really nice except now Authy is urging users to stay alert after 33 million phone numbers were leaked.
Well, and Authy recently canceled their desktop apps. You know, Authy being a Twilio, the parent company, canceled their desktop apps. It just seems like it's really in a kind of a state of disrepair and lack of love and a lack of confidence in Michael at this point. So I went through the super fun experience of resetting about 30 different 2FA logins.
Like, no, it is not. It's like, use your internet explorer six here. Like, no, there are other browsers. Please don't just say use your Google authenticator. Right. But you could just go, yep, this is my Google authenticator. It's called something else and it doesn't come from Google, but sure enough, I'm going to set this up.
Right. And like Christopher out in the, in the audience here, that is my next recommendation is well, if not Authy, what? Because Google authenticators garbage. Like I said, you, if you, your phone gets messed up, you've lost all logins forever. There's not a sink. At least last time I use it, there's no way to sink it or export it or any of that stuff.
you just scan, scan stuff or enter the code and that they give you for the 2FA and off it goes. And because it has a browser plugin, you can just click on your name when it says type in your 2FA code. You don't have to go pull it up. You just click the button and boom, it auto fills it, which is great.
Well, I'm using, maybe I shouldn't tell people, but yeah, I'm using Authy. So are they, they're still supporting it on like, aren't they? they're not supporting it on, they used to have a desktop app. They don't have that anymore. They have a iPad and an iOS and Android app. Since you have an Apple Silicon one, you can run the iPad version on your Mac, just like a desktop app.
So it's kind of feels the same, except for it doesn't have like, like the keyboards behave weirdly and stuff. Right. Cause it doesn't expect you to have a keyboard. Maybe you're using it a lot more than I am, but it doesn't bother me to run, run it on my phone. But yeah. Well, I mean, it doesn't bother me either, but I've got, there's like a bunch of different apps that I have.
For example, the credit card front end system for talk Python courses. It has a remember me button. It never remembers me. Never. It has a 2FA. It never remembers the 2FA. So even if I say, remember me 20 minutes later, I'm putting in the password and the 2FA. And then 20 minutes later, I'm putting the password in the job.
I'm like, ah, so there's like a few places like that. They're just constantly asked for the 2FA. digital issues a little bit like that. Like if, you, every single time you're putting in the 2FA, there's not like a trust this device sort of thing. And so I ended up, I'm probably in used to be in offing now in bit warden.
I'm probably not like five times a day, at least every day. So anyway, so one final thing before I move off of this, after all of this, they said, I don't know how this helps. It doesn't seem like it should help, but somehow they said, as part of our recommendation to users, it's very important that you upgrade to the latest version of Authy.
Why that because the endpoint hands, I don't understand, but, anyway, it says you must. And then if you go and look at the upgrade, all that says for the, it says you must get version 26.1.0. What does it say here? Bug fixes, not, this is an important security update and you need to update because we're trying to protect your privacy.
They're hiding behind bug fixes and it's disgraceful, right? This is bad. So all these things taken together, I'm like, you know what? It may be safe. It may be not, but I'm out. Like this is not where my, my important things live. So. Okay. Yeah. And OFAC also out there says, Hey, if you're okay with recommending paid services, which I am one password is what I migrated away to from Authy.
Yeah. One password is awesome. But like I said, I have my logins at one password. So I put my two FA and bit word. All right. Woo. That was a long extra. That should just been a thing, right? Extra. Remember a while ago, I did this article unsolicited advice for Mozilla and Firefox, like your AI, your, your good citizen AI project.
Well, they didn't do it, but, proton, the Proton Mail people just came up with a, like a Google docs equivalent, but with end to end encryption. And no AI training and no AI training and, you know, all the kinds of things you would like about your data without the negatives. So if you have proton, there's, there's now collaborative docs with it, which is kind of cool.
And it looks pretty, I think it looks pretty nice. So just want to give that a shout out. Does it, do you know if, I probably don't want to ask, does our Google docs open to scanning for AI? Do you know, I believe that the freeware versions are the free versions, but the business ones, maybe not, you know, I think your business workspace stuff is not open to that, but like your, your personal Gmail is open to like scanning for ads and stuff.
Whereas the business one is that's, so that's the price you pay. Last thing, is the code in a castle thing I'm doing in October 5th. The early bird discount closes tomorrow. If you listen to this, right. When it comes out, so if you're interested, please check it out. it'd be super awesome to spend a week hanging out in Tuscany, doing all sorts of things together.
Sit down. So she invited someone over to like watch Netflix. Like I lied. I don't know Netflix. Take off your shoes. We're going to learn rust. I thought it would catch the zeitgeist. Well, right. It's bizarre, man. It's amazing. Okay. And I'll, I'll, I'll maybe I'll put that is the chapter art for this one.
