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


Transcript #328: We are going to need some context here

Return to episode page view on github
Recorded on Tuesday, Mar 21, 2023.

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

00:04 This is episode 328 recorded March 21st, 2023.

00:09 I'm Michael Kennedy.

00:10 - And I am Brian Okken.

00:12 - And this episode is brought to you by Microsoft for Startups Founders Hub.

00:15 We have a really fun one for you again this time, something different, so be sure to check that out.

00:20 Connect with us over on Mastodon.

00:22 All of the links are in the show notes.

00:24 And if you are not hearing our voice at this moment live because you're part of the live stream, Be sure to visit pythonbytes.fm/live, sign up to get notified about the live streams and be part of it every now and then.

00:35 It's a lot of fun.

00:36 Speaking of fun, Brian, share with us this first thing you've discovered.

00:40 - I actually, I wanted to talk about ZipApp.

00:44 Did you know about ZipApp?

00:46 - I have never seen ZipApp in action, but I have heard of ZipApp and it is, - Okay. - it's a very interesting idea.

00:53 New in 3.5, so it's not that old, is it?

00:55 3.5 still sounds new to me.

00:56 I don't care what people say about dates.

00:58 >> Really?

00:59 >> It sounds new to me.

01:01 It has async and await, and that's the first point where I feel like it's, as far back as you can go and still be modern Python.

01:10 >> Okay.

01:10 >> I think you don't get f-strings, but come on.

01:12 It's still, it's not that old.

01:13 It's not something that's been around.

01:15 >> You're more lenient than I am.

01:17 I'm like a 3.7 is the start of the world.

01:20 But anyway, okay.

01:21 So ZipApp, it's been around for a while.

01:23 So all supported Pythons use it.

01:28 But apparently there was like a flub up in the announcement when it came out, so people didn't quite see it when it came out.

01:37 Anyway, so what it is, it's in the standard library and it's a module that provides tools to managing the creation of zip files containing Python code.

01:47 They can be directly interpreted by the Python interpreter.

01:52 It's a command line interface and an API, But the deal is you kind of take like a package or Python package you got or a file or whatever, and it creates an executable for you.

02:04 It's well, not really an executable 'cause it doesn't have the Python part.

02:08 So you still have to run it with Python, but it includes all the stuff except for Python.

02:12 - It's kind of like if you could hand out just a Python script and say run this, but it has dependencies and it has a lot of structure, but you don't have to make people deal with that, right?

02:22 You don't have to have them create virtual environments and install stuff because it can all just take--

02:26 >> I think?

02:26 >> Take the stuff it needs.

02:28 >> I haven't played with it enough to know if it has, oh yeah, has requirements too.

02:32 Install all of your applications dependencies in the app directory using pip.

02:36 So yeah, there's a thing, there's a tutorial inside the documentation saying how to create a standalone application with ZipApp.

02:46 So yes, it includes your dependencies and then you just create this little thing.

02:50 So you can give it to somebody and say, run this on Python 5 or whatever you support.

02:57 That's pretty cool. It's got all that all bundled in.

03:00 I haven't tried this yet, but I didn't even know this was here until recently.

03:05 Brett Cannon mentioned PYZ files, and I'm like, "What the heck is a PYZ file?" I looked it up, it's these Python's zip app things.

03:16 >> Interesting.

03:17 >> Pretty neat. There's some examples in there.

03:21 >> I guess if anybody was trying to think of some way, because we have talked about lightweight ways to ship stuff, to Python stuff to people.

03:32 I think it's fine to tell people, assume that most, depending on your audience.

03:37 But there's a lot of people that it's fine to say, it's running Python, so you're going to have to install Python.

03:43 It's easy to send a file, but sending a bunch of stuff is a little bit harder.

03:48 I mean, you could test it.

03:50 If they're developers, you can say, here's the repo.

03:53 Just download it, install requirements, and there you go.

03:56 But for a lot of people, that's a little too much.

03:58 So this is kind of neat.

04:00 The other use case that I think is a really cool one that I've seen is using it within CI/CD pipeline.

04:07 So if you have a stage that you want to--

04:10 all you're doing is pulling stuff in and running it anyway.

04:13 Might as well just have it all zipped up already.

04:15 So that's a cool way to do it.

