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


Transcript #239: No module named pythonbytes

Return to episode page view on github
Recorded on Wednesday, Jun 23, 2021.

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

00:05 This is episode 239 recorded June 23rd.

00:09 It's almost end of June. Wow.

00:11 2021. I am Brian Okken.

00:13 I'm Michael Kennedy.

00:15 And I am Nick Mo.

00:16 Welcome, Nick. Thanks for joining the show.

00:18 Before we jump in, tell me a little bit about yourself.

00:21 Yeah, sure. So as I said, Nick Mo.

00:24 I'm based out of Ohio and I work as a data engineer Trimble Transportation. It's a software company aiming to revolutionize the way we supply the world and simplify and connect the world supply chain. It wants to make it easier to move goods and freight all around the world.

00:43 I'm also the co-organizer of Kleepi, which is Cleveland's Python meetup group.

00:50 Thank you, Michael, for sharing that on the screen. So yeah, that's a bit about me.

00:54 Nice. I enjoyed Cleveland. We're there for Python.

00:57 Yeah, I think I met you guys there.

00:59 Cool.

01:00 I think you guys were in the JetBrains.

01:02 That's right. We were. It was really great to be in Cleveland for a couple years.

01:07 And I guess we just completely missed Pittsburgh.

01:09 But they're going to get another round out of here as a redo, which is cool from COVID.

01:14 I got a chance to speak at the Cleveland Python meetup and talked about memory. Was that right?

01:21 - Yeah, you're talking about how Python might have used memory was like a really cool deep dive.

01:26 - Yeah, thanks.

01:27 That was super fun for having me.

01:28 Now it's good to have you on our show.

01:29 - Yeah.

01:30 - So was that on purpose?

01:31 Did you make a joke that you couldn't remember what the talk was about?

01:34 - No, I think.

01:35 (laughing)

01:36 I know how my brain might store the memory of what I spoke about, but I just, it could have been that or async and I wasn't 100% sure which one it was.

01:44 (laughing)

01:45 - We did talk about async too though, so.

01:48 - Cool.

01:48 - Yeah, yeah, for sure.

01:49 Speaking of async, Brian.

01:51 >> Yeah.

01:51 >> Databases tell us about it.

01:53 >> Well, this is object relational mappers also.

01:58 We have ORMR, which is an async mini ORM for Python, which supports Postgres, MySQL, and SQLite.

02:08 This was a suggestion sent to us by John Hagan.

02:13 Thanks, John, for sending this in.

02:15 I actually haven't played with this a lot.

02:17 I was looking around, it looks pretty neat.

02:18 But I'm going to quote John here.

02:20 He says, "It's a really cool ORM that combines Pydantic models and SQL models into a single definition.

02:28 What is great about this is it can be used to reduce the repetitive duplication between the models for an ORM and the Pydantic models that FastAPI needs to describe serialization." I guess you do have to specify that twice normally.

02:43 >> Yeah. Normally what you do is you would have the data model, the classes that do the exchange on the API level.

02:51 So those are probably Pydantic.

02:53 But maybe then you have something like a SQLAlchemy model.

02:56 And then somewhere in the middle, you've got to copy the SQLAlchemy data over to the Pydantic model send out over FastAPI and you get it back.

03:03 Then you've got to copy that from FastAPI and Pydantic back into SQLAlchemy.

03:08 And because SQLAlchemy types are not really meant to be transferred on the wire, you don't get the open API documentation that you get from Pydantic integration and all those sorts of things.

03:17 So that's normally what happens.

03:18 But if your database model can also be a Pydantic model, then you don't do that back and forth.

03:24 Yeah, and anytime you've got duplication, it's like that dry issue of just, you're going to mess it up sometime.

03:30 It's going to be wrong.

03:31 I think that's why like SQLAlchemy, I think in version 1.4, they've been playing around with a lot of ideas on how to integrate, like data, not Pydantic, but data classes and the ORM style base models.

03:50 Four different propositions of how that should be done, but it's not yet perfect.

03:54 I think that's something that looks like they could learn from Omar here.

03:59 >> Or at least it's good to have these experiments going on for everybody to look around and see how do we move forward so that we can do this cleanly.

04:07 >> Yeah. The one thing I will say is that with all of these ORMs, I don't know why they never give some love to SQL Server.

04:15 I always see Postgres, MySQL, SQLite, but SQL Server is pretty cool too.

04:20 Where is the support for that?

04:23 >> What's SQL Server?

04:24 >> That's like Microsoft's.

04:26 You know what? Fair point.

04:29 >> I think it really matters what audience you're addressing, Nick.

04:37 If you're talking startups and a lot of the open source crowd, yeah, it's all Postgres, or if you're talking to Michael, it's all MongoDB, right?

04:45 But if you're talking to enterprises, boy oh boy do a big bunch of those enterprises run on the Microsoft stack.

