 Hi, everybody. Okay, good. It's working. So many people for a random person talking about the standard library. That's cool Okay, so let's let's get into it. So Let's start with myself. So my name is Ivana, I am currently a back-end developer. I'm working in Austria I'm working on nice mobile app probably doesn't tell anybody here anything so Doesn't really make that much sense to talk about it much I have been dabbling in Python for More than five years. I like tea. I like games. I like weird music and As long as I'm talking about games if you're bored throughout my talk You can you can try to count how many game references there are there are in there I mean, you don't get anything for it. I may have I could have bought something But you just get an insignificant nice point for me Okay, so but We are not here today to to listen to me talk about myself We are here today to listen to me talk about the Python standard library So what you usually hear about the Python standard library is that you know, unlike most things you can buy on Amazon It comes with batteries included so But what is also true about the Python standard library? Not about Amazon is that many of the things in there are actually useful so Yeah, I mean, I'm not I'm not arguing maybe this is useful for somebody but All right, so So the Python standard library can do almost anything for you. There is there is a lot of tools for any use cases and Everybody here probably has used it more is using it every single day There are some let's say hardware limitations to the Python standard library. So Like there is there. There are some things that we can't do yet Yeah, anyway, so I'll be talking about the Python standard library today and most more specifically I'll be talking about three seven three You can you can look it up on on github and you probably already are familiar with the docs So that's my two sources for for this talk So so when people say that the standard library is huge It really is huge. It has more than 800 contributors and more than 10,000 comets and this is just just from basically two months ago. So Maybe this has already grown. I'm for sure. This is already grown What you can also find the standard library more specifically in the in the Python Python part of the standard library is 639 instances of xx in the comments So I looked at the comments because that's like the most important part about the code, right? so like you can find things like Yeah, this is obviously wrong see this issue or Like, you know, somebody leaving a comment about maybe potentially some other comment That would be better to be removed, which is kind of charming itself Or just like, you know playing this cast About your own existence and about the things you've done and and if you see where this is coming from this last example I think you you can see where ever that discuss is coming from But you can also find there again in the comments is 80 mentions of hacks So like see Python developers are just people like us, right? So Sometimes you just you just do what you got to do All right, and we also have some to do is in there. So, you know, like the temporary solutions from the 90s So, yeah, just working around some issue because there is no other nicer way to do it right now but maybe, you know, like 20 years. Yeah, Sunday Sunday 20 20 years from now somebody's gonna come up with something There's 61 fix me Many of which are located in the one in the same file, which is the the test for f-streams Yeah And You know, sometimes as a developer Like comments are basically your way of talking to enough to do the other developers And sometimes you really need to get your point across and like what is the best way to get your point across? Well, like using as many exclamation marks as possible, right? So Also, I would argue that some languages are better suited than others for putting emphasis on your words So, yeah, all right. So why am I starting off with this? It's not just for fun. I mean, there's a little bit of fun in there, but the point is See fighting is pretty cool and it's pretty amazing and we've all been using it and me and we love it but Maybe if you're anything like me, you're not totally confident About the things you're doing and maybe that holds you back from for example contributing to open source or for from contributing to see Python But what this basically shows you is that? Like you can maybe leave it to do in there Like it's fine. It doesn't have to be hundred percent perfect So go for it Okay So let's take a step back from from the most important part of the code To the actual libraries or or modules one more like the libraries in the standard library So I have a very small selection here because I only have 30 minutes, but Basically, I had two criteria for picking something and One one criterion was is this useful and The other criterion was is this somehow weird or funny in some way so I'm gonna show some some libraries and if you can't tell which which one of these criteria like Triggered the being there then I didn't do didn't do a very good job But let's see Okay, so let's start over turtle So I didn't know before I researched for this talk that there is actually a turtle graphics Library in Python. So for those of you who don't know turtle graphics That's basically a way to teach kids programming So you so you get a graphics canvas and you get a pointer and you can program the pointer to move around to draw things and Then you basically like do you do this by by commands and you can group the commands and functions and so on and you learn some abstractions and the sort of thing so So turtle is there basically for kids to to learn things But we can also learn a great deal from looking at the source code of turtle So for example, you learn that sometimes it's okay to just use arbitrary constants Yeah you also You also learn how important it is to properly document your code and Like yeah, sometimes things just go wrong So error messages are also important. Yeah I Encourage you to try this out if you haven't this is there's a turtle basically ships with with a tiny demo and It's very cute. So So try it out All right So another thing I want to talk about this string So string basically I like string because it contains a lot of useful constants that you can use so like imagine that you have to test something and you want to generate random strings and Like you would just need some sort of alphabet so that you can generate them from and And You can just import string and like it will give you all the escalators or the escalower case letters or all the printables And it's all there and you don't have to go through like an extra step of Coming up with a with an alphabet or something Okay next thing I want to talk about is to to free and now I know nobody in this room uses Python 2 anymore, right? but like in the in the very remote case that that this would actually be the case there is 2 to 3 and like Translating Python 2 to Python 3 is is a bummer But there's a lot of things that you can automate away So and that's what 2 to 3 does for you. So it takes away all the tedious Translation stuff that you have to do so so you basically give it let's say this is your file and You would you would call 2 to 3 on it and It will just give you a Python 3 obviously this doesn't magically take care of all your dependencies and like the actual brunt of work That you need to do when when going from 2 to 3, but it helps with with the annoying stuff Okay Next thing I want to talk about is text rep. I think text rep is under-appreciated because it's very nice So text for basically helps you with wrapping text so imagine you have like a long multiline string like this and And you and you and you basically Like when you look at it since it's a triple-quad string you also get all the white space that's in there So basically you get this like all the indents in the in the beginning and maybe our Very likely this is not what you wanted in the first place But that's where text rep comes comes in so so this is our our string that we had We can just import the dent from text rep and It takes and it gets rid of the longest common white space that you have in the string So it's very handy What you also want to maybe want to do is is Reflow your paragraph. So for example make it smaller make it bigger. So basically we arranged the new lines in some way Text rep can do this for you very easily. You just tell it how many characters Should there be the most online and it takes care of of the reflowing for you. Yeah So next up is web browser so That was also news to me But then it kind of makes it made sense and you will see why in a bit That there is that there is a there's a module in the python stand library whose sole purpose is to to give you the means of opening a web page in your browser and Like this might sound simple But if you look at it or if you think about it for a bit It's really not because it supports multiple operating systems. It's it supports multiple browsers and it's actually quite a big mess But it works very well Like the question is what you would actually use this for I Mean, I don't know but if you for some remote reason wanted to open this xkcd comic in the browser You could do it like this and If you don't know what I'm getting at here, please try this out in your python interpreter So I don't know if this was the only reason for including web browsers and module in the standard library But it might as well have been Yeah, and as as long as we're looking at entire gravity, you know, there's actually something else inside there It also has to do with xkcd I'm not I'm not spoiling anything. Look it up. So So the next thing I want to talk about is your old lip your lip helps you with all your URL needs so It it can compose URLs for you. We can break them apart. It can it can URL encode them and so on and so on So imagine like a usual everyday scenario you want to evolve your Eevee and you want to look up how to do it So there are nice nice APIs on the net like for example the pokey API which which has like all the information about everything in Pokemon and You can you can just clear it So let's let's let's just use your lip for it, right because it's right there So it's very simple. So you just import URL open from your lip request And you URL open the URL, right? I mean, yeah, except you should maybe send a header because I mean, it's just you know Good practice to actually tell the people Maintaining the API why you're contacting the API and that they are not malicious So So yeah, I'll send a header, right? Except you can't really do that with URL open But it's not it's not really an issue because you can just import request class from the same from the same module and And just and just send it basically as a dict but with the request So so you just do this and basically you're done, right? Yeah, not really because this gives you bytes But again, this is not an issue you can just import Jason, right? And then Jason load the whole thing and and basically by the by the time you're done doing all of this area He has already evolved out of boredom and like into the most useful useless useless type ever. Sorry, Glens. Yeah so So yeah, I mean, okay. I know I am here advocating for the standard library, but Yeah, maybe this one's you know just So by the way, this is also what the official documentation for your relic recommends so Okay, so we we install requests and then we can just request sketch the whole thing and send the headers along with it And then Jason if I it I mean a dictified Yeah, but that's not actually why I wanted to talk about this I wanted to talk about this because of pretty print and Pretty print is very nice. So what you get back? It's like this really huge Jason I mean not really huge if you work with IPI is like I do but like kind of big Jason and I Like always well a good like if you want to visualize this somehow It's kind of hard though and you could copy paste it to to an editor and somehow beautified and then also Take care of the apostrophe is turning into quotes. So there's actual Jason and like yada yada or you can you know just import pretty print and I'm not it do this all for you in just two lines Yeah Okay, so So the next thing I want to talk about is either tools and you probably know either tools especially if you're coming from functional languages So it helps you with iterating our stuff and it's very handy. So imagine you have you have nested lists like this and You somehow want to flatten it. So there is no flattening Python right as if probably all found out at some point But it's really not that hard to do because there is an inch in it or tools there is a There is a function that does this for you. So it constructs an iterator That iterates over an iterable of iterables and it basically goes Elements by element in the inner iterables So essentially it flatness the list if you make a list out of it It has this a bit weird constructor, but it also has another constructor so you can use whichever one you want Okay, there is another thing that I want to highlight from either tools and that is Take while and drop while there's also a very functional thing So basically if you have if you have a sequence and you want to either Only take a prefix of it or drop a prefix of it and take the rest Based on some condition you can do this with with drop while and take well. So for example here We drop the prefix of the sequence that is smaller than 30 So all the elements that are smaller 30 But once once we once we hit the first one that is greater we still so it's really just about the prefix Yeah, okay, and following along the same lines so functional stuff Fun tools so this is this has to do with higher order functions So functions which take other functions and do something with them There is if if you're coming from Haskell for example, you probably Probably miss your folds or I I mean I did at some point But then I realized that there is actually a fold in in Python is called reduce and it's in fun tools and What it basically does is that it progressively applies a function on a sequence and And and folds it in that in that regard so for example here you take the GCD so the greatest common divisor and You first applied on the 105 and 21 then you take the result of that and Use that as the left operand for the next element and you continue like this until you reach the end of the list So this is handy because if you have something that you can use in this way You can for example find out the GCD of a whole range of numbers not just not just to that is defined on Originally originally, okay Okay So there's this thing that you probably or that you might have experienced in your work life You have a function, which is very expensive So I don't know it does a call to a very slow server or like it takes a very long time to compute or there is some other issue with it But the nice thing about the function is that it's stable. So given the same argument It always gives you the same result. So at some point you probably will come up with some sort of Cash for it, right? So you can just have a dict maybe that that like takes the arguments as keys and Then saves the results so that you don't have to compute it again again When when you already have the result for that for those arguments So, yeah, like it that's about all well and good But then you have to sort of do some some housekeeping around the date like make sure it doesn't grow too large And then you have to decide what you put you're actually going to Going to remove from it once it does and like it's not it's not hard or anything, but it's kind of annoying so you could do all this or you could just import LRU cache from fun tools and Wrap your functions and basically all of this that I just said is happening in the background So you don't have to do anything. It's an LRU cache. So the least recently used elements or the Yeah, the least recently used elements are going to be the first ones to go away Yeah Okay So so far mostly talked about like functions that you can find in the standard library and that you can use There is There's a very nice very nice Library in the standard library That gives you additional data structures. So it's called collections So imagine for example that you have a list like this and you have you have some things there may be multiple times and you want to You want to have a count of each element like how many times it is in the list for some reason So you can iterate over it and like make a dig and count it But you can also just just make a counter out of it which does all of this for you and A counter is basically just a glorified dig it's it's a dig because you can just access the elements with their names keys And the elements being the the actual count of the of the thing And it's glorified because if you try to access something that is not there Then just gives you zero because the count is zero. So no key errors for you Yeah Okay, let's let's go back to the normal dig So imagine you have a dig like this now like you have people and you have their occupations and Obviously what happens if you try to access somebody who is not there well Python is unhappy with you and for a good reason. So There's there there are many use cases though where you where you don't want this to happen and That's where default dict comes in so again from collections a default dict is basically a Dictionary that does something on on missing elements. So instead of throwing the error it would it would Execute the lambda here and it would save save the result basically so Again, so we so we created basically an empty default dict here We insert the first two people and now if we try to if you try to access Brandon We get the not available back because that's the that's the lambda that we provided in the first place Yeah, and that's a good thing because if you try to actually like put all of Brandon's Occupations in there that would be a bit too much maybe Okay, so So now imagine this scenario. Okay, you okay, not you but like the other person Okay, not you wrote this like very convoluted code and it's like there is like a goal called progression going over multiple modules and then you can for life of you Realize what's going on and like where this thing is called from and and he's over your frustrating. So There is trace back for you Which basically just prints the exception phrase or the the stack trace nothing separate phrase in this case So in this case like if you have three functions like this You and you print stack in the third one you see that Pacific it was called from full verify and for where if I was called from from Lincoln and Okay, now I realized that probably some people in the audience are like looking at me like what is she talking about like? I have a debugger right so I just put the breakpoint there that she also you've used prints to debug but But okay, I'm gonna settle this debate once and for all okay, so Have your fancy IDs, but can your fancy IDs help you clean your bathroom? Like I didn't think so so Just kidding IDs are great Yeah, so that was that was it So there's a ton of good stuff in there. That is what there is a ton of funny stuff in there, too There is not enough time obviously to talk about everything Like not not nearly enough So this was this was a very small selection So I just want to reiterate basically what I said at the start that The Python standard library can do almost anything like with the hardware limitations that we talked about Although Has actually anybody tried this like I'm gonna embarrass myself here now, but let's try Okay. Oh, that's something. Oh, yeah Okay, my point here being But for the things you cannot find in the standard library You can just go and buy PI. There's a lot of weird stuff on there Yeah, thanks. Thank you very much Ivana for wonderful Presentation We have five minutes for four minutes for questions. So please Yeah, okay So this is not a question, but I recently found out that clearly you can import this in an interpreter Import this in part. Oh, yeah, that is a very nice one, too. Yeah, so as long as I have the I have the big screen Actually here Yeah, it's a bit Questions the other integrative one. Yeah, you don't want to do your research. I actually don't have don't have internet here But yeah, I can just I can do it now, sorry I'm too nervous, but you have to look it up, but it's it's just there and I don't want to spoil it That's the right reason any question I Have one there was There's an image that appeared briefly on one of the slides Who an image an image? What was it? Yeah, it was one think everyone was always wondering that Probably mean the slug right Before the reference That's one. Yeah, well, this is this is like the quote is from FTL this game And that's a slug. That's a race. That's in the game Okay, like you're piloting your spaceship and like micro managing everything anymore questions. No, then Thank you