Python Bytes - #328 We are going to need some context here
Episode Date: March 21, 2023Topics covered in this episode: zipapp Reverse engineering the Apple News app with #python and #nerd power What is a context manager? nox-poetry: Use Poetry inside Nox sessions Extras Joke See t...he full show notes for this episode on the website at pythonbytes.fm/328
Transcript
Discussion (0)
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 328, recorded March 21st, 2023.
I'm Michael Kennedy.
And I am Brian Ocken.
And this episode is brought to you by Microsoft for Startups Founders Hub.
We have a really fun one for you again this time.
Something different, so be sure to check that out.
Connect with us over on Mastodon.
All of the links are in the show notes.
And if you are not hearing our voice at this moment live because you're part of the live stream,
be sure to visit pythonbyse.fm slash live.
Sign up to get notified about the live streams and be part of it every now and then.
It's a lot of fun.
Speaking of fun, Brian, share with us this first thing you've discovered.
I actually, I wanted to talk about ZipApp.
Did you know about zip app i have never
seen zip app in action but i have heard of zip app and it is okay it's a very interesting idea
new in three five so it's not that old is it three five still sounds new to me i don't care
what people say about dates really it sounds new to me it has a sync and a wait and like that's
like the first okay uh point where i feel like it's kind of as far back as you can go and still be modern Python.
Maybe you don't get F strings, but come on.
Still, it's not that old.
It's not something that's better.
You're more lenient than I am.
I'm like a 3.7 is the start of the world.
But anyway, OK.
So ZipApp, it's been around for a while.
So all supported Pythons use it.
Right.
So, um, and, uh, uh, but, um, but apparently there was like a, uh, flub up in the announcement
when it came out.
So people didn't quite see it when it came out.
So it's anyway, so that what it is, it's in the standard library and it's a module that,
that provides tools to managing the creation of zip files
containing python code uh and they can be directly uh interpreted by the python interpreter so it's
a command line interface and an api but that the deal is you kind of uh take like a package or
python package you got or a file or whatever and and it creates an executable for you um it's well it's not really an executable
because it doesn't have the python part so you still have to run it with python but it includes
all the stuff except for python so 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 you don't have to have them create virtual environments and install stuff because it can all just i think i mean i haven't i haven't played with it enough
to know if it has oh yeah has requirements to um uh install all of your applications dependencies
in the app directory using pip so um yeah so there's like a there's a thing there's like a
tutorial inside the the documentation saying how to create a standalone
application with zip app so yes it includes your your dependencies and then you just create this
little thing so you can say you can give it to somebody and say run this on python 5 or you know
or whatever you support um and that's pretty cool it's got all that all bundled in i haven't tried
this yet but i didn't even know this was here until recently.
Brett Cannon mentioned PYZ files.
And I'm like, what the heck is a PYZ file?
And I looked it up.
It's these Python zip app things.
So interesting.
Pretty neat.
There's some examples in there.
So, yeah, that's just I guess this just if anybody was trying to think of some way because we have talked about like lightweight ways to ship stuff to Python stuff to people.
And I think it's fine to tell people that assume that most I mean, depending on your audience, but there's a lot of people that it's fine to say it's it's running Python.
So you have to install Python.
But then and it's easy to send a file,
but sending a bunch of stuff is a little bit harder.
I mean, if they're developers, you can say,
here's the repo, just download it, install requirements,
and there you go.
But for a lot of people, that's a little too much.
So this is kind of neat.
The other use case that I think is a really cool one that I've seen is using it within CICD pipeline.
So if you have a stage that you want to, all you're doing is pulling stuff in and running it anyway.
Might as well just have it all zipped up already.
So yeah, it's a cool way to do it.
Yeah, it's super cool.
Just a quick one.
Yeah, Henry on the audience says Python can run zip apps files directly.
PYZ files has been supported since well before 3.5
3.5 just makes it easier to do oh really okay yeah apparently yeah i thought that that was true but
yeah the zipapp is really cool neat indeed cool so well what do you have for us 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.
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 nerd power.
That sounds fun, right?
Mm-hmm.
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 News.
And there's a way in Apple News to say, read later.
But because it's Apple News, it tries as much as possible to keep you within its little walled garden of Apple News.
For example, if you share it with somebody, it doesn't share the original link.
It shares the Apple News link.
And if you want to access those links outside of the Apple News app, well, nope, you don't get that, right?
And so this EQ person, who is the nickname or online name of Dave Bullock, 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 ripping stuff out of the internals
of these macOS apps.
What about this one?
And it got a few hints and turned out that was able to
pull in some information to actually get the apple news 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 and then it said well i shared this shared this back with Rhett and Rhett's like, oh, that's awesome. How
about I create a whole CLI library that lets people do that and stash it into a SQLite one.
And then who knows, maybe you connect that to dataset from Simon Willison and, you know, like
it just, it can just go from there. So there's Apple news to SQLite as a CLI that came out of
this whole journey.
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.
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.
Yeah, usually can.
Yeah.
Anyway, a lot of fun.
People can check this out.
And I think if you're looking for ways
to get better with Python or practice with Python
or do interesting little projects,
maybe this will inspire you
because it's not a ton of work for this, right?
Yeah.
We need Nerd Power t-shirts.
Nerd Power, I think.
Oh, yes.
I got some t-shirt stuff to share with you at the end
as part of our joke.
So I don't know if there's a nerd power one out there.
Just real quickly on the audience, Fabio asks,
a while ago I talked about testing a paid search engine.
That was Kaggy that I've been using.
I'm still using Kaggy.
I've been enjoying Kaggy a lot.
So that's the one.
Brian, are we ready to tell folks about our sponsor before we move on?
I think that's a good idea.
I do as well.
And as promised, I got something fun for you this time.
So remember, this one is brought to you by Microsoft for Startups Founders Hub.
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.
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.
So I said, hey, OpenAI, can you rewrite the Founders Hub ad as an American football coach trying to motivate his team?
So we're going to get back.
Attention, all startup founders.
This is a call to action.
Are you ready to reach the next level?
Then listen up.
Microsoft for Startups Founders Hub is offering a rare opportunity, a chance to get in the game with open AIs,
APIs, and Azure Open AI service.
These tools that'll take you to new heights
with the power of generative AI at your fingertips.
But it's not just the technology,
it's the support system.
You get 150K in Azure credits
and the richest deal from any cloud provider.
And tech support, man, it's top notch.
The team at Microsoft will
give you guidance, the guidance you need to make the big plays with one-on-one technical advisory.
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. You'll get expert help on product
fundraising, go-to-market, and more. This is your chance, startup founders, to get in the game and
make a big impact. There's no funding requirement, whether you're in idea phase or further along. All you do is sign up and
get ready to unleash the power of AI in your startup. So what are you waiting for? Head over
to Python by Setup.fm slash Founders Hub 2022 and join Microsoft for Startups Founders Hub today.
The time is now. The game is on. You've got the tools you need to succeed. Yeah. Thank you,
Microsoft for Startups, for sponsoring the show and OpenAI for bringing home the championship.
There we go.
Nice.
That's fun, right?
That's good.
Yeah.
Love it.
Okay.
I also love context managers.
How about you?
I do, but just an interesting suggestion
from the audience of,
would it be different if Ted Lasso wrote it?
Oh, I bet it would.
Well, you know, there's always next week.
Yeah.
So let's talk about context managers.
So I have used, I love context managers, and I've used Context Lib, Context Manager Decorator to create context managers.
And if you've never tried it, it's the it's it's a it's like the easiest
way to make a context manager with um with context manager you decorate a function and with the
decorator context manager and then you put a yield in the middle of it or anywhere in it it can even
loop um wait maybe it can't loop no that's something else that's a generator um no context
manager you put a yield in it and um and it can be but it can
be in the middle of like an exception block or try accept or or something else and then so it
runs the first part and then after the with block it tears it down with the teardown it's nice i
knew about this but then i was reading uh an article from trey hunter uh what is a context
manager and it's actually a really kind of nice article
that talks about all the stuff.
Basically, if you didn't have it,
so first off, it tells you what a context manager is.
And one of the ones that people are most familiar with,
maybe, is OpenFileAsF.
So with OpenFileAsF or any with block,
you're using a context manager, right?
So opening file is great
because then it closes afterwards uh and then um he he talks through uh some things he one of the
the next example he brings up is context lib changed her and i don't know how i didn't know
this was here but uh what it does is you if you use with changed or it changes your directory.
But then when you're done with the with block, it goes back.
Cool.
Somehow I missed that.
So I was going in and looking and there's a whole bunch of other stuff.
So I did know about there's I did did know about closing.
So like the file open thing, you can anything that has a close statement in it you can use uh um you can use the context manager closing um to do to
use like for line and page wait uh with closing url open as an example because then when when
you're done uh for urlib url lib it uh closes it so anything that has a you have to do a close
command afterwards you can use closing that's cool but there's a there's
async closing also cool um there's a bunch there's suppress so if for a part of your code you want to
suppress a particular uh possible exception you can suppress it for a little bit of your code
super handy um redirect standard out but just for a little while uh you can do that uh just
there's and then uh in redirect stranded, and then there's our changed error.
But there's a bunch of stuff in here.
So I guess I encourage people to go through, 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.
Neat.
That is really neat.
I am a huge fan of these types of things.
And hold on, don't scroll anymore.
Do you see when changed or came into existence?
3.11.
3.11.
That might be why we don't know about it
because that's brand new.
Yeah, nice.
But I love these types of things where you say,
for this block of code, I want something to be true.
And then afterwards, I don't want to think about how it happens.
Like, however I get out of here, early return, exception, done, whatever.
Just undo the thing.
Yeah.
Really, really nice.
I use some of these.
I mean, 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, right?
Oh, neat.
Because otherwise you've got to keep resetting.
Like, oh, I'm returning here, so I've got to reset here and here. And it's just like, nope, in this that, right? Oh, neat. Because otherwise you've got to keep resetting, like, oh, I'm returning here,
so I've got to reset here and here.
And it's just like, nope, in this block, it's blue.
Now it's not blue, right?
Those kinds of things, I just love it.
So having these built in,
I didn't realize there were so many built in.
This is great.
That's a cool use case.
I like that for a conference manager.
Yeah, thanks.
How about that?
Neat.
Indeed, indeed.
So this next item here that I got first, Brian, this is a follow-up
to a question. When you talked about Knox, somebody asked, can I use that with poetry?
And we didn't know for sure, but you can bet the audience knows. Yeah. Yeah. So yes, in fact,
there is a Knox dash poetry library that you can use.
And it's super simple.
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 is going to run Python 3.10 or 3.9 together, right?
And then you just say, session install what you need to. 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. The problem was there's a couple issues. If you
don't have something that understands poetry, then you end up the package with 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,
the dependency constraints that you're hoping for. You just get whatever the latest of those are.
PyTest wouldn't be constrained. Poetry would be installed as a build backend every time.
There's just a bunch of stuff that's a little bit clumsier. So what you get here is you basically get a library that you say,
sort of from Knox import session, you say from Knox poetry import session,
and this is a wrapper proxy to the underlying Knox session
that configures things to make sure that poetry works.
So for example, the install will use the lock file that poetry uses
and other types of things.
There's a bunch of stuff.
It'll build the wheel from the local package.
It'll install the wheel as a PyTest package and then invoke PyTest to run the test against that installation, which is pretty cool.
So it also has some other types of things that you can put on it here.
Where do they go?
There's a bunch of Poetry-specific things.
You can say session.poetry.
There it is like session.poetry.installroot or session.poetry.buildpackage as a wheel or a
disk and 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 oh nice yeah nice. Yeah. I like it. This is from, came to us from not one,
but two people said,
hey, you guys, John Hagen and Mark Pruitt.
So thank you both for sending in
your particular use cases for that.
And it also comes to us from Claudio Jolowitz,
who created the hyper-modern Python project,
Cookie Cutter.
And this is part of that to support that project.
I actually had him on HuckPython a little while ago,
back in about a year ago.
So if you want to hear way more about this,
we probably even talked about this in passing.
And I was like, oh, OK, interesting.
Like, didn't really.
Yeah.
There it is.
We hear so many things.
Like sometimes, you know, it doesn't always stick the first time, though we try yeah that happens it does happen all right and it also happens that
we are at the end of our main topics but you got any extras you want to share i do and it kind of
talks about it's a little bit similar to what we're just talking about so i was just at pike
cascades this weekend um this last weekend just just got back. I took the train back yesterday.
It was a lovely ride. How was the train ride?
Did you enjoy it?
Well, I did on the way back.
On the way there, I kind of thought I would be able to sleep, but I didn't.
I wasn't able to.
So on the way back, it's a nine-hour drive or nine-hour train ride from eight or nine
from here to Vancouver.
On the way back, I'm like, I'll just work.
So I just edited a bunch of audio. So I was working the whole way back is great.
But one of the things that happened to me, there was a couple people saying, Oh, yeah,
we met I was at, you know, I was at PyCon, Portland, or Ohio or something like that. We met,
I'm like, that's wonderful. I meet so many people and I have such a bad memory.
I mean, I trust and I don't,
I feel bad about not remembering everybody I ran into.
Anyway, sorry, if I said hi and introduced myself
and you already, we already have done that, my apologies.
I'm bad at this too.
It's not ideal.
But it's also not personal.
People sometimes take it personally.
It's like, well, it's really hard when you meet 400 people.
Yeah. And then also the questions of you talked about this one thing. It was kind of like that. What was that again?
Like, sorry. That's why I have Google search or whatever. So I did give a talk and it's great. slides are up at python test.com slash pi cascades 2023 and there are they are going to chop up the
videos and i think that's what they've told me that they're going to chop up the videos but for
now the day one and day twos are available um if you really want to watch mine it's about sharing
pi test figure fixtures um it's at what uh hour 22 hours, 40 minutes, 58 seconds, approximately on day two.
But that exact link is in our show notes as well.
If you want to click through and watch 25 minutes of me talking about packaging.
So it was really fun.
I had a good time.
So do you have any extras?
Today is TalkPython's birthday.
I believe.
Got to do math.
I think it's eight years old, which is nuts.
It's either seven or eight.
That's nuts.
But the reason I thought of that is the latest episode right now is Python Tips and Tricks
for Better Testing.
It also brings into some of the stuff that you talked about in your talk.
So if people are interested, they should check out this episode and they can see your cool
hat if they do.
Nice.
Cool.
All right.
That's not what I was planning on sharing, but yeah, it ties right in there.
Now, what I wanted to share was one thing.
So let me tell you about my router challenges here.
So at my, as you know, Brian, but probably almost no one else does, I have my office
above my garage.
My garage is a separate building from my house and there's no ethernet connecting this
as much as I've tried to make that happen.
I haven't been able to make it happen.
So I'm like, well, how do I make this faster?
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.
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. And we were like, kind of like disagreeing. Like I want one closer to her office and I need one closer to mine. And one's got to be plugged into the internet.
And we were like, kind of like disagreeing.
Like I want it closer to my office.
No closer to mine.
Mine's a separate building.
I need more.
Right.
And, and back and forth.
And so I ended up solving this with too much money and getting this thing called the Atlas
Max 6E Linksys router.
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? A good combo.
So this is not really my recommendation, what I want to tell you about. 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.
Like, why would I ever say plug in Ethernet
to the secondary node, right?
Still Wi-Fi, it's just one more hop, right?
Well, I was getting a little interference in the house.
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.
Do you know what I learned?
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.
I'm only getting a rough 570 megabit
over wireless across buildings.
Earlier, I was getting 800 megabit over Wi-Fi.
Well, I mean, you're also streaming video at the same time.
Yeah, we're streaming video and doing other stuff, right? Uploading, downloading. 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? Yes. It's ptas.net. I'll try one more. I was completely blown away by how much of a difference it makes. But yeah, it's really, really impressive to plug into these wireless things. So if you've got a mesh network, try Ethernet to it, even if it doesn't matter. That's my extra.
Cool.
Yeah. I didn't know what Wi-Fi 6E was. So I looked it up. It's 11AX.
That makes sense.
So I test Wi-Fi stuff, but I'm testing it on the standard side.
So we get it before they decide what the buzzwords are going to be.
Yeah, I see.
Okay.
Yeah, that's really cool.
AX is really cool.
It's the first standard that, I mean, that is now i'm getting nerding out on wi-fi but it's the first standard that's not
bi-directional the same so the the the protocol from up from the the node to your device is
different than the other way kind of like cellular is like that too but uh before uh ax um it was identical the you the same same sort of protocol
and traffic and speed and everything from in both directions but this way yeah so anyway so neat
yeah anyway really really cool out in the audience david pool says uh linksys makes good stuff
wired is always better than wireless i work on wi-fi professionally yeah for sure yeah all right
nice ready ready for some jokes yeah so i'm gonna take us to an unexpected place etsy you're ready
for this now this is pretty i i uh i considered not putting this here because it's fairly graphical
like like you need to see it in nature so what i'm gonna do is i'm gonna take our favorite one
and put it as a chapter art for people to appreciate. Okay. Okay. So these are like tech signs that have words or sayings that don't
necessarily match with what you might expect. 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. It says, caution,
magic smoke cannot be reinserted once released.
Isn't that awesome?
Yeah.
Yeah.
Here's one.
This is a throwback to quantum mechanics, I believe.
It was a little wavy hexagonal lines and a bunch of question marks.
It says, uncertainty.
Do not attempt to measure.
This one, I think probably all of us can relate to this.
I've created some of this myself and had to deal with it.
It's a hexagon with some piece of technology in the middle.
It says, caution, cursed technology inside.
Oh, yeah.
It's a pentagon in there.
Cool.
Nice.
Yeah.
Yeah.
Pentagon, rather.
Thank you.
And let's see.
Maybe one more here, if we can find it.
Oh, this one.
This will be a good one.
A Gen 2 one.
It's got a Gen 2 logo in the middle.
It says, warning, kernel may need recompiling in order to change sound volume all right well there you have it folks that's one of my jokes
there's a whole bunch of these you can click around and enjoy them for a while they're pretty
good nice these are great they are cool well thanks again wonderful episode sure was thank
you brian for being here and thank you everyone for listening really appreciate it see y'all