04:54 Windows, Windows servers, Microsoft SQL Server, and that's a non-trivial amount of the use cases for these things, so I agree that it should get some attention, even if it's not necessarily the one that the maintainers or many of the people are most keen to use.

05:10 - Yeah, and I agree, it was joke, but.

05:12 (laughing)

05:14 One of the things I wanted to point out that John mentioned is that one of the benefits of ORMR is there's a quick start specifically for FastAPI.

05:25 So you can look at the documentation and there's a FastAPI quick start on how to get this running with FastAPI.

05:31 - What an interesting combination of descriptors from the ORM class side and pedantic models you get here.

05:40 So for this, we have like the Pydantic model based type of thing.

05:45 We've got the columns specified with type information that Pydantic would use, but then you set them to things like an integer column that's a primary key or a string that has a max length setting and things like that.

05:56 Yeah, it's like the worst of every world.

05:58 But it's better than repeating stuff, right? So yeah, interesting.

06:04 Yeah, I think it's pretty good. And Nick, you mentioned SQL, alchemy and data classes.

06:11 Pydantic also has some integration for working with data classes as well. So maybe there's a way to bridge those things across for like fast API and similar situations as well. I haven't tried that, but it's possible. Yeah, let's see out there in the live stream. We got Sam Morley. Hey, Sam says this looks a lot like a Django or M. Yeah, absolutely. It really does. And then Dean is hoping that we'll get some support for a very important database, accessdb, that and, oh gosh, what was it, db2 and a couple of the others.

06:40 Yeah, there's some really important ones that we might be forgetting, but I think it's going to be okay.

06:44 Oh man, access, that gives me PTSD from college.

06:48 I can imagine.

06:51 All right, well, speaking of people who might be getting some trauma, let's talk about nomodulenamed.

06:59 nomodulenamed.com, in fact.

07:02 It's now its own website.

07:03 It used to think of it as like an error.

07:05 And now it's actually a service.

07:08 So error explanations of a service, I guess is what you would call it.

07:11 All right. So Garedoon pointed out that there's this website called no module named.

07:17 And it looks super plain.

07:18 And I went to like, what is this?

07:20 It has 3,626 packages.

07:25 And oh my goodness, like 2 million modules or something like that.

07:28 And it has 151,000 package install guidelines.

07:32 So for example, what if I'm working with HTTPX?

07:35 And I get the message that says, Python error, no module named HTTPX.

07:39 This is what you would have if you wrote import HTTPX and you went and tried to run it, but you were new and you didn't realize there were external dependencies or that HTTPX wasn't built into the standard library.

07:51 You would get that error, right?

07:52 Yeah.

07:53 So this tells you how to fix it.

07:55 It says, oh, this is probably because you don't have the package HTTPS. Let's see if I can go something like fast API that responses.

08:04 Is that a thing? And what will tell me if I try it?

08:06 Oh, no such model. Yeah.

08:08 But so NumPy, for example, it'll give you a lot of these and it'll tell you, this is probably because you don't have the package NumPy or NumPy MIPS 64 installed.

08:19 So that's what I was looking for is if it would show like, well, the package name is not exactly what you're looking for.

08:25 So maybe BS4, sometimes these modules that, so for example, if I say BS4, it'll say, "Oh, it's because you don't have, if you have the error, no module name BS4, it's because you don't have beautiful support installed." It's more than just like, "Duh, pip install the thing that there's no module of." It tries to help a little bit more with understanding that, and it tells you how to get the latest version, it tells you how to install it.

08:49 There's even a related article.

08:51 >> Extremely beautiful SEO on that with with people just googling error messages as well.

08:58 >> Yeah. Pretty interesting.

09:01 Garrett Dunn, thank you so much for sending that in.

09:04 It's simple, but these kinds of things can help people who are new, they're getting in. I think one of the powers of Python is we have people coming from all these different backgrounds and experiences, and they are not all computer science people that know about package managers and love that.

09:19 They're just like, "Oh, I know that I can do cool.

09:21 I can load this file and make a picture out of it that I need to work on, but I get this stupid no module name this what is this right and then they can this you know these kinds of things can help.

09:29 Yeah I'm trying to teach my 11 year old some programming and we started with packaging.

09:34 Yeah we didn't.

09:36 I know you started with virtual environments and then packaging.

09:39 Yeah but Brian I thought you would have started with testing first.

09:44 I always test first.

09:47 I think this is like a really this is like a really cool project.

09:50 I find it super useful when working on projects related to GUIs like Qt or WXPython, because those packages come with so many underlying dependencies, and sometimes you might miss one or might miss something that is an OS dependency that you don't know.

10:12 I feel like this could help you out.

10:13 I know I've run through this a few times where I'm using a package that is built on top of Qt, But it tells me you don't have PyQt GT, PyQt Fibers.

10:24 Exactly.

10:25 Well, I like that.

10:27 I think you probably already mentioned this, but the error message is the module not found.

10:33 That's often not the same.