04:16 >>Yeah, it's super cool.

04:18 Just a quick one.

04:19 Yeah.

04:19 Henry on the audience says Python can run zip apps files directly.

04:22 PYZ files has been supported since well before three, five, three, five just makes it easier to do.

04:27 Oh, really?

04:28 Okay.

04:29 Yeah.

04:29 Apparently.

04:30 Yeah.

04:31 I thought that that was true, but yeah, the zip app is really cool.

04:33 Neat.

04:34 Indeed.

04:35 Cool.

04:35 So, well, what do you have for us?

04:37 I want to tell you all about, maybe a use case or a story of like how you can just take Python and solve your own interesting little problems.

04:47 So this comes unknowingly through Rhett Turnbull, who shared a blog post in sort of an interesting circular way over on Mastodon and says, "Great blog post from EQ on reverse engineering the Apple News app with Python and NerdPower." That sounds fun, right?

05:05 So this is an article that you can check out and basically it says, look, one way that's awesome to do a bunch of research and gather up a bunch of news is Apple And there's a way in Apple news to say, read later, but because it's Apple news, it's like tries as much as possible to keep you within its little walled garden of Apple news.

05:24 For example, if you share it with somebody, it doesn't share the original link, it shares the Apple news link.

05:29 And you know, if you want to access those links outside of the Apple news app, well, nope, you don't get that.

05:34 Right.

05:34 And so, this EQ person who is, the nickname or online name of Dave says, let's see, I reached out to Rhett Turnbull, here's the circular bit, about his OXX, or macOS photos, Python library that he had been using to extract data from photos, which have a similar issue, and says, you seem to be familiar with like, ripping stuff out of the internals of these macOS apps, what about this one?

06:03 And got a few hints, and turned out that, was able to pull in some information to actually get the Apple Newsroom link out of a list of saved Relator things hidden somewhere, and then goes through using Python to actually, Python plus Beautiful Soup, to pull that out into something that is meaningful and you can actually share with folks, which is pretty cool.

06:25 And then it said, well, I shared this back with Rhett, and Rhett's like, oh, that's awesome.

06:28 How about I create a full CLI library that lets people do that and stash it into a SQLite one, and then who knows, maybe you connect that to Dataset.

06:38 I'm in Willison and you know, like it just, it can just go from there.

06:41 So there's Apple News to SQLite as a CLI that came out of this whole journey.

06:45 But I just thought it was a really cool use case of like, look, you don't need to be trying to create the next major API service or the major web app.

06:54 Here's just a fun way to like, I've got this problem, I've got the structured data and maybe Python can solve my problem here.

07:00 - Yeah, usually can.

07:01 - Yeah, anyway, a lot of fun.

07:03 People can check this out.

07:04 And I think if you're looking for ways to get better with Python or practice with Python or do interesting little projects.

07:10 Maybe this will inspire you because it's not a ton of work for this, right?

07:14 - Yeah, we need nerd power t-shirts.

07:16 Nerd power, I think.

07:18 - Oh, yes.

07:19 I got some t-shirt stuff to share with you at the end as part of our joke.

07:25 So I don't know if there's a nerd power one out there.

07:27 Just real quickly on the audience, Fabio asks, a while ago I talked about a testing, a paid search engine that was Kaggy that I've been using.

07:35 Still using Kaggy, been enjoying Kaggy a lot.

07:38 So that's the one.

07:40 Brian, we ready to tell folks about our sponsor before we move on?

07:43 - I think that's a good idea.

07:45 - I do as well.

07:46 And as promised, I got something fun for you this time.

07:49 So remember, this one is brought to you by Microsoft for Startups Founders Hub.

07:52 One of the really important things you get is access to the open access and credits to the OpenAI APIs and the Azure OpenAI service.

08:00 So I said, hey, let's take the boring, boring, the straightforward ad that we had written originally, and I asked OpenAI to rewrite it.

08:09 So I said, "Hey, OpenAI, can you rewrite the Founders Hub ad as an American football coach trying to motivate his team?" So, what do we get back?

08:17 Attention all startup founders, this is a call to action.

08:19 Are you ready to reach the next level?

08:21 Then listen up.

08:22 Microsoft for Startups Founders Hub is offering a rare opportunity, a chance to get in the game with OpenAI's APIs and Azure OpenAI Service.

