We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode #412 Closing the loop

#412 Closing the loop

2024/12/2
logo of podcast Python Bytes

Python Bytes

AI Deep Dive AI Chapters Transcript
People
B
Brian
Python 开发者和播客主持人,专注于测试和软件开发教育。
M
Michael
帮助医生和高收入专业人士管理财务的金融教育者和播客主持人。
Topics
Brian认为在for循环中直接在循环目标中赋值是一种简洁的写法,虽然存在争议。他认为这种写法与walrus运算符类似,都实现了在循环或条件判断中同时进行赋值和使用变量。他承认使用临时变量的写法更清晰易读,但认为额外的代码行会降低效率。 Michael对这种写法表示中立,他认为这取决于代码的具体情况和个人偏好。他认为在简短的代码片段中,直接赋值可能更简洁;但在较长的循环中,使用临时变量可能更易于理解和维护。 Michael认为使用临时变量的写法更清晰易读,尤其是在较长的循环中,这有助于提高代码的可维护性。他认为直接赋值的方式可能会降低代码的可读性,并且可能导致代码难以理解和维护。

Deep Dive

Chapters
This chapter discusses a debate sparked by Ned Batchelder's blog post about loop targets in Python. The core of the discussion revolves around whether it's acceptable to directly assign values within a for loop, such as assigning to a dictionary key, or if it's better to use a temporary variable for clarity. The discussion also touches upon the walrus operator and its relation to this debate.
  • Debate on assigning values directly within a for loop vs using temporary variables
  • Relation to the walrus operator
  • Clarity vs conciseness in code

Shownotes Transcript

Translations:
中文

Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 412, recorded Monday, December 2nd, 2024. I am Michael Kennedy. And I am Brian Ocken. This episode is brought to you by us, especially our Black Friday things. Visit our website for the Black Friday things. You have 14 hours, so make haste.

Make haste. Hopefully you'll listen straight away. And if not, you know, thanks for supporting our work and check out our courses and things like that. Links are in the show notes. You can also get the summary of every episode delivered directly to you. Handcrafted. Artisanal.

newsletter by Brian Ocken here. So check that out. And we mentioned this last time, but we are now blue sky-ians. We now live in the sky, the blue sky. In particular, Python Bytes is over there with the handle python, you know, at pythonbytes.fm. And both Brian and I are linked directly from there.

So come follow us. I have a little extra, extra, extra to follow up on Blue Sky and Mastodon and Twitter, X Twitter, whatever, all of these things. So I think you will find that interesting. But right now, I would like to know, Brian, what you find interesting. I find interesting that there's a controversy over loop targets. Yeah.

this is so inside baseball, I think, but, um, the, okay. So Ned Batchelder, uh, wrote a blog post and apparently a, uh, social media post on, um, on, uh, on blue sky actually. But, um,

about what loop targets are. So what I'm talking about is a four loop. So if you say four, like four X in range 10 or something like that, then X gets assigned 10, like one zero through nine. Right. So what's, what's the controversy? The controversy is what you should put in for X. So in his little code example, he's got, uh,

For param, he's got a parameter dictionary, and there's a query and a page size. There's no page element, but we're going to fill that in later. So what he's doing is he has coded for params, quote, page in iter tools count. And what that's going to do is it's just going to go through and get 100 things at a time.

and put them in a page dictionary until it's empty. And there's a break to get out of the loop once there are no results left. I think this is kind of clever, and I don't see the problem here. So the problem is this kind of this index into the dictionary, and that's where you're putting the loop parameters. Yeah, this is wild because I've seen exploding or expanding tuples into multiple things. Like for thing in dictionary,

the items, you'd say maybe key comma value in the loop target. And that's perfectly normal, but assigning a key in a dictionary, this is new to me. Really? Okay. Yeah. I'm not necessarily, I'm neutral on it, whether or not it should be done, but I'm just learning about it now. Okay. So the, the, in the discussion, so really what's happening is there's a, there's in the discussion, he talks about it that

you could have like an extra variable. You could say pageNum. So for pageNum in iterToolsCount, it makes it more clear. And then you assign the pageNum to the, you assign that to the dictionary. But really you're just using this pageNum just as a temporary variable, just to stuff it in there.

So I say, why not just put, just, just assign it where you're going to use it. And because this extra line of code, I, I don't know, I'm kind of on the fence because this is more clear. I think it's more clear to use a temporary variable. It's a more readable. However, there's an extra line of code. So it is like that much more, it's not that much more readable. I don't think. And it's not, and it, there's that if,