10:35 It's not the same name as the thing you pip install.

10:38 So.

10:39 Yeah, like one that drives me crazy is DateUtil.

10:41 I love DateUtil.

10:42 I think it's like magic for the pain of parsing dates.

10:45 But that's not what you install.

10:47 you install Python underscore date, you tell, right.

10:50 And so there's just, it's those situations where you're like, why is there no date?

10:53 You tell I have pip install date until, and then it's not even the right thing.

10:56 Or, you know, it's, it's just, yeah, I think it's helpful to sort of put those things together for people.

11:01 Yeah.

11:01 Yeah.

11:01 And for people doing new packages, don't do this if you can.

11:04 even if you have the perfect name for your package, maybe come up with something else that you can actually, it's available on pipe.

11:12 Yeah.

11:13 Yeah, for sure.

11:13 All right, Nick, you got the next one, right?

11:15 Yep.

11:16 I got the next one.

11:18 So I was looking through Jupyter.

11:23 So I'm like, as I said, as a data engineer, I often use Jupyter for like data wrangling and just trying out how to like clean up some kind of data before I actually do the actual cleaning in our data pipeline.

11:37 As I stumble, I got a new iPod.

11:40 I went into like tinkering around with Python code and I was like researching into how to do that.

11:46 and I stumbled across JupyterLite.

11:48 And I was like, okay, cool, JupyterLite.

11:50 But sometimes I'm not gonna always, always gonna be connected to internet using my iPad.

11:55 And then I look deeper into it and it's a Jupyter distribution that runs entirely in the browser and is like built from the ground up using JupyterLab components and extensions.

12:08 And essentially-- - Oh, that's cool.

12:09 - And the kernels that are available are like in the browser.

12:14 So like there's a Python kernel that is like in the browser and it's built using PyIoDyed.

12:20 That was like really cool to see.

12:21 And there's also like, I think there is a, where is it?

12:26 In the user guide, there are other kernels such as, yeah, JavaScript and p5.js, which I think is like a graphics library to build like things on canvas.

12:39 But it was really cool to see like it's supports Python 3.8 and you get like start session, you can run Python code, Python completion, which is really cool.

12:50 It's interesting, they call the kernel Pyolite.

12:53 - Pyolite, based on PyIodide.

12:56 - Yeah, based on PyIodide.

12:58 And this is, I pulled it on, this is how it looks like, and it looks pretty cool.

13:02 So it also supports, right, I think for now, it supports Altair and Olegoid again.

13:08 - I think Matplotlib as well, I think, yeah.

13:11 - Malta, malta.lib.

13:13 And so like open up this Altair notebook.

13:18 It even has something called micro pip, which is like, I don't know what this means, but I think it means that it's like a package manager, but for the browser for Python, which is interesting.

13:33 - And it's asynchronous because it's JavaScript basically, right?

13:37 So it's a wait micro pip installed like Jinja too, or Altair or something like that.

13:42 How interesting.

13:43 - That's very cool.

13:44 I think it also, everything that you download and everything that, all the data that you like load up, it's being stored in the browsers like local storage or some other, I don't know, index DB.

14:00 So it's like self-contained.

14:02 The only thing I noticed is that right now, it's not, what was the word here?

14:09 PWA.

14:10 So, I was just thinking it would be fantastic if that was a progressive web app and they just have it in mostly offline mode.

14:18 Yeah.

14:19 Edge does a great job with PWAs and every time you detect like a manifest or Jason, it will show you, do you want to install this app?

14:26 And I would just love to have like this click install and then have Jupyter light wherever I go, or load it up on my iPad and then disconnect and still be tinkering around with what I want.

14:36 So this is all browser based.

14:38 So that's really cool.

14:39 I'm not gonna run any of these, but I encourage everybody to check this out.

14:43 It's pretty cool.

14:44 - Yeah, yeah, this is really cool.

14:45 I do the same thing with, I use Brave.

14:48 So I have like a YouTube app installed on my Mac and I've got a Twitter app installed at all as Progressive Web Apps.

14:55 So you can just launch them.

14:57 I do wish Firefox supported that.

14:58 Firefox people, if you're listening, bring back the Progressive Web App.

15:01 We all need this.

15:02 - Yep.

15:03 - Yeah, that's cool.

15:04 What are some of the other notebooks in there that look cool?

15:06 Are these like demo ones or did you create these?

15:09 - Yeah, there's a demo one.

15:10 So there are, there's a p5.js one.

15:14 There's the alternate one.

15:15 I don't know what folium is.

15:17 There's the interactive widgets, which is cool.

15:19 So it still uses Jupyter's IPython notebook widgets.

15:24 Matplotlib.

15:26 - Oh, Plotly as well.

15:29 - Oh, cool, and Plotly, cool.

15:31 And so this is a de facto like PyOlight one.

15:33 So it supports Matlab, Pandas, that's cool.