08:29 These tools that'll take you to new heights with the power of generative AI at your fingertips.

08:34 but it's not just the technology, it's the support system.

08:37 You get 150K in Azure credits and the richest deal from any cloud provider.

08:42 And tech support, man, it's top notch.

08:44 The team at Microsoft will give you guidance.

08:46 The guidance you need to make the big plays with one-on-one technical advisory.

08:50 And you'll get a network of mentors, seasoned players in the startup world who will be out there to help you navigate the field and make the tough calls.

08:57 You'll get expert help on product, fundraising, go-to-market, and more.

09:00 This is your chance to start a founder to get in the game and make a big impact.

09:03 There's no funding requirement, whether you're in idea phase or further along.

09:07 All you do is sign up and get ready to unleash the power of AI in your startup.

09:11 So what are you waiting for?

09:12 Head over to Python by SetupM/FoundersHub2022 and join Microsoft for Startups Founders Hub today.

09:18 The time is now, the game is on.

09:19 You've got the tools you need to succeed.

09:21 Yeah.

09:21 Thank you, Microsoft for Startups for sponsoring the show and OpenAI for bringing home the championship.

09:27 There we go.

09:28 Nice.

09:29 That's fun, right?

09:30 That's good.

09:31 Yeah.

09:31 Love it.

09:32 Okay.

09:33 I also love context managers.

09:34 How about you?

09:35 >> I do, but just an interesting suggestion from the audience of, would it be different if Ted Lasso wrote it?

09:42 >> Oh, I bet it would.

09:43 Well, you know, there's always next week.

09:45 >> [LAUGH] Yeah, so let's talk about context managers.

09:51 So I love context managers, and I've used context lib, context manager decorator to create context managers.

10:01 and if you've never tried it, it's like the easiest way to make a context manager.

10:07 With context manager, you decorate a function with the decorator context manager, and then you put a yield in the middle of it or anywhere in it.

10:16 It can even loop. Wait, maybe it can't loop.

10:19 No, that's something else. That's a generator.

10:22 No, context manager, you put a yield in it, but it can be in the middle of an exception block or try except or something else.

10:30 And then, so it runs the first part, and then after the with block, it tears it down with the teardown.

10:36 It's nice. I knew about this. But then I was reading an article from Trey Hunter.

10:41 What is a context manager?

10:44 And it's actually a really kind of nice article that talks about all the stuff.

10:48 Basically, if you didn't have it, so first off, it tells you what a context manager is.

10:53 And one of the ones that people are most familiar with, maybe, is open file as F.

10:58 So with open file as F or any with block, you're using a context manager.

11:04 Opening file is great because then it closes afterwards.

11:08 Then he talks through some things.

11:13 The next example he brings up is context lib changed dir.

11:17 I don't know how I didn't know this was here, but what it does is if you use with changed dir, it changes your directory, but then when you're done with the with block, It goes back.

11:28 Cool. Somehow I missed that.

11:30 I was going in and looking, and there's a whole bunch of other stuff.

11:34 I did know about closing.

11:37 Like the file open thing, anything that has a close statement in it, you can use the context manager closing to use for line and page.

11:50 With closing URL open as an example, because then when you're done for urlib, URL lib, it closes it.

11:59 Anything that you have to do a close command afterwards, you can use closing.

12:03 That's cool. But there's async closing also.

12:07 Cool. There's a bunch.

12:09 There's suppress. If for a part of your code you want to suppress a particular possible exception, you can suppress it for a little bit of your code.

12:17 Super handy. Redirect standard out, but just for a little while, you can do that.

12:24 >> Then in re-extended error, and then there's our changed error, but there's a bunch of stuff in here.

12:30 So I guess I encourage people to go look at the context lib and look at some of the stuff you can do, because there's a lot of great stuff there.

12:39 >> Neat. That is really neat.

12:40 I'm a huge fan of these types of things.

12:43 Hold on, don't scroll anymore.

12:44 Do you see when changed error came into existence?

12:47 3.11.

12:48 >> 3.11.

12:49 >> That might be why we don't know about it, because that's brand new.

12:52 >> Yeah. Nice.

12:53 But I love these types of things where you say for this block of code, I want something to be true.