if if this in this short code snippet not a big deal but in a larger for loop you may have more reason to possibly have something break because somebody like you know commented that line out or something and it suddenly doesn't work so anyway um i'm i'm like this is weird this is controversy but even he so he wrote this up just to talk about it and ask what people think and most of the responses are like no or one i think it's a cool idea and uh i

I think it's a terrible idea. Uh,

Anyway, I guess I'm bringing this up because I just want to point out that with for loops, there's an implicit assignment. And so you can use that assignment to assign to wherever you want to use the variable. I'm going to make an observation here. You tell me what you think. I believe the people who are for this are also fans of the walrus operator. And people who are against this are anti-walrus. Oh, that might be true. Right? It's kind of the same thing. It's in a for loop, you're assigning to a variable...

And kind of sort of defining and assigning in a sense, whereas, you know, the walrus operator does it for if statements, but it's like not assign the variable, then test it. It's like all at once do the assign and test or assign and loop. But there is already an assign in the for loop. It's always assignment. But is it colon equals? No, just kidding. Is it colon in? No, it's interesting. It does. Yeah. It's for loop, the original walrus operator. All right.

I'm actually going to come back to loops, but not yet. I want to talk about the standard library. No, no, not the one that you know, not the one that comes with CPython, the async standard library. It's the missing toolbox for an async world. Did you know we're living in an async world? I feel like Madonna is rocking in my back. Is that Madonna? I don't know.

Yeah. So one of the problems is if you go and look at many of the things that you know and love, say, iter tools or func tools, those things have not been, kind of blowing my mind, because they could be, have not been updated to support async. Oh.

Okay. So when you do like functools, you know, say a decorator at functools.lru cache, that is perfect for a synchronous function. It doesn't work for an async one. And why do I think that it should work for it? Because just a couple of weeks ago, I covered my chameleon flask feature.

decorator for templates. And I, in 100 lines of code, I wrote something that decorates and operates on both sync and async functions. So surely the people who create async IO could probably like write a multi-operator decorator deal, but they don't. And I'd have no idea if there's any

intention ever for them to do so. So what this is, is it's kind of like a clone of those things. Not totally. It also has some other nice features, but it's like an async version of those. So if you have an async function, you want to apply an LRU cache to it. Well, go grab this bad boy.

So it's got a bunch of built-ins for things like asynchronous zip sum or even converting the list. It has func tools that supports things like I talked about LRU cache. And if you look at it, it looks a whole lot like you would imagine. It has a max size. It has...

typed and so on, but it operates on awaitables and it returns an LRU async callable, something you can await like you should the function, rather than just a coroutine that is, I don't know, you cache the coroutine. I don't know. It doesn't make a lot of sense. So this is what this is about. It's got a bunch of things like that. It has the built-ins. It's got the functools libraries for iterators, async caches, attributes, the context lib, an async context lib. So

I can do things like add an async decorator in context that derives from a context decorator. And you can basically short circuit the implementation, a simple implementation of a_enter and a_exit, so on. It has a heap queue, which implements Python's heap queue, but for async, which is pretty cool. So you want to merge and stuff. And then it has some extra tools. And I haven't done enough with this to know whether this is useful, how I would use it. So stick with it. Anyway.

It has things like borrow, where you can borrow an async iterator to prevent it from closing. Okay, I don't know about that. You got scoped ones. But this one is really nice. I've written this code before, and it's not easy to get completely right. It has a thing that you can just call asyncstandardlib.sync, giving it a

async function and it'll just sorry other way around given a sync function it will turn it into an async function that you can await if you need to or yeah or you can give it an async one as well and it doesn't really care I think it adapts but so bunch of stuff going on here if you're like ah there's a bunch of these cool built-ins that I'm used to and they don't work with async well check out the async standard lib

Very cool. I think it seems like we have typed Python and non-typed Python, and now we have async Python and synchronous Python, and then we're going to have free-threaded and non-free-threaded Python. We're going to have typed async free-threaded Python. Yeah. And every other combinatorial possibility there. It's going to be nuts. Yeah. Yeah. But I think this is a cool one. It's not super popular. Let me go back.

to it and see what its GitHub stars are. But it's kind of one of those things that's like, yeah, this is definitely worth it. I don't know. So it has some... Oh, here we go. No, don't say. 240. So it's starting to pick up some speed. But I think it's real simple. It's like the kind of thing that's either going to work or not work. So if it's useful for you, go for it. Nice. Yeah, it's cool. Um...

