Transcript for:
Python Packages Overview

in this video I'll show you 15 really cool python packages that you should know about whether you're a data scientist backend developer or a circus clown some of these packages are going to be very useful to you let's dive in the first package that I want to cover is SQL Alchemy it's the go-to orm for database work in Python this abstracts away much of the SQL complexity while still giving you full control over queries when needed this is what it looks like I've already done a more complete video about SQL Alchemy but just to give you the basics the way that it works that you use Python classes to Define tables in the database here's an example of a user table and each user has an ID and a name and you can indicate things like a primary key indices these kind of things and then because it's a class you can add methods to it that do something with these objects for example here I have added a method that helps with printing user information then the way that you use this is very straightforward you have a piece of code that's sets up the database so it creates a database engine in this case I'm using SQL light but this works also with other database types then you create all the tables you create a session and then you use the session to do things like adding a new user or reading all the users when I run this code then you see we now have added a user named Aron with ID one now I know not everybody likes to use omm some people prefer to stick with SQL queries because well SQL in itself is of course already very powerful but I I do feel there are some advantages to using an omm especially if you're coding in Python for one thing having a class like this makes it easier in your python code to access and change the data for example here I can now simply write new user.name and then I'm going to get the name and then I can do assignment so it ties in really nicely with the python syntax which I think is a really nice benefit of using ORS another thing that ORS do is they add some security because they do things like uh validating and sanitizing input which if you're working directly with SQL query you always have to be careful that you're protecting yourself against injection attacks now that being said omm may not always be the choice for you because it does create a dependency on a particular omm package like SQL Alchemy and if you don't like that particular om then of course that doesn't work all that well also if your queries are really complicated and you can really benefit from the power of SQL then it might actually be harder to use an omm than just write a SQL query and finally of course by using an omm you make yourself dependent on a particular programming language and package so if you want to switch I don't know from python to typescript or rust well rust doesn't have SQL Alchemy it's going to have another type of or so then you need to change everything in the code whereas if you use SQL queries well the queries don't change the second package I want to cover is beautiful soup if you're doing web scraping beautiful soup is a really cool package this helps you parse and extract data from HTML and XML documents really easily now note that web scraping is actually not always allowed so always check the robots.txt file that most servers have and this file is going to indicate whether web scraping is allowed or not for example this is Wikipedia's robots.txt file this disallows specific types of agents and Bots that they want to not have them scrape their data from the website checking whether web scraping is allowed is something you can really easily do in Python here's an example of a script that does that so urp built-in package of python has a robot parser module and I'm using that here to parse a robots.txt file and then I check whether scraping is allowed for a particular user agent so in this case if I Supply that with a website URL like Wikipedia and the user agents then it can check if we can scrape that web it so let me run this then this is actually what you get so in our case we are allowed to scrape on wikipedia.com for this particular user agent now back to beautiful soup this has a bunch of utilities and functions that allow you to easily extract data from HTML and XML in this very basic example I take the URL of my website arnold.com then I get the data I parse the HTML that's what this beautiful soup object does and then I extract all the links from it and then when I run this you see that there there is a bunch of different links here but there's way more things you can do you see there is like a ton of different methods on the soup object that we can call to find things to do all sorts of checks to insert data etc etc so really nice package if you need to work with HTML data third package that I want to mention is simpai so if you're dealing with complex math simpi is really cool this lets you solve algebraic equations symbolically that makes it useful for both Engineers data scientists here an example of what it can do so I'm importing the simp by package and then I have a main function where I create a symbol called X and then I create an equation which is in this case a quadratic equation x^2 + 2x - 8 equals z and then simply I can actually solve this for X so let me run this and you see we have two possible solutions minus 4 and two so that's really cool I personally haven't found a direct use for this because I'm not using my mathematics in this way in my applications that I built but if you do this might be very helpful to you um also another thing that I don't really like about this package is that the type annotations are not very precise so if you go to the definition of solve for example there is no type information here and I think it would be helpful to actually have this so that also uh we don't get these uh type errors in the ID but if you are willing to work around that and accept that limitation and this is a pretty cool package the next package that want to cover is Cookie Cutter this helps you quickly set up new projects by using a template and that reduces the boilerplate code that you need to write at the start now I don't have a specific python example for this one because this is more like a project management tool but it's actually very helpful for that so installing this is really easy you can use pepx and then it actually offers a command line interface and there are a lot of cookie cutter templates available on GitHub this is just some of the thousands of results that you can find if you search for a cookie cutter but for example if I take a look and try to find a cookie cutter template for fast API you see that we have 232 results there is one with react there is a general fast API cookie cutter template there is uh one that's made by Microsoft there is another template made by someone else etc etc and this is also one of the main problems with cookie cutter which is that if you start let's say a fast AP project well you now have to pick between 230 different templates which one is the right one for you that's like really hard to discover so some uh templates in the docs they mention what kind of tooling is involved with it so you can sort of think about whether that's something that you might want to use uh but at the same time uh you may spend like hours sifting through hundreds of different cookie cutter templates without ever really finding what you actually need and then it will would have been better to just spend that time and set up the project yourself from scratch also because then you actually understand what is happening in the template you understand how it's structured whereas with the cook cot templat well basically you the boilerplate code is done for you but you also don't have a clear idea of what is actually happening behind the scenes so the next time you set up a project that cookie cut or template may not work for you and then you're back in cookie cutter template hell so be mindful of this but if you have a template that works for you and that does exactly the things that you need then it is actually a nice thing to have and by the way setting up the scaffolding of your project is just one of the things you need to think about when you're creating a new piece of software check out my free design guide that outlines seven steps I use the design software from scratch you can get it at rn. codesign link is also in the description of this video the fifth package that I want to mention is pickle this is actually in the python standard library but it's a simple way way to serialize and deserialize python objects this is great for saving the state of your program or sharing objects between different systems and the way it works is very basic so I just import pickle I create some objects so I have a uh dictionary in this case with some data and then I open a file and I simply dump that data into the file and similarly I can also open that file and as you can see here this stores the data in a binary format and similarly if you want to load the object back then you just open that file and you do pickle. load and then you get the loaded data so when I run this you see that it actually prints the data that it loaded it's exactly the same as the data that was stored now also here there are some things to be mindful of you can actually not just store data but also code and then when you load that data then that code is actually going to run so uh you have to be a bit careful with that because pickle potentially introduces a lot of security issues in your code because of that reason and second because it stores the data in a binary format you can't easily open this file and look what's in it that's different from for example storing data in a Json file where you can always open the Json file and see exactly what's in there and also that's safer because Json typically won't contain python code whereas in uh pickle file you can actually have python code so the binary format means that if you get a pickle file that you didn't create you have no idea what's inside it until you read it and actually load the object and run the code if there's code in there so be careful the sixth package that I want to talk about is p game and I actually want to play around with this a bit more I haven't really done a lot of game development lately I did quite a bit of that in the past but I haven't done that in Python yet but I'd love to explore this package a bit more and build like a cool game with it if you want me to do video about that let me know in the comments now here's a very simple example of how this works so I have my main function I initialize the game I set the display to a certain resolution and then I'm creating a simple fixed frame rate game Loop that basically refreshes every 60 ticks and that's the Y Loop here so if there is a uh quit event then I'm going to set running to false so then the Y Loop stops and the program quits and then what I did here is I simply fill the screen with a color and then I draw a black rectangle in it and I change the value of some X so that the rectangle moves moves over the screen and when you're on this then basically this is what you get so very basic of course this is not a game at all but that's how it works and then P game is all sorts of different features for building games now again if you want me to dive into that more deeply I'd be happy to do a video about it though probably python is not your language of choice if you want to develop a AAA game or something next up is the package missing Noe this is a package that helps you visualize missing data in your data sets which is really helpful if you need to do a lot of dates are cleaning and pre-processing so how does it work I'm importing the package here I create a main function where I have some data which is just the dictionary and I create a data frame uh of that and there is some missing data in this on purpose so uh you can see that happening right here but of course if you look at the data like this or if you're reading it from a CSV file or something then it's a bit hard to see what is actually missing and what is present in the data set so what you can do with missing knows that you can take this data frame and then use matplot lip to actually create a plot of the missing data for this particular example this is what the plot looks like so these black spaces are places where there is data and the white spaces are where data is missing so this very easily gives you an overview of hey where do I need to fix some things in my data next package I want to mention is Ginger 2 and this is a very flexible template engine you can use this to generate HTML based on an HTML template you can also use this for AI prompts for chat Bots that's actually how we've been using Ginger 2 at iron codes it's very simple here I created a very basic template and then I can render that template by supplying the missing information and then I can print that so when I run that then basically this is what we get but it does way more than just replacing variables like this with an actual value it also supports if statements with conditions Loops uh nested objects those kind of things so if you need to create a complex template then actually Ginga 2 is pretty powerful all right next up is Watchdog and this is great for file system monitoring you can use this to automate tasks that depend on file changes or be notified if something happens in a particular folder this package is particularly useful if you are a circus clown because now you know when somebody tries to access your laptop while you are performing on stage probably to get their hands on all that nasty stuff you have on there you sick bastards told you it would be useful anyway this is what it looks like so I have a python script where I import Watchdog and I have a Handler so whenever uh something is modified in the file system then I'm going to print something and then I'm going to set up the Observer and the file system Handler then I start the Observer and then I keep the script running so that of course the thread is not killed off and at the end we stop the Observer and join the thread so when I run this this is now monitoring changes in the current folder that's my recording folder so here I have a second terminal and now if I create some file like so you see that it notices that the recording folder has been modified and then you can do something with that like response to this file being created or something like that so very helpful next up is the returns package this helps you write save for more functional python code by leveraging monads and patter matching for for example better error handling so they have maybe monad they have all sorts of different other containers that are helpful and they also have a result object here you can see how that works so if I have a division function that takes an A and A B value of course if B equals z we can divide by zero in that case we return a failure object and if not we return a success object and you can use that together with Python's structural pattern matching so that I'm calculating the result so this is not going to raise an exception like we might be used to in Python but then I'm going to match the result if it's success then I'm going to print the value and if it's failure then I'm going to print the error so in this case let's say I divide 10 by two which is allowed right so then it simply prints five but if I try to divide 10 by zero we're going to get the error division by zero as expected and there is no exceptions happening here this is purely handled by montic error handling which is really nice now I do think that you have to be careful with using this kind of approach in your code because this is not built into python I think it makes more sense if this were really built directly into Python and python would kind of move away from using exceptions but I don't think that's going to happen anytime soon so then the question is how useful is this actually because the rest of the Python code or any of the libraries that you're using they will still raise exception so you have to kind of modify them to fit in this framework of error handling let me know in the comments what you think i' I'd be happy by the way to do a more full video about the returns package and show you some of the things that it can do but you do need to consider whether you really want to introduce a new type of error handling in code that probably already raises exceptions in other places next package that I found interesting is numer iser and this allows you to convert written numbers into numerical values and that's useful when you're dealing with human readable Text data so this an example of how it works simply import a package I have my text which is a written number and then I can print the numer ised version of the text and that's actually going to give me an integer value and when I run this then you see exactly what is happening right here now I'd say this is a pretty Niche use case I I have no need for this particular but I still thought it's cool that it's out there and by the way if you're enjoying this video so far you'd like to see more of my content make sure you subscribe to my channel and hit that like button while you're at it it means a lot to me if you do that because it really helps me reach more people here on YouTube all right next up is the Box package this makes dictionaries behave like objects allowing dot notation to access deeply nested data structures and this is how it works so I import the Box class I create some nested data this is a dictionary and then I create a box and then I can access the data using dot notation like so and then when I print this this is going to print UT it took a while why did why was it so slow let me try that again ah okay probably just some weird thing going on anyway that's how it works now you already see a problem with this that type annotations are actually not generated so that means that if you use this then you're going to get all sorts of type errors in your IDE if you have type checking switched on which you should so it would be nice if doing this actually resulted in a typed object you know maybe using a generic so that we Supply the type that box would generate between parenthesis but unfortunately currently the library doesn't offer that I can kind of get around this by using protocols so I can import the protocol type here and then I can create a class um my data let's say we just use uh is active now just to show you how this uh is actually supposed to work so we have is active is a Boolean and then we can make I don't know U we can make my box of type my data and then we can print my box dot is active and then you see we don't get the error here but then obviously we are getting a type error here because box is not off type my data so in the end it's not really a solution so I said what would be nice at least is if box would allow us to specify a type so that we won't have these type issues in the IDE anymore another interesting package that I want to mention is python this introduces functional pipelines into python making it easier to chain operations so instead of having to Define your own compose function or something like that you can use the pipe operator and this is what it looks like so I have here a list of numbers and then I'm creating a result by starting with this list of numbers and then passing it to a filter function and then multiplying all of those Elements by two and so the result you will get by doing this is this particular list let's run this to see that is actually correct yeah so that's what you see right here so this is a nice library to add this type of behavior again the problem with many of these packages is that they sort of seem to directly extend the python syntax by using these type of operators but then actually the type annotations are not really correct so that leads to all sorts of type issues in your IDE which uh is a Pity but still if you perform these type of pipe operations often in your code this might be a nice package for you the next package that I want to mention is nice gooey which is a nice gooey nice gooey makes it easy to build web-based user interfaces which includes dashboards and controls for various types of python application so this actually starts a local web server that you can then go to and then access user interface that way and interact with your python code so it's very simple I have a home function here that has a label and there's an interactive counter that we can increment so here I'm using python to have some logic in my application and then I have a button and when you click that it's going to execute this function and then I call ui. run to actually run it and this is what it looks like and I can now press this button and basically increment the counter a bunch of times in my opinion this looks already a lot better than something like TK inter it's also nice that this uses a web based interface so you can simply open it in your browser you don't uh need to launch an application or something so I think that's really cool if you need a quick UI on top of your python code this is a nice way to do it all right the final thing that I want to show this is actually not a python package but it is really cool it's called screenshot to code and this is a deep learning based tool that converts UI screen screenshots into code so you take a screenshot of a website and then it gives you HTML and CSS really nice so I haven't actually tried this on my machine but basically this is how it works we uh create a screen recording and then it's going to analiz that and then generate the HTML for that which is really fun so if you need to quickly come up with a simple design that looks like something else that already exist this might be a great way to get started and of course this use an AI model to do the job it works with clae it works with uh gp4 and a couple of other things as well so that's really cool there is bait version of this if you go to the website but actually you can also just clone the get repo and then launch it on your local machine that also works so those are the 15 packages and tools but now I'd like to hear from you what's the coolest package that you've discovered lately let me know in the comments if you'd like to learn about even more cool python packages make sure to check out this video next thanks for watching and see you next time