12:59 And then afterwards, I don't want to think about how it happens.

13:01 Like however I get out of here, early return, exception, done, whatever.

13:05 Just undo the thing.

13:06 really, really nice.

13:08 I use some of these, I mean, not, I don't know of a built-in one for this, but that with Colorama to say during this section, I want all the output to be this color and then no matter how I leave, roll it back to the default for like text output and things like that.

13:22 Right.

13:22 Oh, neat.

13:23 Otherwise you got to keep resetting like, "Oh, I'm returning here, so I got to reset here and here." And it's just like, "Nope, in this block, it's blue.

13:29 Now it's not blue." Those kinds of things, I just love it.

13:33 So having these built in, I didn't realize there are so many built in.

13:35 This is great.

13:36 - That's a cool use case.

13:38 I like that for a content expansion.

13:39 - Yeah, thanks.

13:40 - I hadn't thought about that.

13:41 - Indeed, indeed.

13:42 So this next item here that I got for us, Brian, this is a follow-up to a question.

13:49 When you talked about NOX, somebody asked, Can I use that with poetry?

13:53 And we didn't know for sure, but you can bet the audience knows.

13:57 - Yeah.

13:58 - Yeah, so yes, in fact, there is a Knox-poetry library that you can use.

14:05 And it's super simple.

14:07 So the way Knox works, quick reminder for folks who maybe didn't listen or didn't listen that closely or just forgot from last week is, you create a session for some operation and you say, this session's gonna run Python 3.10 or 3.9 together, right?

14:21 And then you just say session install what you need to.

14:24 For example here, the example on this page says install pytest and install the local package that you're working with plus its dependencies and then run pytest.

14:33 The problem was, there's a couple issues.

14:36 If you don't have something that understands poetry, then you end up, the package would just the wheel metadata and not the lock file gets installed, which means you don't get the pinned versions installed that you're hoping for.

14:50 and the dependency constraints that you're hoping for, you just get whatever the latest of those are.

14:56 pytest wouldn't be constrained.

14:58 Poetry would be installed as a build backend every time.

15:02 There's just a bunch of stuff that's a little bit clumsier.

15:04 So what you get here is you basically get a library that you say, sort of from Nox import session, you say from Nox poetry import session, and this is a wrapper proxy to the underlying Nox session that configures things to make sure that poetry works.

15:19 So for example, the install will use the lock file that Poetry uses and other types of things.

15:24 There's a bunch of stuff.

15:25 It'll build the wheel from the local package.

15:27 It'll install the wheel as a pytest package and then invoke pytest to run the test against that installation, which is pretty cool.

15:35 So it also has some other types of things that you can put on here.

15:40 Where did they go?

15:41 There's a bunch of Poetry-specific things.

15:43 You can say session.poetry.

15:46 There it is.

15:46 session.poetry.installroot or session.poetry.buildpackage as a wheel or a disk, an export requirements, all these different things if you want to leverage the fact that it is actually Poetry, or you can just use this wrapper and write exactly the same code, but it now works correctly with your project.

16:03 - Oh, nice.

16:04 - Yeah. - I like it.

16:05 - This is from, came to us from, not one, but two people said, "Hey, you guys, John Hagan and Mark Pruitt." So thank you both for sending in your particular use cases for that.

16:15 And it also comes to us from Claudio Jolowitz, who created the hyper modern Python project, Cookie Cutter.

16:26 And this is part of that to support that project.

16:28 I actually had him on Puck Python a little while ago, back in about a year ago.

16:33 So if you wanna hear way more about this, we probably even talked about this in passing and I was like, "Oh, okay, interesting." Like didn't really, didn't you?

16:40 So there it is.

16:42 We hear so many things, like sometimes, It doesn't always stick the first time, even though we try.

16:46 Yeah, that happens.

16:48 It does happen.

16:49 All right.

16:49 And it also happens that we are at the end of our main topics, but you got any extras you want to share?

16:54 I do.

16:55 And it kind of talks about, it's a little bit similar to what we're just talking about.

16:59 So I was just at PI Cascades this weekend.

17:01 this last weekend, just got back.

17:03 I took the train back yesterday.

17:05 So lovely ride.

17:06 Did you enjoy it?

17:07 I, well, I did on the way back on the way there.

