Python Bytes - #362 You can deprecate a global variable?
Episode Date: November 28, 2023Topics covered in this episode: Habits of great software engineers Flask 3.0 Build Conway's Game of Life With Python polars business Extras Joke See the full show notes for this episode on the w...ebsite at pythonbytes.fm/362
Transcript
Discussion (0)
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 362, recorded November 28th, 2023.
I'm Michael Kennedy.
And I'm Brian Akin.
And this episode is brought to you by Scout APM.
We will tell you more about them later, but please check out what they're offering.
It really helps support the show.
And if you want to connect with us on the socials, the place we're all hanging out at is
mastodon, fostodon.org specifically. I'm Adam Kennedy. Brian is at Brian Ocken and the show
is at Python Bytes. Fostodon.org is invite only. Of course, you can follow us from any place on
Mastodon, but if you want to be part of it, you know, just shoot, shoot one of us a message. We
can send you an invite, help you get over there to hang out a little bit closer to home for us.
And finally, if you want to be part of the live stream and see how the show is made,
see what we're doing, even the real mistakes that we might edit out,
check that out at pythonbytes.fm slash live, usually Tuesdays at 11 p.m.
And older videos are there as well.
Let's kick it off.
What do you got?
This is a gentle ease back into the show.
Over the Thanksgiving break, I was thinking a lot about kind of what I wanted to do for the next year.
Starting to, you know, starting to think about plans for how to, where do I want to focus my time?
And I ran across this great post by Vadim.
Let's just say Vadim.
Habits of Great Software Engineers.
And there's a lot of great stuff here.
So also nice index on the right, which is cool.
So focusing beyond the code.
So I'm not going to go through all these.
I'm going to go through, there's like 12 or 13 listed,
but I'll go through a few of them.
Focus on the code. So I don't know if i buy this but he says as a developer you code 20 of your time you should excel at coding nonetheless but it won't be enough to be great i think i code
more than 20 of the time i would say too well define code right okay so is coding is that
reading code is that thinking about code is that oh the design
part and right or is it actually i'm hitting keys on keyboard you know what i mean like that's true
right because people say you code is read way more than it's than it's written and stuff but i think
for me coding is like the act of being in the code and adding features or evolving it even if
like i'm not key stroking it at that moment.
So unclear, but if that encompasses all,
then 20% is way too low, agreed.
Okay.
Well, I guess the point of this,
regardless of the percentage,
is focusing on the other parts of your job also
and focusing on, you know,
not just being a better software engineer,
but also, well, being a better software engineer encompasses more than just coding well and knowing like knowing Python
details and things like that, like communication skills and all that, um, design skills, uh,
learning other, learning to read other people's code, things like helping unblock your teammates
who are having a challenge instead of writing code yourself. Like I'm going to help them get that,
that problem fixed or something. Right. Like all those things well and uh okay so number two was uh efficiency anti and anti-fragility
i'm not really a fan of the anti-fragile word i don't even really know what it means but i guess
uh i think it originally comes from nassim talib and that whole philosophy although i haven't read
a whole lot of that either anyway yeah but it's kind of about
things that um get better the more they kind of get abused right like they sort of build up a
resilience well i think it originally comes out of like economics okay um well the the next one is
one i really uh agree with is the joy number three is the joy of tinkering build projects
try out frameworks build stuff on the side uh keeps the
spark spark alive and i this is one i couldn't agree more it doesn't um i for a while i was
often thinking about like what could i build that i could actually release or that i'd be proud of
that's great um you could try to at least do a side project that tries to make money or something
but if you just want to try out fast apI, for instance, just build something with FastAPI.
Just come up with a toy project and build it and learn it.
And it's fun.
Even if you never apply it to other stuff.
I've had so many times in my career where I've had something that I tinkered with just
as a toy that suddenly I could use.
I'm like, oh, yeah, I've built wikis before and stuff like that i
can go ahead and uh do that for project or you know i uh i've done a flask app on the side so
if i need a flask app at work it's going to be pretty easy things like that think plain plain
with toy projects i say put that on top of your list for 2024 more toy projects um i'll go through
the rest of these pretty quick.
Number four is knowing the why.
Number five, thinking in systems.
Number six, tech detox.
Recharging away for the monitor.
This makes you a better programmer.
Couldn't agree more.
The art of approximation.
I don't really know what this is about.
I didn't read this part.
Knowing important tech numbers.
Anyway. I actually want to cover that. Knowing important tech numbers. Anyway.
I will, I actually have one, I want to cover that as one of our items in the future. There's a couple of hosts or charts or whatever that somebody put up that I think would be really
interesting. So this is like, what is the relative speed of L2 cache versus RAM, RAM versus disk?
What is the cost of like a CPU core
or a database operation on the cloud,
et cetera, et cetera.
But I don't like some of the numbers there.
I think they're way, way skewed.
So I want to come up with my own table
and then put them both forward.
So that's why I haven't covered it yet.
But that's what this is about, I think.
Okay.
Well, what's funny is that like the teaser
at the beginning of this post post says there's only eight.
But there's more if you read it.
But there's a table of contents on the right.
So you just look at the table of contents.
The one that I really the last one I think is the most important for me is strong opinions loosely held.
And for this for me, this means that I'm going to have a default approach to test for a lot of problems and a default way to do things.
It just speeds things up.
It's like, if I don't care about it, just do it quickly.
You've got an opinion how things are done.
However, be open and minded enough to when somebody approaches you and says, hey, there's a better way to do this.
Or keeping your eyes open with the tinkering trying new new ways to do things
be willing to abandon your old strong opinion and go oh there's a better way cool my new opinion is
this now um that pretty much summed up our podcast yeah exactly yes so yeah uh nice nice post thinking
about um you know kind of planning for your time allotment for the upcoming year.
Yeah.
Lots of feedback on this one.
Jeff out there says, tech numbers like LEET 1337.
Excuse me.
Yes, those are the important tech numbers.
Those are the important ones.
Yeah.
LEET, NOOB, all those things.
And then Grant following up on the antifragile says this.
Number two speaks more to reactive
principles that came about with the cloud it could be summed up as build with the expectation
that your k8s pods can be nuked mid-task at any point yeah exactly that oh you might just use one
vm but it could die but if you like know that it's always getting abused you would build like fail
over and those types of things into it, making it hence less fragile.
I think that's kind of that principle there. Yeah. Okay. But also if it's a side project,
if it's down once in a while, it's fine. Yeah. This is not a side project. Yeah.
Right. Maybe a habit of great software engineers. I don't know. I wonder how many blogs run on Kubernetes. I don't know what the answer is, but I bet too
many. The answer is too many. Too many run on run on kubernetes okay okay what do you got for us all right my i'm good i got eight
nines uptime on my blog okay although we probably do brian because ours runs on a cdn as a static
file okay that's pretty good uptime right yeah then the database doesn't need to be migrated
for that business, does it?
All right, moving on.
We've covered this enough.
Let's talk about a new version of Flask.
So Flask obviously is a super important web framework.
Everyone has heard of Flask.
It's kind of one of the two main pillars that I would think of, like the old guard pillars, right?
There's Flask and Django.
And the debate for so many years has been like, do you use Flask or do you use Django?
What kind of web developer are you?
Well, Flask 3.0 was released just a little while ago.
Was that three weeks ago? Something like that.
There's also a 3.0.1 that is currently unreleased, but has like very, very minor changes.
So anyway, the changes for a 2.0 to 3.0 release are not that huge.
Although I think the numbering probably comes from this removed,
previously deprecated code there.
That's probably what pushed it
from a two to three
rather than some mega new feature.
Yeah.
Right.
Because it's like, hey,
it's not backwards compatible.
Miguel Grinberg actually wrote
an interesting but controversial piece on that.
I'm not going to link to it,
but y'all can check that out.
I just want to cover the release here. But this, actually this first line, there's a lot of
discussion about it. Let's say neutral there. So some of the things that are interesting here,
though, Brian are not so much interesting because they're flasks. They're just like,
wait, what? You can do that? So deprecated the dunder version attributes. So frequently people
will go to their dunder init.py for their package and put a dunder version attribute. So frequently people will go to their dunder init.py for their package
and put a dunder version global variable and set it to a string. And then you can say, well,
what version is the thing? Well, it's flask.dunderversion is the version and that's just
a string, right? Yeah. Yeah. So the idea is that's deprecated. And now the way you're supposed to get the version of Flask is using feature
detection or with importlib.medadata.version of quote Flask. Okay. Which, which I had not like,
okay, so cool that that might work, but how in the world do you make it possible for a package
that that comes out of it? If I wrote a package and I wanted to say dot version of my package in import lib,
how do I put a number in there
that makes it the version, right?
So do I have it here?
Yeah, basically you have to go,
if you use a pyproject.toml, at least one way is,
and you just set the metadata category,
set the version to something.
And then that's that.
Okay.
Yeah.
Pretty cool.
And so that's where that
value comes from when you say import lib.metadata.version pretty cool what's more interesting
is deprecated a global variable how do you duplicate a global variable i know how to
deprecate a method a class but a variable but just delete it that's it. You wanted to give a warning when you access the variable.
Okay.
And the variable's value is a string.
Like, okay, what?
So I'm like, I got to see how it is.
So I went and I tracked down the changelog.
I checked around the PR that made this happen.
I'm like, I got to see the code.
What is this?
So it says a deprecated Dunder version.
And here you can see somewhere, you can just see they literally, like you said, deprecated Dunder version. And here you can see somewhere,
you can just see they literally, like you said,
they deleted the Dunder version variable,
but they added a function to the module
called dunder get adder.
For some reason, I thought that only applied to classes,
but no, apparently modules.
And given that it comes in and it asks for the version,
it says, no, There's a warning,
and then it calls the import lib.version thing on itself.
Interesting, huh?
Yeah.
Actually, this is a handy thing to keep around.
Yes, exactly.
If you're asking for Dunder version,
it warns and then it does the new thing.
Otherwise, it just says,
no, we don't have one of them.
But yeah, that's wild, right?
Yeah.
Huh.
I love how dynamic Python is.
Yeah, it's nuts. It was a function or a how it's a function call to a dunder get adder method on
the module wow yeah i think i'm going to replace all of my global like function declarations as
get adder exactly it's gonna write all yeah the whole function is just a bunch of dispatch to get out it's gonna be awesome yeah um restructured the code such that the flask app and blueprint
blueprint classes have sans io bases i don't know what sans io bases are but that moves things like
a request and response over to wrapper sections and out of the app itself things like that um
requires a new version of verx oig and that's pretty much it.
So interesting.
So it's interesting that 3.0 is out.
Way to go, Palette's team.
But it's this deprecate version attribute.
This is interesting to me.
Yeah.
And so the meta,
I haven't played with it,
but that import lib metadata version,
you showed it working with
like a static version metadata, but it probably works with dynamic versions
in PyProject Homo based projects also. Yes, probably.
It's just my lack of experience with that kind of stuff to talk about it.
Interesting. Cool. All right.
You know what else is cool? Our sponsor this week. Let me take a moment and tell
everyone about them.
Once I find where I put my notes. Okay. So this episode, as a couple of recent ones,
have been brought to you by Scout APM, Application Performance Monitoring.
Definitely a cool, cool service. You know, there's a lot of profiling and stuff you can do, Brian,
and it's great to run your code in the profiler. There's a bit of a Heisenberg type of
thing. As you observe it, it changes the behavior of the performance, but you still get a sense.
But it's nothing like my app is running in production. Let's see how it did during Black
Friday. Oh, this was the part that was slow. We could like save a ton of money, run fewer servers
or Kubernetes pods or whatever it is you're doing. If we change it this way, right?
So having that data is awesome.
So if you're tired of spending hours trying to find the root cause of issues impacting
your performance, then you need to check out Scott APM.
It's a leading Python application performance monitoring tool that helps you identify and
solve performance abnormalities faster and easier.
It ties bottlenecks such as memory leaks, slow database
queries and background jobs. And of course, the dreaded N plus one quorum query problem and more
directly to source code so that you can spend less time debugging and more time building. I don't
know where that fits in the 20% of your code example, but I think this kind of stuff counts
too. You'll love Scout because it's built for developers by developers. It makes it easy to get set up and deliver insights in less than four minutes. Seriously, you could be done
by the time you're done listening to this episode or live stream. So start your free trial and get
instant insight today. Visit pythonbytes.fm slash scout. Please use our link because it really does
support the show, right? If they don't know you're coming from us, then they might not sponsor us
anymore. So please pythonbytes.fm slash scout check it out thank you to scout apm for supporting python bytes off to you
brian what's next well next we've got um something fun uh on real python there's a um there's a post
building build conway's game of life with python uh from leo donos ramos um and i just couldn't resist because i think i
have a feeling that maybe there's some new like younger software folks that maybe aren't familiar
with game of life maybe i gotta live a while to know about the game of life well i hope everybody
knows about it but it's a it's kind of fun and so this is just a fairly good walkthrough of if you
were gonna build Game of Life.
And it's one of the cool things about it is building on the command line.
It's using arg parse, but also curses.
And you've got a curses version in the terminal of Game of Life.
And how cool is that?
It's pretty, just pretty fun.
Looking through, I've seen some rather awful implementations of Game of Life over the years.
Like a little bit of Pearl Golf
going on sometimes.
But this one looks pretty good.
It walks through setting up
an entire project
using PyProject.tomo, which is great.
And then talking about the different
splitting up the implementation
into like a grid
and one module, the patterns in another,
different views, and then the CLI in another.
Just the split up looks good for, you know,
modularizing the application.
And kind of if you're, a lot of times people want to play,
like build games or something,
and this is kind of a game that you don't actually have to play.
It just plays itself.
So I just wanted to highlight it.
It's just a fun walkthrough of implementing Game of Life.
Yeah, you already talked about these little projects in your first thing, right?
Here's a cool little project that you can play around with.
And I think games are a good way to get into coding.
Not Doom, Half-Life, whatever.
I've been playing a ton of Smalllands, by the way.
Also not a good way to get in a program.
It seems like something that would take forever.
But Smalllands is awesome.
However, I think this is like little games,
you know, they're easy.
You don't have to have a huge amount of domain knowledge.
You don't have to know about like databases
and connection strings and like,
how do I embed the password?
And how do I make it run all that?
Like just have fun.
Yeah.
And so that one of the fun things about this is like the hiding of,
so it has a,
like a life grid.
So one of the,
of this project,
almost all of the project is going to be transferable to other stuff,
except for the curses part.
Like I'm probably not going to write a whole bunch of curses applications,
but that's the same thing with like most,
a lot of games,
unless you become a game developer,
a lot of the game development,
it's using some sort of,
you know,
user interface stuff that you're probably not going to use for a lot of
projects,
unless you go into using game writing more games,
which is great.
But having that isolated off to one little part was actually,
it's,
it's pretty kind of nice to be able to set that
um anyway but good yeah it is cool to see how you might create that little ui as as well i mean
usually people are doing unreal engine unity pie game arcade one of these is like quite you quite
deep down in those frameworks yeah and it's just a little bit one of the things i wasn't sure about
and i so i i didn't
see any disclaimer at the top i did not run through this but uh was if i didn't know that you could
use curses on windows for instance so is this a uh maybe somebody in the in the audience knows if
you could yeah maybe someone it might be because of windows terminal versus command prompt oh okay
yeah yeah possibly but i But that's a guess.
Zero knowledge.
All right, enough of this fun, Brian.
Let's get down to business.
Okay.
Polar's business, that is.
This comes from Marco Gorelli.
And this is just a simple plugin for Polars.
Now, if you don't know Polars,
Polars is like Pandas with two major differences. It has a lazy, fluent API.
So you can kind of chain commands together and it
doesn't actually execute until you kind of work through them, kind of like generators and those
sorts of things. And it's written in Rust to be super, super fast. So this is a plugin for
pollers. And the idea is it lets you answer questions like how many business days between
now and then. So if like my, my payment system says you get paid net 30, well, or you get paid after
10 business days, but one of those days is the 4th of July.
And then there's the weekend.
And then there's like, how many days, you know, calendar math is already gnarly.
If you want to like throw in skipping weekends, skipping holidays, even worse.
Right.
So super simple.
This thing, the extension itself, is written in Rust.
So it's kind of compatible with Polars.
I believe Polars has actually a Rust API as well as a Python
API.
So maybe this is available there.
I have zero information about that.
All you do is you can even define your own holidays
and weekends.
Like every Wednesday, I'm taking it off, whatever.
So the way it works is you just go import pullers and then pullers business.
Somewhere down here, you can go to grab a column, say offset by and just say 5 BD.
And it'll skip by, you know, it'll go through all the columns, figure out what their dates are and using vector math, convert those to five days ahead by however they offset like each one
individually. That's pretty awesome. Right? Yeah. I don't know. Yeah. I don't know how that works.
That sounds complicated, but yeah. So you can put in like, what are your weekend days? What are your
others? You know, obviously Saturday and Sunday is pretty standard, but like we have restaurants
around here that are closed on Sunday and Monday.
They're open on Friday and Saturday.
So if you're trying to like do business math around like that kind of schedule, like you
could just say Sunday, Monday for your weekend and you're good to go.
Nice.
Yeah.
Well, like one of the things I was thinking about, it was like education wise.
Schools often have like tons of. So many times, so much time off. Yeah. Well, like, um, one of the things I was thinking about, it was like education wise, um, schools often have like tons of, uh, so many times, so much time off.
Yeah.
Like in, uh, like T non teacher non-work days, which, or whatever, it's, uh, the teachers
are still working.
It's just the students aren't, aren't working.
They should call it student non-work days, but they should.
Yeah.
Like my daughter is like, oh, I don't have school this week.
No school is a week. Why? Like, oh, it's parent teacher conference. Oh, my. Okay. Yeah. but they should yeah like my daughter is like oh i don't have school this week not school this week
why like oh it's parent teacher conference oh my okay yeah my daughter even calls they call it uh
her friends call it uh like no no school november yes exactly exactly anyway uh so that would be
good for that or lots of other businesses have wacky holidays so so people can check this
out it's it's you know easy to understand easy to adopt if it's relevant to you but it seems
pretty cool yeah all right brian how extra do you feel today i just have one extra and that is
last week you get uh what i i got convinced that to do a black friday sale uh for the complete pi test course and it is um one word black friday
all caps just that and you get 50 off of uh um off the course so um check it out um i'm and i
and over the break i did a whole bunch on it so i've up to what chapter 13 oh chapter 13 was great
i wanted to talk about that a little bit chapter Chapter 13 was on debugging. And instead of just like artificially adding errors to the code,
I decided to do a, like a test-driven development little thing
of writing a couple of tests first,
doing the implementation and resisting the urge to refactor
during the implementation.
And then once everything was green,
going back and doing a refactoring.
And it kind of made, then explored, I enjoyed learning this anyway.
So it's exploring, testing, debugging, using PyTest plus PDB for the Python debugger.
One of the cool things about learning PDB is you can use it with talks.
So you can debug an individual environment if maybe your code's only failing on 3.8 or 3.9 or something.
So anyway, that's still up and that's my extra.
How about you?
Pretty cool.
Yeah, congrats on getting chapter 13 up.
And I think debugging 13, I think stuff could go,
that's perfect.
As long as you didn't release the chapter
on Friday the 13th, everything will be fine.
I got a few things. First,
there was like a really heartfelt message from Kenneth Wright that he posted on X Twitter.
And so I thought maybe I'd just read that because it's, you know, just kind of remind
everyone of, you know, how nice the community is and why we all like to be here. You up for that,
Brian? Yeah. All right. Kenneth, who's done things like requests and many other projects,
says, Dear Python community, I hope this letter finds you well and thriving in your various endeavors.
Python, my name is Kenneth Reitz.
I've been part of this vibrant community for quite some time, contributing to projects like Quests, Pip, Envy, and among others.
Today, I write to you not just as a fellow Python enthusiast, but as someone who, like many of you, have faced moments of uncertainty and fear in my journey with programming.
It's a path that, while rewarding, is often fraught with challenges and moments of self-doubt.
I'm reaching out to share these feelings because I believe in the power of our community to support
and uplift each other. In recent times, I've found myself grappling with a sense of fear,
fear of not living up to expectations, fear of the unknown or ever-involving field, and perhaps fear
of not being able to contribute as significantly as I have in the past. These are feelings I'm sure many can relate to. But in these times of vulnerability
that I'm reminded of the strength and compassion of the Python community, that we're a group that
not only shares a love for programming, but also cares for the well-being of its members.
Your support, encouragement, and willingness to share knowledge and experiences have always been
a source of strength for me.
So reaching out to ask for your understanding and continued support, not just for me, but for everyone in the community facing similar challenges.
It goes on.
I'll let people read it.
I'll link to it in the show notes.
But I just think that's kind of a nice message.
And I wanted to amplify it a bit for Kenneth.
Yeah.
I mean, obviously, he's someone who people, I know many people look up to as like, wow, look how much he's accomplished. And so if you're feeling uncertain or like you're not contributing enough or whatever, like if Kenneth also feels
that way, like, you know, maybe these are just feelings people have and not, not so much things
that should stop you. All right. So that's one. Remember last time I talked about Python 3.13
0 alpha 1 being out? Well, this time, since it was so close to win three two or alpha two was coming
out alpha two is here as well so i just wanted to link over to that um really many of the features
that they talk about uh what's coming in 313 really just has to do with like deprecations and
very concrete stuff but i know there's the faster c python initiative making a lot of changes although
they're not even at all mentioned here just Just like we removed many deprecated things
and many of the dead batteries that we talked about,
like Telnet lib.
Go find another way to implement your Telnet client
instead of the standard library and so on.
So alpha three will be scheduled for December 19th.
So we can round out our year
with a third Python 3.13 alpha release.
So that's nice and quick.
And finally, as you touched on as well,
thank you to all the people who participated
in the Black Friday thing.
And just, we raised a bunch of money for charity,
which is awesome.
So really happy to have done that.
So just a bit of a heartfelt thanks on my end
for people who participated.
All right, ready for a joke?
Yeah.
The joke is called Select Star,
and I think we can all relate to it. Okay. So Select Star Ready for a joke? Yeah. The joke is called select star. And I think we
can all relate to it. Okay. So select star being theoretically a bad thing. Can I get this image
all the way over? No, not really. Okay. So the, the question here says it's, it's a person. I
think this is a Will Smith from iRobot kind of type of thing. Anyway, it doesn't really matter.
The developer says, can an AI really write efficient SQL queries?
And the AI looks back at the developer and goes,
can you?
The developer goes, hmm.
Maybe not.
And of course, the title is Select Star,
which is like barring joins
and other crazy group buy stuff,
like pretty much like the worst thing
you can do for performance.
Like give me everything.
I don't care if I use it, right?
I know there's like a 10 kilobyte per row description description i don't need that just give it to me anyway i think this is the like
the ideal use case for using an ai is optimizing sql queries but yeah yeah anyway i yeah i wouldn't
trust i don't trust myself with production sql hey so can can ai really write efficient SQL queries? Can you? Okay, well, I see some agreement in the
audience out there as well. Very nice. I want to add a, just a funny thing that my wife shared
with me yesterday. And as many of you know, or maybe you don't know, I used to have short hair
and over the pandemic grew it out. And i have longish hair um i would say
that counts as long and um so often it's pulled back but but it's often not also so i wanted to
share this this funny thing that my my wife shared said uh i admire those with hairstyles i don't
have a hairstyle i have hair most are most days it has zero caterpillars in it.
That's as good as it gets. So a girl, woman with like long flowing hair. Honestly, the length is
about the same as yours right now. So that's pretty awesome. And apparently this was that
came up by Tom Cox. So that's funny. Anyway. Yeah. Very good. Well, also a very good episode.
Good to be back with you and with everyone.
Yeah.
Thank you.
Yeah, you bet.
Thanks everyone for listening.
See y'all later.