I was going to talk about, I haven't had breakfast yet, so I was going to talk about some, maybe getting a bagel. You want a bagel? Okay. Yeah. I mean, I love bagels. As long as they got everything, we're good. Everything bagel. Let's go. Well, I am taking a look at a project called Enhanced Jack. It's called Bagels from Enhanced Jack. Who's Enhanced Jacks? It's Jack's Tam. Cool. Aspiring student studying. Oh, nice. University student. Cool. Anyway, why am I bringing up bagels? Well, bagels is, is,

is kind of a fun little expense tracker, but I think it's a great example of using Textual for something that people probably could sink their teeth into pretty easy. So it's an expense tracker with multiple accounts using Textual. I've tried it out. It's really pretty easy. There's a bunch of stuff I like about this. And I'm bringing it up, not really because I think everybody needs an expense tracker, but I think a lot of people look for

starter, like a starter project to possibly tweak and make their own. And I think this might be kind of a fun thing for people to look at a few things about it. I like it that it's a in the command line, it's a textual app, but it's also the install instructions. I love seeing this, it's starting to use the UV tool install so that you can just just run bagels from anywhere. It's the way this is the way this is the way.

And it's so fast to get started. I also like that the project is pretty new, but it's just a few weeks started. But there's already some features included. Features included is great. And then also how the development setup is listed and how to... No, it doesn't talk about how to run tests, but that's all right.

It's running PyTest, of course. But then a roadmap of sort of things that they'd like to add to it

It's heavily inspired by posting. So anyway, just a fun little project. The tests are in place. It's not a complete coverage yet, but it's a new project. So if you want to help out, I think it's a good thing for people to check out. Also, I've always wanted to write my own little expense tracker. And so this is a good start for, even if it doesn't do everything I want it to do, to take up the code base and maybe play with it, learn some...

stuff. It's also, it's written, it's using, can't remember. It was using a Postgres, like a SQL alchemy. Also, if you, if you want to have a simple small project that uses SQL alchemy to learn that, be a good one to take a look at. Awesome. Yeah, that's really cool. And we've covered post, postling, posting, what was it called? The post, the posting. Yeah. Little app that was written in. Yeah, posting. Remember that

That was the postman alternative for the terminal written in textual, which is cool. Yeah, which is like a dream to work with. It's a fun one. Yeah, very nice. Since you brought this up, I'll throw this out here. Not super necessarily relevant, but I recently ran across Maybe.co. It's almost a company, but they lost the M along the way. Anyway, it's a...

Fully open source OS for your personal finances running on Docker. If you want to do self-hosting, keep all of your data private instead of like sending off to Intuit or somewhere that it probably doesn't belong. So anyway, people can check that out. That's kind of cool. Not an endorsement. Haven't used it, but kind of thinking about it. I would rather bring it full circle. We began with loops. Let us end with loops. Isn't that perfect for a full circle? They go in circles, don't they? Yeah. So this is the early days sort of thing.

But Giovanni, who is the creator of the Emmet framework and more relevant to us, the Grainian async-sync Rust-based web server that powers Python bytes and other things that we have, is creating this thing that is an alternative to UV loop. So UV loop is a, I think it's,

based on libv, I can't remember exactly the origins of it, but it's a loop that you can plug in as an alternative for the asyncIO event loop implementation. So

So why would you do that? Well, it turns out that you can optimize some of the juggling of the little tasks. So if you have like three tasks, one is call the website, one is talk to the database, one is write a file or whatever, don't do anything. The built-in one's fine. But if you have a million tasks and you're breaking up into little tiny pieces and they're jumping all over, like that juggling could be faster with UV loop. And I think that's where we're going to see it go with R loop.

So our loop is an async event loop implemented in Rust and it's coming along. It is a work in progress and not ready. But the reason I bring it up early in its life here is it's a really cool option. We've seen how significant the improvements for other Rust things like Pydantic and UV have been and

And so if you're passionate about this and you want to have maybe a little influence before it gets fully baked, as this thing's coming to life, you know, jump in. The way you use it is just like UV. It's super easy. Just before you do async things, you just say asyncio.setEventLoopPolicy, which is

a factory, I'm thinking, kind of a factory method more, I don't know, whatever. And you just give it rloop.eventlooppolicy, and that means any time code creates a new event loop, it's going to be using the factory method from rloop rather than the built-in one. Off you go. Cool. Yeah. Well, cool. Well, that's it for our items, right? I think so, yeah. I think it is.

What do you think? Extras? Yeah, I have a few. You want me to jump in? Jump in. I've had a lot of stuff going on in personal life lately, and trying to fit everything into my life is sometimes difficult. So I've been reading, reading in quotes, listening to the audiobook for 4,000 weeks, a time management timeout.