17:10 I kind of thought I would be able to sleep, but, it didn't.

17:14 I wasn't able to.

17:15 So on the way back, it's a nine hour drive or nine hour train ride from eight or nine from here to Vancouver.

17:22 On the way back, I'm like, I'll just work.

17:24 So I just edited a bunch of audio.

17:28 So I was working the whole way back.

17:29 It was great.

17:31 But one of the things that happened to me, there was a couple of people saying, oh, yeah, we met.

17:37 I was at, you know, I was at PyCon, Portland or Ohio or something like that.

17:42 We met.

17:43 I'm like, that's wonderful.

17:44 I meet so many people and I have such a bad memory.

17:46 I mean, I trust and I don't, I feel bad about not remembering everybody I ran into.

17:50 Anyway, sorry, if I said hi and introduced myself and you already, we already have done that, my apologies.

17:57 - I'm bad at this too, it's not ideal.

18:00 But it's also not personal.

18:01 People sometimes take it personally.

18:02 It's like, well, it's really hard when you meet 400 people.

18:05 - Yeah, and then also the questions of, you talked about this one thing, it was kind of like that.

18:10 What was that again?

18:11 I'm like, oh, sorry.

18:13 That's why I have Google or search or whatever.

18:16 So I did give a talk and it's great.

18:19 The slides are up at pythontest.com/pykaskades2023.

18:24 And they are gonna chop up the videos and I think that's what they've told me that they're gonna chop up the videos.

18:31 But for now, the day one and day twos are available.

18:34 If you really wanna watch mine, it's about sharing pytest fixtures.

18:40 It's at what, two hours, 40 minutes, 58 seconds, approximately on day two.

18:47 But that exact link is in our show notes as well if you wanna click through and watch 25 minutes of me talking about packaging.

18:55 So it was really fun, I had a good time.

18:58 Do you have any extras?

19:00 - Today is Talk Python's birthday.

19:02 I believe, gotta do math, I think it's eight years old, which is nuts, it's either seven or eight, that's nuts.

19:09 But the reason I thought of that is, the latest episode right now is Python tips and tricks for better testing.

19:14 That also brings into some of the stuff that you talked about in your talk.

19:17 So if people are interested, they should check out this episode and they can see a cool hat if they do.

19:22 - Nice.

19:24 Cool.

19:25 - All right.

19:26 That's not what I was planning on sharing, but yeah, it ties right in there.

19:29 Now, what I wanted to share was one thing.

19:32 So let me tell you about my router challenges here.

19:36 So at my, as you know, Brian, but probably almost no one else does.

19:40 I have my office above my garage.

19:42 My garage is a separate building from my house and there's no ethernet connecting as much as I've tried to make that happen.

19:48 I haven't been able to make it happen.

19:49 So I'm like, well, how do I make this faster?

19:52 I've been using a two node mesh network and it turns out that people getting stuff around us have been causing a lot of interference.

20:00 And so my wife needs one closer to her office and I need one closer to mine and one's got to be plugged into the internet.

20:06 and we were like, kind of like disagreeing.

20:08 Like I want it closer to my office.

20:10 No, closer to mine.

20:10 Mine's a separate building, I need more, right?

20:13 And back and forth.

20:14 And so I ended up solving this with too much money, getting this thing called the Atlas Max 6E, link this router.

20:22 I don't know if I recommend it, 'cause it's really expensive, but it's one of the few things that both has three nodes and it's 6E Wi-Fi, not just Wi-Fi 6, right?

20:32 Good combo.

20:33 So this is not really my recommendation, what I want to tell you about.

20:35 My recommendation is, I've always considered these mesh network things to be like, well, if I'm on Wi-Fi and that talks to one of the nodes and then the node talks to the main node at the house, that's just as good.

20:48 Why would I ever say, log in ethernet to the secondary node, right?

20:51 Still Wi-Fi, it's just one more hop, right?

20:53 Well, I was getting a little interference in the house.

20:55 I said, well, let me just try plugging in, not to the thing that connects to the internet, but to the node that wirelessly across the buildings connects to the other thing that connects to the internet.

21:04 Do you know what I learned?

21:05 I learned something insane that, I don't know if I can simulate it here, across buildings, I'm apparently not getting it right now.

21:15 I'm only getting a rough 570 megabit over wireless across buildings.