15:38 - It's important to LaTeX as well.

15:39 - Yeah, so like as I was saying before, Pyodide is, what is it?

15:46 Is a like implementation of Python on the browser.

15:51 Actually the implementation of Python is on the computing stack on the browser.

15:56 So I think things like Pandas, NuPy, SciPy, Scikit-learn are already like available, it's within the Pyodide ecosystem.

16:05 So you don't have to--

16:06 I had the guys behind it, you know, Firefox and Mozilla was behind it originally, at least.

16:11 And I had them on talk Python.

16:12 I believe it's web assembly based.

16:15 I think what they did is they took all these major visualization libraries and things like pandas and NumPy and compiled them all into a Python plus those web assembly thing that runs in the browser instead of a JavaScript version, which is pretty awesome.

16:28 Oh, you mean symbol output?

16:30 Yeah, the symbolic output, like the, got the math symbol integral of the square root of - 1/xdx, beautiful. - Yeah.

16:37 - I wonder if you could hand calcs on it.

16:39 - Oh, yeah. (laughing)

16:42 - Awesome.

16:42 Cool, all right, well, that's a really good one.

16:44 I love it.

16:45 All the data scientists out there can definitely enjoy that.

16:49 - Yeah, cool.

16:50 What do we got next?

16:51 - I think you're up next.

16:52 - Oh, right, okay.

16:54 So next we've got--

16:56 - More plotting, maybe?

16:57 - Yeah, more plotting.

16:58 So this is a long title.

17:02 Basically, it's lots of plots.

17:04 There's eight popular graphs made with pandas, Matplotlib, Seaborn, and Plotly Express.

17:11 I've seen a lot of articles and stuff talking about how to do different plots in one or more of these.

17:19 A lot of the articles, and rightly so, are focused on something cool you can do with one library that you can't do with others.

17:27 I've seen Seaborn ones like that, and that's great.

17:30 What I like about this article is it's like, well, let's just take these different pandas plotting and matplotlib, seaborn, plotlyexpress, and do the same plot.

17:42 Let's do something they can all do.

17:45 And so that's what this article does.

17:47 It does a whole list.

17:49 You got normal line charts, grouped bar charts, stacked bars, pies, a whole bunch of things, and histograms, and then you can just compare to see what it looks like before you try.

18:02 And for one, it's got the output, what do the graphs look like, which is important.

18:08 But also, it's a fairly simple article, it's talking about what do the plots look like, but also how do you make them?

18:15 It's in a notebook viewer, Jupyter notebook viewer, and it shows you what's the code look like to get these plots set up.

18:23 And I think that's a big part of choosing your plotting library, is looking at the API to see what kind of API looks comfortable to you.

18:30 I've got to write this code.

18:32 Will I be able to remember this?

18:33 >> Yeah.

18:34 >> Or will it be like regular expressions and I learn it every time I use it?

18:37 >> Yeah. Or if you get stuck with one and you want to switch to other to look at what the deltas are.

18:42 I like these side-by-side apples to apples comparison articles.

18:47 I think this is good for choosing the simple parts of plotting.

18:51 But some of the comparisons are funny because like the bar charts just all look the same.

18:55 but, that one's orange versus orange and blue versus green and blue.

19:02 It's not all the same.

19:02 yeah, but you get down there, some of the fancy ones in the, in the, yeah, they do look great.

19:08 Some of the, what area charts.

19:10 yeah, that one looks great.

19:12 What's that?

19:13 probably express area charts.

19:15 The costume.

19:15 Yeah.

19:16 The area charts look good.

19:17 And what's the donut chart was a donut pie chart with a hole in it.

19:22 Yep.

19:23 Why do people use that?

19:24 What's I think it's because of like with the pie charts, the sectors are kind of, it's kind of, sometimes it can be hard to see like how much with no, like, yeah, the circumference of like at this sector.

19:39 So maybe the donut chart, it kind of makes it easier to see like, okay, this takes like all of this.

19:45 It's just a visual thing to be honest.

19:46 Yeah.

19:47 Okay, good.

19:48 Nick, this is your world.

19:49 What do you think?

19:49 I think this is really cool.

19:51 but to be honest, all of these APIs don't compare to the grammar of graphics from R.

19:57 And so I usually use...

19:59 If I am going to do graphics in Python, I would prefer to use something that like conforms to the grammar of graphics because to me, that's kind of...

20:10 You know how like Python has the import this and it's all philosophy of how you write Python?

20:16 The grammar of graphics has that.

20:18 It gives you these sentences, so to speak, to build graphics.

20:23 That makes so much sense in my head.

20:25 For GraphQL, it's either Altair or ggplot.

20:31 There is a Python port of ggplot that's pretty good, but I think Altair is like the Pythonic de facto version that I've used.

20:40 That's really nice.

20:42 All the other ones that make me have to do do these method calls on objects, just I can't remember it.

20:52 I'll have to come back to something like this.