Time Management for Mortals. It's a book by Oliver Berkman. And I'm listening to it for the second time now in the last couple of weeks. I just picked it up a week or so ago. But I'm really enjoying it. And it's more of a you can't get everything done, but that's okay. Just how to be okay with the limitations of life. So very refreshing time management book. It's also got some practical advice too. But it's great. So highly, highly...

recommend that. It's Advent of Code time, and I've heard of the, so I've definitely heard of the Advent of Code, but the

the advent of code.com. Very cool. A lot of people do that every year for, um, uh, in December to do little code snippets every year. But today I came across, um, Adrian Roselli's development advent calendars for 2024. So if, um, advent of code, isn't quite what up your alley, there's a whole bunch here. There's HTML hell advent calendar. There's a whole bunch of code and code based advent calendars here. So, um,

None of them Python specific. There's a Perl specific, but C sharp advent of cyber. Just quite a, quite a few fun, different calendars. CSS. If you wanted to learn CSS, maybe the Joomla advent calendar, you too can host it. So yeah. And apparently he's going, it's gone back since he's got links back to 2010. So it's fun. Wow. Yeah. If you get your homework done early, you can do more advent account. Yeah.

Yeah, I don't. So I tried Advent of Code a couple of years ago and then I just realized that like in my free time, I'm doing so much coding at work, side hustles and everything that I kind of want to do things like draw and paint and cook and things like that when I'm doing other,

other stuff. So anyway, I hear you. I'm the same. I already do a lot of programming, even in my spare time, I do more programming. Yeah. And so I don't need extra ones, but I know it helps people, especially people are trying to learn a topic. It can kind of force you if you don't have a way to apply it. And today is December 2nd, Monday, traditionally a cyber Monday. And it is the last day for the black, black Friday sale, Turkey sale for the python test.com courses. However, it's,

And so I'll take off the automatic. But if,

If you hit me, if you're listening to this later, close-ish to December 2nd, direct message me on Blue Sky and I'll hook you up. Blue Sky, that's a good transition. All right. Also, Black Friday at TalkPython. So TalkPython.fm slash Black Friday, 20 to 50% off the course library. Nice logo there. The image is great. Thanks. That's pure CSS, by the way. That's some mad glow in CSS.

Yeah, it's cool. Yeah. Awesome. All right. So we talk blue sky. So I just, you know, last week when I finished producing the show and I published it onto the internet, typically go to the social networks and I'll do a quick post. Hey, new episodes out. Enjoy it. If you want it, this one had a little guitar solo at the end, which was super fun. And so it said latest episode out for 11 TLS client. Hello. Guitar solo was the name. And I published that and I published it at the same time with the same text to everyone.

X, Fostadon, Mastodon, and Blue Sky. And I just thought, huh, I wonder what the engagement looks like. Because I don't know how you've been feeling, Brian, but I feel like people...

I tried and tried to get folks to go to Macedon and like some, some people came, came along, but a bunch just didn't. You know, they were just, you would go back to X and you would just see them all talking there. I'm like, man, I don't know. And I'm not necessarily super against X. I'm a little bit against it, but I'm not a lot, but it's just, it's become not very practical as you'll see in just a second. Not very useful. So, you know, you want to go talk to yourself in a closet, it's

It's awesome. You want to talk to other people? Less so. Anyway, so my test here, this is a non-scientific test that I put out there. What is the interaction level per platform? And what you have to keep in mind before I tell you guys this, there's a

dramatic difference in number of followers, subscribers, whatever they're called at that particular location, okay? So basically take the numbers, divide by the number of followers, and consider that, like the amount of interaction. The reason I tell you this is you may want to come follow us and join us on Blue Sky, but that's a bit of a get in the head. So check this out. So over on X, we have 27,000 followers, okay? Posting this exact message one week ago, we got eight likes and two retweets, reposts.

boost name it whatever yeah so I don't know what that is 2 divided by 27,000 but it's a small percentage okay fasted on mastodon right it's not fostered on mastodon because it's across all all the better verse same posts identical here we have a

3,000, let me see, 3,100 followers, okay? So yeah, that's great, but this is almost nine times less, something like that, right? It's many, many less times, but four boosts, which is not out of control, honestly, but it's, and two favorites, two likes, but as a ratio, it's still a lot more because multiply by nine, right? Yeah. Blue Sky, which we've been there a couple of days, we had something like that. We only have a, somebody hover, different hover targets,

750 followers. Yeah. Follow us on PlaySky. Get over there.

Yeah. Anyway, we have 16 likes and two reposts and a quote posts and then some conversation about it. And that's 30 times less followers and more engagement than both platforms. So anyway, I, y'all take that for what it is. I just thought that was an interesting experiment. What do you think, Brian? Yeah, I'm finding, I'm finding blue sky more interactive. I've got about this approximate same numbers on, uh, Fostodon or Mastodon and, and blue sky. And,