21:20 Earlier I was getting 800 megabit over wifi.

21:23 - Well, I mean, you're also streaming video at the same time, so.

21:26 - Yeah, we're streaming video and doing other stuff, right, uploading, downloading.

21:29 But, so my advice to people, if you have a mesh network, Even though you're connected to one of these nodes, you think, well, that's wireless to the end anyway, like, is it really worth ethernet?

21:40 Yes.

21:40 it's because.net I'll try one more.

21:44 I was completely blown away by how much of a difference it makes, but yeah, it's.

21:49 It's really, really impressive to plug into these wires, wireless things.

21:52 So if you've got a mesh network, try ethernet to it, even if it doesn't matter.

21:56 That's my, my extra.

21:57 Cool.

21:58 Yeah.

21:58 So I had to look it up.

22:00 I didn't, I didn't know what wifi six E was.

22:02 So I looked it up, it's 11 AX.

22:06 That makes sense. So I test Wi-Fi stuff, but I'm testing it on the standard side.

22:13 So we get it before they decide what the buzzwords are going to be.

22:18 >> Interesting. Yeah, I see you.

22:19 Okay. That's really cool.

22:21 >> AX is really cool.

22:23 It's the first standard that is, now I'm getting nerdy out on Wi-Fi, but it's the first standard that's not bi-directional the same.

22:30 So the protocol from up from the node to your device is different than the other way.

22:38 Kind of like cellular is like that too.

22:41 But before AX, it was identical.

22:45 The same sort of protocol and traffic and speed and everything from in both directions, but this way.

22:52 Yeah, so anyway, so neat.

22:55 - Yeah, anyway, really, really cool.

22:56 Out in the audience, David Poole says, "Linux makes good stuff.

23:00 A wired is always better than wireless.

23:01 I work on wifi professionally.

23:03 Yeah, for sure.

23:04 Yeah.

23:04 All right.

23:05 Nice.

23:06 Ready?

23:06 Ready for some jokes?

23:07 Yeah.

23:08 So I'm going to take us to an unexpected place.

23:10 Etsy.

23:11 You ready for this?

23:12 Now this is pretty, I, I, I considered not putting this here cause it's fairly graphical, like, like you need to see it in nature.

23:19 So what I'm going to do is I'm going to take our favorite one and put it as a chapter art for people to appreciate.

23:24 Okay.

23:24 Okay.

23:25 So these are like tech signs that have, words or sayings that don't necessarily match with what you might expect.

23:32 So for example, here's one and it looks kind of like a Ram chip that's hot or something, and it has a little heat wave coming up.

23:39 It says caution, magic smoke cannot be reinserted once released.

23:43 Isn't that awesome?

23:46 Yeah.

23:47 Yeah.

23:47 Here's one.

23:48 It, this is a throwback to quantum mechanics.

23:50 I believe it was a little wavy hexagonal lines and a bunch of question marks, this uncertainty.

23:55 Do not attempt to measure.

23:56 this is what I think probably all of us can relate to this.

23:59 I've created some of this myself and had to deal with it.

24:02 It's a hexagon with some piece of technology in the middle.

24:05 It says caution, cursed technology inside.

24:07 Oh yeah.

24:08 It's a Pentagon in there.

24:10 Cool.

24:10 Nice.

24:10 Yeah.

24:11 Yeah.

24:11 Pentagon rather.

24:12 Thank you.

24:12 And, let's see, maybe, maybe one more here.

24:16 if we can find it.

24:18 Oh, this one, this'll be a good one.

24:19 A Gen two and it's got a Gen two logo in the middle.

24:22 It says, "Warning, kernel may need recompiling in order to change sound volume." [laughter]

24:27 >>All right.

24:28 Well, there you have it, folks.

24:29 That's one of my jokes.

24:30 There's a whole bunch of these.

24:34 You can click around and enjoy them for a while.

24:37 They're pretty good.

24:38 >>Nice.

24:39 These are great.

24:40 >>Yeah, they are.

24:41 >>Cool.

24:42 Well, thanks again.

24:43 Wonderful episode.

24:44 >>Sure was.

24:45 Thank you, Brian, for being here, and thank you, everyone, for listening.

24:46 Really appreciate it.

Back to show page