- Podcasts
- Python Bytes
- #389 More OOP for Python?
About this episodeTopics covered in this episode: Solara UI Framework Coverage at a crossroads “Virtual” methods in Python classes Extras Joke See the full…
See the full show notes for this episode on the website at pythonbytes.fm/389
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#389 More OOP for Python?
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds. This is episode 389, recorded June 24th. I'm Brian Okken. And I'm Michael Kennedy. This episode is brought to you by Scout APM. Listen to their spot later in the show and check them out by going to the show notes and clicking it.
You can connect with us on Mastodon. We love hearing from you. And if you're listening to this later, join us live every once in a while if you want. Go to pythonbytes.fm/live to be part of the audience and it'll tell you when the next episode is. And I'd like to say thank you to everybody that signed up for the mailing list to get our show notes.
And they'll probably happen in about the next month to six weeks. So you got to be on the list to win the prize that we invent for the thing that we'll talk about later. So sign up. Yeah. Go on by side of him. Click on newsletter. Put your info in. All right. Well, why don't you kick us off with it's a nice sunny day.
Solera is a pure Python framework, but in React style. So it is a sort of rapid application development, somewhat data science-y focused front-end framework or framework for building front-end UIs that's like React that allows you to write better Jupyter and web apps. So it does this by basically integrating IPy widgets and other types of React-based APIs, programmable, like code, not APIs you call, on top of IPython widgets.
So that's super cool. It can either be stuff that runs inside notebooks that makes them better interactive things, or it can just be standalone web apps, like FastAPI or stuff like that. Yeah? Pretty neat, huh? Yeah. So we always need more UI frameworks than Python, I believe. I would put this if I was considering Streamlit or Dash or something along those lines.
It's kind of in that realm, okay? So let's see. It talks about the benefits of putting things together. But the most important thing I would recommend to you is probably check out their examples to start. So let's check out their examples. So in this area, we've got a whole bunch of different examples that don't want to scroll.
Isn't that crazy? Yeah. And then you can also go and see, you know, what does a tutorial look like? Somewhere. I gotta go to get started. Quick start. So if you go to the quick start, it'll show you basically how to program this stuff. And it's super easy. You just put a decorator, say this is a component, and it shows up in your page.
It's really focused on building these little reasonable and composable components. So it's a little bit like shiny, actually, I think, actually. So anyway, super cool way to build these interactive dashboards and stuff that's more than just a picture, but a thing you can actually play with and publish to the web.
Yeah, especially, I love stuff like this, like, especially if you don't really know how you want to look at the data yet. So you can give people lots of options. Absolutely. And I don't see a price. All I see is Staris on GitHub. So I think it's just an open source project that you can use for your stuff, not a commercial thing, from what I can tell.
So one of the things, I was looking at this the other day, and I don't know if you came across an answer to this question, but it said a pure Python React-style framework for scaling Jupyter and web apps. The and is, like, I was a little bit confused. Do I have to be a Jupyter-type programmer to do this, or do you know if I can?
No. If you want to use FastAPI, it can. And there's a thing called, what do they call it? A Solaris server or something like that. So you can run that, and it says, look, it'll run with FastAPI, Starlet, Flask, and so on. Okay. And I imagine you just create, just basically serve up some common template that, like, starts it all, you know, those kind of things.
The other thing that's worth pointing out in this whole discussion is, it says built on top of React-ton. And React-ton is this interesting project here. It says it's like React. Yeah, it is. It's a really good logo. It's a Python logo as the nucleus of an atom, and then a bunch of little small Python logos circling it like electrons.
That's good. So this basically lets you create code for IPython widgets that is in the style of React. So if you go down here, you can create, for example, a component, which is a clickable button. And you basically initialize the state, and then you write the handler. And just like the handful of lines of code.
Yeah. Yeah. Yeah, it's pretty neat. And so that's what this thing is all about, is creating these little widget-type things or components, and then building up your UI out of it. So I say, certainly worth checking out. I kind of want to just do one of those, like, just a button that just tells you how many times you've clicked it.
No, I'm on the wrong page. I do want to talk about coverage. So Ned Batchelder wrote an article called Coverage at a Crossroads. And so there's a couple things I wanted to point out about this. So the gist of it is, Ned wants to make coverage.py faster. And one of the issues is with the way coverage is written right now and the way it's composed.
So he starts the conversation discussing really how coverage works. And that's the part where I really like, even if you don't care whether coverage is fast or not, this is a nice, fun article to realize how coverage works. And so coverage uses a thing called a trace function. So it takes your code and adds these trace functions to every line so that coverage can know when any line gets hit.
And then coverage also does branch coverage. So coverage does branch coverage with allowing a thing called the arcs. So it generates these four lines that look like branches, these arcs of like, it could go like your line, you could go from line one to line two or line two to line three, things like that.
And so it keeps track of all that. And the reason why things could be faster is because like, let's say you hit line two already. You don't have to, and there's no branches in there or anything. It's just a line of code. You wouldn't need to actually hit the trace function every time you hit that, but there's no way to take it out.
So there's some ideas around making it faster. And SlipCover, for instance, has a bunch of ways. So he uses a bytecode thing that there's a discussion around SlipCover as a different coverage tool. There's also discussion around SysDop monitoring that came in in Python 3.12. And with SysDop monitoring, you can do something like a trace function, but it's something that you can take away later.
So the idea, like for line coverage, this would work just great. So you could just say with SysDop monitoring every time, if I hit this line, great, now I can take that out so I don't have to worry about that line ever again. But there's some discussion that Ned has around how do you deal with branching for that.
And it's an interesting take on kind of an interesting idea. There's an interesting problem set for how to deal with it, kind of like maybe ARCs aren't the right way to do it. Maybe there's some other way to deal with branch coverage. And it's not trivial because there's an example of a try finally block with a return statement happening in the try.
So you don't necessarily hit – you always will hit the finally even with the return, but you don't know if you'll hit the line after. You know, you'd have to look at the code. But really fun discussion. There's a call out of where if you want to get involved, you'd like to offer some solutions or just help with the discussion.
There's that too. But even if you're not going to help, I think it's a cool discussion of how coverage works. And I just wanted to thank Ned for putting time and effort into making sure coverage is an awesome tool. So thanks, Ned. Yeah, that's pretty excellent. And of course, there's going to be functions and branches of execution that are in some kind of tight loop and run a million times.
But after the third time, you've already realized it's 100% covered. So if you could just – because running with coverage and stuff like that, all this profiling and code coverage stuff puts a serious hurting on the performance. So if you could say, all right, that part's done 100%, stop slowing it down, that'd be cool.
But anyway, cool. Awesome, awesome. Up next, thank you to Scout APM. Let me tell you real quick about Scout APM. They're big supporters of Python Bytes, so we appreciate that very much. So if you are tired of spending hours trying to find the root cause of issues impacting your performance, then you owe it to yourself to check out Scout APM.
They're a leading Python application performance monitoring tool, APM, that helps you identify and solve performance abnormalities faster and easier. Scout APM ties bottlenecks such as memory leaks, slow database queries, background jobs, and the dreaded N plus one queries that you can end up if you do lazy loading in your ORM.
And you'll love it because it's built for developers by developers. It makes it easy to get set up. Seriously, you can do it in less than four minutes. So that's awesome. And the best part is the pricing is straightforward. You only pay for the data that you use with no hidden overage fees or per seat pricing.
And I just learned this, Brian. They also have they provide the pro version for free to all open source projects. So if you're an open source maintainer and you want to have Scout APM for that project, just shoot them a message or something on their pricing page about that. So you can start your free trial and get instant insights today.
Check them out. It really supports the show. This one, Brian, comes to us from another Brian. Okay. Way in the past, though. This is Brian Skin that back in January said, hey, guess what? Pep 698 has been accepted. How awesome is that? And I haven't, it hasn't bubbled to the top of my list until now.
But I think it's, we're talking about this is in Python 312. And yeah, let's, let's check it out. So if we go over to the pep, it is an override decorator for static typing. So, you know, some languages, C#, I think C++, but it's been a while since I messed with it. You know, have explicit override keywords and virtual methods.
I know C++ has virtual methods. I just don't know if you indicate that you're overriding it. Tell me, Brian, do you use the override keyword? Or do you just, do you just write the function? You just, just write it. Yeah, that's what I thought. Yeah. Yeah. And in C#, you actually say there's a virtual function in the base class, which is intended to have a potential people overriding.
And then in the derived classes, you say override instead of virtual to indicate that you have to say that right to say that you're, you're replacing this behavior. And you might need to call the base class version and all that kind of stuff. Well, basically Python gets that if you want it as a lot of things with typing, it's multiple, it's optional and multiple ways.
So let's jump over. If you scroll down, it's kind of weird to me. The PEP says this PEP is here for historical reasons. It's now part of Python. So please see override. When you click it though, it doesn't take you to the Python documentation. It takes you a separate typing documentation for Python, but is also in the Python documentation.
Interesting. Which one wins? I don't know, but whatever. The example here in the link from the PEP is super clear. So you had a parent class and you don't say virtual because we don't have that, but you can say at override. So the parent class, the base class has a foo function that takes no parameters and returns an integer.
I'd hear you in the child class, you say override and then as a decorator and then foo, same signature. And if you were to say override something that didn't exist, type checking will say there's no signature. Your intention is to override this thing, but it doesn't exist. Maybe, maybe you meant to type.
All right. Well, thanks, Brian Skin, for sending this in. And yeah, interesting. Cool. One of the other things that's neat is speeding up Python bits. And so there's kind of a neat article called from Gage. Oh, that must be the group that makes this stuff. Anyway, called Parsing Python ASTs 20 Times Faster with Rust.
So this article is talking about speeding up a tool called TAC. Now, TAC is a CLI. It says it's a CLI tool that lets you define and enforce import boundaries between Python modules in your project. Yeah. Remember, I talked about that a few weeks ago. Yep. Actually, right? Yeah. Episode 384, you talked about it.
So yeah, that was just May 21st, just a couple of months ago, or last month. Nice. Nice. Anyway, at first, I'm like, okay, you sped up with Rust. We're kind of hearing that. Why I'm calling this out, though, is not just that TAC seems cool, is that I really liked the methodology that they're talking through here.
So they talk about not just how they made it faster, but the process they went through to figure out which parts. So they used profiling, but in specific, I think they used a tool called PySpy and SpeedScope to visualize and get flame graphs for performance. Interesting. I've not heard of either of those.
I have not, or maybe I have, but we cover a lot of tools, man. Anyway, these look slick, but the output of it showed that there was roughly 90% of the total time was taken by a function called get project imports. And in that, it spent about two-thirds of the time parsing the ASTs and the remaining one-third traversing them.
So some great information and some great examples of instead of trying to just throw Rust at it to speed things up or even just using Python to optimizing your own Python, taking a look at exactly what you're doing and only speeding up the little bits that are slow, which is a – I love that aspect of this.
So, okay, so they took those bits. Also, went by and there's a discussion of why the AST parsing was slow anyway because that should have been some C code as well. But they – there's a little bit of a deep dive into there, but then they jumped into just rewriting it in Rust. And then this – we've covered these tools before using PyO3 and Maturin to help get – quickly develop some Rust for a couple functions.
So, yeah, 20 – that seems like a pretty fast speed up. So, nice. Yeah. Yeah, it's real nice. Also, just kind of a great user example and a short write-up on using these – using profiling to really look at where your bottlenecks are and then maybe throwing Rust at it there. Yeah, and it looks like they just basically re-implemented that one function in Rust and then just used it as a dependency, right, with their PyO3 extension.
Yeah, so I totally want to try that. Like, just – that would be a great way to get into Rust a little bit is to just not try to make a Rust application but just replacing, like, one function or something. This one algorithm is called a ton of times. It's super slow, relatively. It's where we spend all our time.
Do I have extras? Let me check. Yeah, I do. Last – when was it? Let's go ahead. Episode 388. That was just last week, wasn't it? It was last week where we talked about not deleting all the repos. Yeah. Well, one of the things I talked about was – what did I talk about? I talked about import by string with packageutil.resolve name.
Bad on me for not doing my homework a little bit. Brett Cannon notes that discussions around this are maybe not really great things because packageutil is deprecated or it's going to be or something. So, Brett's recommending not using packageutil. Just FYI. If it doesn't give you a deprecation warning now, it will someday.
So, a lot of this boils into a PEP 2026, which is also nice numbering because the shout-out of 2026 is when we would switch. So, the idea would be what we've got – oh, there's a table here. 3.14, no change. That comes out in 2025 if we go to this. And instead of 3.15, we just make the 2026 version 3.26.
Right? 3.12 means 2024. 3.13 means 2025. And so, if you're not using the calendar numbers, I know 26 is closer to 2026 than 15, but there's still, to me, it's like, well, 16, 27. I don't know. It's just, like, here's the next year's version. Yeah. Okay. I'm all for it. I think we should switch because I think that since we've gone to a one-year release cycle anyway, why not somehow encode that in the number?
You know, ways to help fund the maintainers. Those are good, too. But you don't have to fund them. You can just be nice to them. Say thanks. Advocate their thing. That's good. So in that end, I kind of always forget that Brett was involved with PyProject.toml. And we might not be using it as we are today without Brett.
I think only one extra. Extra. And that is last week I talked about the Shiny for Python Reactive Web Dashboard with Shiny, that course over at Talk Python. I said it was going to be available soon. It is now available 100%. You can go sign up for it and take that course. The course is completely free.
No strings attached. So just click the link and take the course if you want to learn about building interactive dashboards. A little bit actually like the Solara stuff, but more full-featured, I suppose. And a little more packaged, a little more like Streamlit in that sense. And then also I talked about working with PyCharm to get people six months of PyCharm Pro for free, as long as you don't already have an account that would have to renew.
But I will, because we may come back to this, right? Okay. But I want to start with book one, The Silent Void, and just read you a few bits of it. And if you visit this webpage, folks, this is best viewed in Netscape Navigator 4.0 or older. Because it's got like the digital noise background and all sorts of like, whoever thought like this arts and craft paper was the proper background for reading.
But here we are. Oh, and it also has a frame. There's a frame inside of it. So it's just missing the blink tag. Nonetheless, here we go. Here is the introduction for the chapter. Thus spake the master programmer. When you have learned to snatch the error code from the trap frame, it will be your time.
So I will call it the Tau of programming. If the Tau is great, the operating system is great. If the operating system is great, the compiler is great. If the compiler is great, then the application is great. The user is pleased and there is harmony in the world. The Tau of programming flows far and returns on the wind of morning.
Oh dear. I think it's a little easy. This one's good, Ali. This is one too. This is the last one. The Tau gave birth to machine language. Machine language gave birth to the assembler. The assembler gave birth to the compiler. Now there are 10,000 languages. Each language has its purpose, however humble.
Exactly. Dear. Well, that's the joke for today. If you will. It's the entertainment segment, Liz. I'm not sure it's a joke. Yeah. No, it's good. I like it. It's the meditation section, Brian. All right. So with that, I'd like to encourage everyone out there to send us good programming jokes because we're running dry.