And I'll get like twice as much interaction on, I mean, approximate gut feel, twice as much interaction on Blue Sky as I do on Mastodon. Yeah. Yeah. And I'm not saying this to bag on some social network or try to promote other too much. I was like, people are trying to find their community. I think right now this is where the community is. And if you go here, you can go to my account and go to the starter pack and there's the Python personalities and you can follow a

A bunch of us. The onboarding experience is way better. The onboarding experience for Mastodon still feels like it's too many decisions right at once. It's like if you wanted to play games on Linux. You probably could.

And by the way, I'm no longer on X. So don't notify me there. Yeah. Sounds good. All right. A couple of other real quick things. On episode 277, I believe-- I don't have my show notes up, but I'm pretty sure that was the number-- way back when, I talked about strnum, string enumeration, which is a thing that basically backports the string enum from Python 3.11 so it can be used in other places. It's super cool. It's even better than the built-in one, by the way.

So I can say, give me a string enum and derive a class from that. That becomes an enumeration where you say that thing dot. But then you have the fields and just say equals auto, auto, auto, auto. And it'll actually set it to the text of whatever the variable is. If you refactor, rename it, it will apply that refactoring to the string version and the variable version of it. And you can even do things like,

use a lowercase or an uppercase one as the base class, a lowercase strenum, and that will make the string version lowercase, even if you have a different representation for the variable names. Anyway, why did I do this? Because on my ListMonk little client, somebody came and said, why doesn't this work on anything less than 311? I'm like, because it uses strenum from 311. So I'm like, you know what? I'll just derive for the other class strenum.

Add that as a dependency. I already had dependencies. That's one more small one. And guess what? Now it supports older versions of Python. I stopped it at 3.10 because I want nice type hints. I don't want ugly type hints. You're nicer than me, man. I'm like, it doesn't support it. So it doesn't because I don't want to support it.

Yeah, but then I was curious, like, well, why doesn't it? And I remember we talked about this thing, and if I literally just change a bass class and don't do anything else, that was pretty low effort. So, you know, it took like five minutes, right? All right. Yeah. So anyway. All right. That's it for my extras. Shall we?

close it out with a joke. Yeah, let's do something funny. Man, I know we were just talking, we just experienced a food holiday in the United States last week with Thanksgiving and all, and I hope everyone's was good if you celebrate it. However, sometimes there's a lot of food prep and it can be, it can be hard, right? It can be hard. Like you're shopping, you're chopping, all of these things. So here is a programmer or just a computer user really who has a grocery list and the grocery list says eggs, corn, tomatoes, onions, rice, milk,

They decide they don't want the onions. So they highlight it and they hit control X and a tear forms in their eye because of course it does when you cut onions.

That's really funny. It's pretty good, right? I mean, yeah, it's good. I don't know. It's cheesy. It's a little cheesy. Well, it's easy. Yeah. Yeah. I'll do one more for us. That's straight out of the comments because Cohen did a pretty good one. When we were talking about the loop targets, it says, how about this? We're putting two controversial ideas together. Sum of sum of numbers plus X for numbers of zero in number

Numbers if X colon equal numbers squared is such and such like, oh my goodness. This is a lot of stuff going. I said, here's a job interview for you. Job interview question. If you answer with a straight face, you fail.

- If you think that's all right, nope, you're out. I don't know, not seriously, but as a joke, it's pretty good. - Yeah, it's job interview stuff. Job interviews are tough. I know a lot of people are going through that now. - Yeah, well, you know, the control X part and also the job interview might bring a tear to the eyes. I don't know, I haven't applied for a job in, well, since the '90s, however.

Which is insane. It is insane, man. It's actually insane. But because all my job transitions have been like, hey, you'd be awesome. Like, why don't you consider working for it was more the other way around, you know, which is pretty fortunate. But it sounds like your resume has like, like lawn mowing on it still or something. Exactly. Exactly.

I work for companies. They just reach out to me and say, would you consider working for us? I know, but you probably didn't have to. You haven't probably had to update your resume for a long time. Yeah, my LinkedIn is like, I've had some experience at a pizza place, and I've done lawn mowing. So, no, it's good.

No, but I was just going to say, I think it's probably pretty brutal. You've got to, you know, pass the AI gauntlets and all sorts of weird business and take home quizzes. And I can see why there would be tears. Tears, yeah. But not for listening to the show. Thank you everyone for coming. Thank you, Brian. See y'all later. Bye.