20:54 How do you use Ganymath or are you using Seaborn?

20:58 I really like the fact that Seaborn has a lot of one-liners to do simple charts in one line, which is great.

21:05 Unlike with the grammar graphics, it still makes you have to build everything out.

21:09 But if I'm building something really custom or I am just building something that I want to have complete control over, the grammar of graphics just gives me a better way of remembering what to do compared to having to remember all this API, all this method API calls.

21:27 >> Well, I mean, the author, Dylan Castillo, says, "Let me know what you think." So maybe we could give him some feedback to add Altair and a couple others.

21:37 >> Oh, yeah, that'd be cool. Dean also has some thoughts out there, Brian.

21:41 Oh, Seaborn and Pandas use matplotlib in the back end, so you can do everything they can do with matplotlib.

21:47 Okay. It'd be harder, but not impossible.

21:49 Also, that's probably why they look all the same.

21:51 [LAUGHTER]

21:54 They are the same. Turtles all the way down.

21:56 He also says, "Remember kids, almost every command in matplotlib returns the object it charts.

22:01 That's the start of OOP, object-oriented plotting." All right, Reno.

22:05 OOP, it'd be two Ps.

22:07 (laughing)

22:09 - All right, well, Brian, you got to talk about databases.

22:15 So I'm going to talk about databases too.

22:17 But my databases are going to be smaller and in memory and embedded, but also about MongoDB.

22:22 So there's this really cool one created by David Latwe called MontyDB.

22:29 So it's a Monty, it's a MongoDB tiny-ified.

22:34 So it's MongoDB implemented in Python, and you can have it in process, kind of like SQLite, I believe.

22:41 We've covered a couple of these libraries that are starting to show up that let you do sort of embedded MongoDB, which I think is really neat.

22:48 So it's inspired by TinyDB, and its extension, TinyMongo.

22:53 So the way you work with it, it's super simple.

22:55 You just import the Monty client.

22:57 If you want to go crazy, you could say as Mongo client, and make it basically the same.

23:01 And then you can give it connection strings like colon memory colon, that should look familiar from something like SQLite.

23:07 And then you can insert data to it, do all sorts of things and do queries against it, run like the MongoDB query syntax against it, and you get the responses back, which I think is pretty cool.

23:18 It's certainly interesting for testing.

23:21 If you told it to use a file storage, it could be an interesting little embedded database and things like that.

23:27 So pretty cool.

23:28 It supports many of the MongoDB versions up to 4.2 and 4/4 on the way with wave emoji.

23:34 I'm not really sure about that, but also--

23:36 - Sweat. - What's that?

23:38 - I think it's sweat.

23:40 - Oh, gotcha, like the work is being done, gotcha.

23:43 So you can pip install MontyDB, and it will work in sort of its way.

23:49 If you wanna use the actual serialization library from MongoDB itself, you can say install MontyDB bracket bson to install that as well.

23:59 And it also has a lightning memory map DB LMDB library.

24:04 It can use that as the storage engine as well.

24:07 So you can pip install, you know, add that on as well.

24:10 So for the storage, you've got in memory, you've got a flat file.

24:13 It'll actually use SQL lite as a back end store, which is pretty cool.

24:17 And then that LMDB lightning memory mapped DB.

24:20 So this looks pretty neat to me.

24:22 If you're gonna do some kind of embedded thing, or you're gonna do some testing and you want something lightweight, it's not a separate server, you've got to set up and run and all those kinds of things.

24:33 This is cool.

24:34 - I think it's awesome.

24:35 - Could you make this a pytest fixture, Brian, that just gives you like preset, sets up your database and gives you access to the connection or something?

24:42 - Yeah, I mean, I actually, I don't, I'm not really a fan of people switching their databases too much for testing because most modern databases have in-memory options or smaller version options.

24:52 But I mean, we use SQLite for tons of stuff.

24:56 That's not just for testing.

24:57 And if you've got SQLite at the back end, there's no reason why this couldn't be a production thing then.

25:03 - Yeah, absolutely.

25:04 - No, this is really cool.

25:06 This could be really useful for like CLI apps that need to store your things.

25:11 - Exactly, you wanna have a little thing, but you don't wanna say, "Oh, you wanna run my little utility, "I packaged it up with Py2 app or Py2 exe or something." You're gonna need to install MongoDB and become an admin of that.

25:23 No, you just use like a SQLite file as the back end store or the LMDB version.

25:29 Another thing that's common from the MongoDB world is there's a set of CLI tools that allows you to manage it.

25:35 So I can connect to it.

25:38 I can import a bunch of exported files from some other or backed up files from some other MongoDB instance and import that into my current server or whatever or create those exports, right?

25:51 There's actually a bunch of utilities called Monty import, Monty export, Monty restore, Monty dump.

25:57 All of these are the parallels of Mongo dump, Mongo restore, and so on.

26:04 If you're used to working with MongoDB, it's not just explicitly that there's some API to talk to some file.

26:11 There's also the tools that are there as well.

26:13 >> Yeah.

26:14 >> Yeah, I think that could be a cool project.

26:17 So why did I make this mostly for just fun and practice, but also needed to run in this limited environments for like render farms in the film industry.

26:27 So that's pretty cool.

26:28 (laughing)

26:29 - It's a side project also with render firms.

26:31 (laughing)

26:33 - It's a side project for my super computer, yes.

26:36 (laughing)

26:37 - I love the name by the way, Monty, Monty Python.

26:40 - Yeah, I mean, yeah, it really brings the MongoDB word play in with Monty Python, Python, or, or Daniel.

26:48 Pretty cool.

26:48 All right, Nick, you got the last one?

26:50 Awesome.

26:51 Exhaustiveness checking with mypy.

26:55 So essentially what exhaustiveness checking is, is a feature of like a lot of type checkers where they guarantee that the programmer has covered all their cases.

27:07 And so with mypy, you could essentially check things like whether you've covered all the, or like you have written all the if statements you're supposed to write at compile time rather than figuring that out at runtime.

27:23 And I really got into using mypy and trying to have it save my butt a lot the way I think about code by embracing types.

27:34 So I stumbled across this, which was really interesting, where this article written by Haki Benita went into how auxiliary net checking actually works.

27:46 So they start out with enum that has order status and you have a function that is called handle order that takes a status, which is an instance, which should be an instance of order status.

28:01 And so in his function, he has it like, if status is order ready, you do something.

28:07 If status is order shift, you do something.

28:10 But then he gave this, like, added this like new scenario where, What if you wanted to check the status of something scheduled?

28:20 And so he tried to run my Py right now and I didn't complain about it.

28:24 So like, okay, cool.

28:26 - Yeah, 'cause one of the things that's very common is if you have something like a set of cases, in this case it's put together in an enumeration, you have more cases over time, but all these if, else if, else if, else if statements all over your code, have you exhaustively gone through and added that case check for all of them?

28:44 Probably not.

28:45 >> Yeah, probably.

28:45 >> You've got a good test, a really good test.

28:48 >> Okay. He proposed one quick way of checking that you handled all cases is by adding this assert false comma on handle status, you pass in the status using f strings.

29:04 Then when you try to pass a state that you have not actually handled before, you actually get assertion error, which is all right, but if you use mypy, there's this clever trick where you create a function called assertNever that takes a value called noReturn and returns noReturn, and in it, it has the assertFalse on handle value.

29:29 Then when you use that function in your handleOrder function, you at the end case, you have this else assertNever and you pass in the status.

29:41 Now when you check with mypy, mypy will know, hey, argument one to assert has incompatible type, literal, or their status schedule, expected, no return.

29:50 >> How interesting.

29:52 >> Yeah. This is a compile time and you can actually get this.

29:54 >> Yeah. I think that's the important thing because I was looking at that going, I could just add the else statement and put the assert there and have nothing to do with mypy and it would catch that error.

30:04 But that catches that error when that code runs.

30:06 Like I said before, hopefully there's test, but oftentimes there's not test for everything.

30:10 >> Yeah.

30:11 And so especially it's there might not be a test for the new thing you've added. And so this is cool in that it it checks all the possible types that could go in there. That's cool.

30:20 Yeah, and it's the part that really got me was that it integrates with your ID. So PyCharm VS code, or any any editor that implements a language server can then like look at this and say, Hey, you haven't handled all your cases, right? And you get that immediate feedback rather than having to run your code and then find out, "Oh, dang it, I missed this case." >> Yeah. People who are not looking at the live stream, YouTube stream, which is almost all the people listening, Nick is showing on the screen this assert function that's checking the enumeration.

30:59 There's just a red squiggly line that literally says, "Assert never has incompatible order status scheduled." That's the missed enumeration case.

31:08 I think that's incredible that actually finds this.

31:10 >> Yeah. It works because mypy uses this technique called type narrowing.

31:18 Essentially, what that means is that it's given a variable as it goes through a control flow, like if statements, switch statements, while mypy will confine or in other words, narrow down the types as it goes through those control flow.

31:39 It works with enumeration types, unions, literals.

31:45 I have in the article there are examples of how you could pass in a union of different types, strings, float, and you could still use this technique and it'll tell you, "Hey, you've missed a case." Or you could do this with literals.

32:00 You have RGB and then you only implemented the, you only check for like two cases, which are R and G, and they need to tell you, hey, you did not handle the B case. So yeah, and so like, the article goes further into different ways in which you could set this up, have my check all of the different cases for you, which is really cool.

32:23 You've even got like the, the various suites for cards, like diamonds, hearts.

32:29 It's interesting that to Mai Pai, when it sees an enum that has like clubs, diamonds, hearts, and spades, all it sees is like a union of literals, which are sweets, cards, sweet clubs, sweet heart, which is actually interesting.

32:45 That's how Mai Pai sees it.

32:46 >> Yeah, that's very interesting.

32:47 Basically, it's emojis.

32:49 >> Yeah, it's basically emojis.

32:52 The one other thing I wanted to mention here is that there was a specific, oh yeah.

33:01 This feature is actually something that Guido actually thought was pretty cool.

33:07 And so I think it's part of PEP 622, structural pattern matching already.

33:12 So if you are matching against an enum or something that has multiple different states, the matching, hopefully Python 3.10 will give you a nice error saying, "Hey, you missed a particular case." And this could really, and if you're a Django developer, or you just use Django, or even, yeah, you use a new ORM, and the ORM provides something like choices, where like yes, no, or dollar, euro, like these kinds of choices in the field, this works pretty well.

33:46 And so in your Django code, you could actually have mypy telling you, "Hey, you missed handling a particular case." - Yeah, that's awesome. - Yeah, which is really cool.

33:56 Yeah, Sam out in the live stream was sort of on to the same thoughts you were talking about with Peter there.

34:01 Wonder if one could hack on the match mechanism to deliver this functionality at runtime using by somehow getting all the variants of the enum and checking the branches in the AST or something. Yeah.

34:13 That's actually interesting because I know that part of the structural pattern matching any object can implement the magic method match, and maybe that is your gate, like that's your entry point into providing that kind of checking at runtime.

34:32 Of course, with Python, anything that has around runtime checking, there's performance costs with that, so be careful.

34:40 >> Yeah, but having this built into Mypy already would be good.

34:45 And Juergen is talking about on the live stream, he says, "I wonder whether you could rewrite the code "to not use if statements at all, "but be more polymorphic." Which I agree, it's a really interesting idea with the method overloading and stuff.

35:00 And it reminds me back a couple weeks ago, Brian talked about function overloading with single dispatch and multiple dispatch.

35:07 And yeah, you could kind of more or less make that happen there.

35:10 So yeah, pretty neat.

35:11 Although you still may miss a case, I'm not entirely sure.

35:14 At least in the enumeration bit, that won't help you, right?

35:16 'Cause the enum will still be the same type.

35:18 It'll just, you'll still have more values.

35:20 - Yep. - Awesome.

35:21 Good one, Nick.

35:22 Brian, what else we got?

35:23 - Well, I've got a couple things.

35:25 One of the things I wanted to note was that this is the second week in a row we've featured an article by Haki, or Haki, and the third in this year.

35:36 So we should probably try to get him on the show or something, so.

35:39 - Yeah, absolutely.

35:40 That sounds good.

35:41 - He's doing some good writing, so thanks.

35:44 The other thing I wanted to mention is I've got, oh yeah, by the way, my book is out.

35:50 - Yay!

35:53 This is the book too.

35:54 - Yeah, second edition of pytest is available for beta.

35:58 So people can tell me everything that's, I already got it, somebody said they have got an issue.

36:04 It's a minor issue with it already, so thanks.

36:08 But it's just been me and my editor so far working through it.

36:11 having more people, more eyes before we go to, go to shipping the physical book would be great.

36:17 So of course this is through pragmatic, but if you go to pytestbook.com, it'll take you there. So that was my extra.

36:24 >> Right on. Cool. I've got a couple as well.

36:26 Yeah, I got some neat ones here.

36:28 So how often do you maybe have like a blueprint, floor plan?

36:32 Maybe you're looking at a house and you're trying to decide whether you want to buy it, what would it be like to actually live there?

36:39 Maybe you're trying to figure out, well, I'm planning out this apartment or I have this place, I want to remodel it like Ikea it all out or something along those lines.

36:48 I ran across this thing that uses some interesting models called plan to scene.

36:53 The idea is it'll take what is literally a floor plan, like a blueprint floor plan that shows like swinging doors and bits, and then you tell it what kind of room it is.

37:03 It's like a bedroom or a bathroom or whatever, and it will generate a 3D world that has things like sinks and toilets and couches that are three-dimensional, not just somehow projected in there.

37:16 There's all of these interesting things you can see.

37:19 There's like if you pull up the site, there's all these spinning worlds and you can see that they've created these little environments just from floor plans, which I think is pretty insane.

37:28 Anyway, you can go ahead, Nick.

37:30 >> That's really cool.

37:32 I wonder if like, because like Tremble, we own like SketchUp.

37:36 I wonder if they do this kind of stuff.

37:38 They take floor plans and then they make it 3D.

37:42 That's really cool.

37:44 - Yeah, there's a whole bunch of comparisons of how it used to be done, how you can pick like different, you know, different flooring and walls and source codes available on GitHub.

37:53 People can run with that.

37:54 So that's pretty cool.

37:55 It's called plan to scene.

37:56 And then just a quick shout out to this TCast podcast.

38:00 I happen to be a guest of recently, and we got to talk about Python and data science and how Python and data are sort of changing the world and stuff, and it's really fun.

38:09 So people can check that out.

38:11 Yeah, and that's it for the things I got.

38:12 Nick, anything else you want to throw out there?

38:14 - Yeah, just a shameless plug.

38:17 As I said earlier on the livestream, I co-host the CLEPI, which is Cleveland's Area Python Meetup Group.

38:25 And so we have meetups every second Monday of the month.

38:30 And one of the reasons why I would encourage anybody across the world or US or everywhere to still come and present is because oftentimes meetups are a great place to present talks that you are planning on giving and maybe like continental conferences or like other larger conferences, smaller crowd and you know, we show you guys a good time, right?

38:59 So it's a great place to come, give your talk, get feedback from that, and then, you know, take on the improve on it.

39:06 So that's one.

39:07 And then the other, shameless blog is that PI Ohio has its own conference coming up on July 31st registrations are open.

39:20 We have pretty cool t-shirts.

39:22 So yeah, register.

39:24 >> Is that live or streaming?

39:26 >> Yes, that is being streamed.

39:28 >> Okay. How about your meetups?

39:30 Are those streamed or live?

39:32 >> Those are virtual. We used to have them in person, and that's when pandemic really messed things up because you have stuff like pizza over and those have a good time talking about Python.

39:43 But no, the virtual setting has given a lot more people access.

39:49 We're able to have more people on.

39:51 >> Nice.

39:52 >> Yeah, that's fantastic.

39:53 Pi Ohio is definitely one of those big regional conferences that a lot of people pay attention to even if they're not in Ohio.

40:00 Are you going to go back to in-person only?

40:02 Are you going to do a hybrid stream and in-person or is it going to be?

40:06 What's your plans for when the world returns to normal?

40:10 >> That's if it ever returns to normal.

40:12 I think we'll change forever.

40:14 But to answer your question, I think my co-organizer and I have been thinking about it.

40:20 We're not set yet, we see the benefits of the virtual, but we also see the benefits of the live.

40:28 Things have changed so much, we don't even know whether the live person is still available.

40:32 But no, it's something we're thinking about.

40:35 >> Cool. Well, it's a challenge, I think all the meetups and other events are having, especially these smaller monthly, bi-weekly things.

40:44 It's one thing to say that there's going to be a big conference and we'll all go to it or not.

40:48 But if you're doing it every couple of weeks, and it's mostly local but not 100% local, yeah, it's a challenge.

40:54 - Yeah.

40:54 - Fantastic.

40:55 All right, Brian, you ready for a joke?

40:57 - Definitely.

40:58 - Okay, so I've got one and then Nick has one.

41:01 So this one, the title of the joke is Root Beer Float.

41:05 Okay, so a programmer walks into a bar.

41:08 He orders 1.000000119 root beers.

41:13 The bartender says, "I'm gonna have to charge you extra.

41:16 "That's a root beer float." The programmer says, "Well, in that case, make it a double." [laughter]

41:22 It's bad, right?

41:23 That's bad.

41:24 Yeah.

41:24 Alright.

41:25 And Nick, you've got one as well. You want to do this one for us?

41:28 Yeah, would someone like to join?

41:30 You want me to be the bearded person?

41:32 Yeah, I really have something going on there anyway.

41:34 Alright, so...

41:36 Will refactoring the code improve the loading time?

41:39 Not really.

41:40 Will it improve the security then?

41:42 No.

41:43 So, it's for browser compatibility?

41:45 Yeah, no, not really. Nope.

41:47 So tell me, why is it always the same old story with you guys wanting to refactor everything?

41:53 I need to know.

41:55 Because as devs, if we know...

41:57 excuse me, if we know we've left behind some messy code, we can't stop thinking about it.

42:02 We wake up in the morning, at lunchtime, in the evening, when we go home, and when we're trying to go to sleep.

42:08 It haunts us, you know.

42:10 It haunts us.

42:12 I love it.

42:14 And it's true too.

42:16 (laughing)

42:17 - Totally true, it's totally true.

42:19 - All right, I have one more joke for you guys.

42:22 - Oh yeah, hit us.

42:22 - All right, how much does a chimney cost?

42:26 - No idea.

42:26 - Nothing, it's on the house.

42:28 (laughing)

42:30 - Very good.

42:31 - I have a friend that is so into dad jokes, which is weird because it's only 22.

42:38 - Practicing, practicing for the future.

42:42 (laughing)

42:43 - Yeah, but--

42:44 I think dads can be blamed for all bad jokes.

42:46 (laughing)

42:49 Yeah, I wanna highlight Juergen says that they cost 2,500 euros.

42:56 - That's expensive.

42:57 (laughing)

42:58 - Well, thanks a lot for joining us today.

43:01 This was a lot of fun.

43:02 And thanks everybody in the stream for showing up and we'll talk to everybody next week.

43:07 - Thanks, bye everyone.

Back to show page