Pydantic is OP, here's why

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
those of you that have been around a while remember I did a video on pedantic about a year ago which is a library that aims to allow users to represent and validate data using incredibly powerful interfaces that are similar to data classes in that video I use version 1.10 but since then version 2 has come out and while not an awful lot has changed I thought I'd make an updated video showing you how to do it with version 2.0 so I'm going to be showing you not only a simple example on how to create and validate data using pedantic but I'm also going to be showing you an example of how you can use it to represent and work with API data as well because it is really really good for that of course if you find this video helpful at any point then consider leaving a like to let me know and maybe subscribe if you want to see more videos like this but with all that out of the way let's learn how to use pedantic to get started with pedantic we first need to install it so we're going to do pip install pedantic like this and you can see they've done something very clever and made it P oh I think that oh no it's spell with a PE in normal English so they've done something very fancy there and we can see that we installed pantic 2.7.2 pantic core type in extensions and annotated types pantic is very type hint heavy uh which is why it does all that stuff uh and we can start creating a model so uh we need to from pedantic import base model and then for our model in particular I actually want to import date time as DT and then we can create a model just by simply making something inherit from base model so this order is now a pantic model and we can use this similarly to data classes or pretty much exactly the same as data classes really so we could set an ID equals int and we could set an item name is a string we're then going to set a quantity as an INT we're going to set created at and then we have say delivered that which equals DT dat time or none equals none so anyone that's use data classes or even atas before will find this all very familiar you know you define something at a class level you define its type you don't necessarily need to Define um a default value though you can if you want and it's all you know it should all look very much the same so if you were to and create an order in our code so we can do name uh equals main whoops we can do o equals order and then if we set say the ID equals 1 the item name equals test quantity yeah that'll do uh equals we'll set this to like three and then we print o uh we get a perhaps unsurprising output which has you know id1 item name equal test quantity is three and everything is you know formatted properly and you even get the date uh which one of these is the date this one this one's the date the exact date and time I'm recording this video there you go everything looks very normal so far everything looks very familiar you might be wondering if You' never seen panti before why you would want to use it over sunite data classes or atas and there are a few cool little things that pantic does that um that data classes especially don't do and there are actually some things that atress doesn't do either uh so one of the cool things I atas might do this but it does type coersion so we're going to get an error here but if we do this we can see that the type is actually set to an integer it's not displaying in the same way as a test word and we can actually verify this by doing print uh type O uh dot do quantity there we go we can see it's a class int so even though that we've passed in a string our quantity because the field is is actually an integer pantic has taken the liberty of converting it for us I think in I think in atas you have to manually tell it to do that uh in pedantic it does it for you which is really nice one of the other particularly useful things you can do is validators now atas does do this data classes does not um but we could say if we imported field validator and this is one of the changes between version one and version two in vers version one this was just validator in version two it is field validator and this is a decorator so we can set field validator and then we pass in the name of the field that we want to validate so in this case it's going to be quantity and we can set def validate quantity and then we're going to set value uh to be an INT and you'll notice we are passing in the class not self uh so class is actually a um well this turns into a class method technically so you pass in the class and not the instance when you do this and that's it's going to return an INT and then we can say if value is less than one uh we can say that quantity must be greater than zero we can raise a value error otherwise we just return the value straight up if it's fine and now if we set the quantity to three we can see everything's fine if we set the quantity to -1 it starts complaining and it throws a Slightly bizarre looking Trace back but you can see one validation error for order it go us the name of the field that error and the type of the error and then the message of the error and it also gives us some other um some other nice uh details as well I don't think version one did so you can use this to validate Fields as you pass them into the model uh if we do something similar for uh field validator created at because we wouldn't want this to be in the future so we can do validate credit that and it's going to understand exactly what I want to do uh yes it is actually good I understand exactly what I'm going to do uh we can actually probably do this instead there we go that's a bit clearer cuz then we can uh use back text to say that created at must be in the past so it's now comparing it to the current time if it's bad it'll ra an error and we can see we could do say uh if we put this one into the future much like the other one it will error but you'll notice that we actually get an error for both so it would tell us um that that uh firstly that quantity must be greater than zero and it will also tell us that created at must be in the past so it cumulates all the errors together it's not going to be that oh no I've mess. quantity fix that oh no I've messed up created that it tells us both at once which is actually really nice weirdly one thing it doesn't check by default is that assignments pass the validation checks so if we just fix all this and we can can get rid of that so if we do o. quantity equal minus1 or negative 1 when we run it we won't get an error we can actually print o. quantity to confirm this is what we get we you know it sets it to negative 1 so we can bypass it that way if we don't want that to be a thing then we we can pass a configuration so you do that by passing class config into the model itself this is uh anyone that's familiar with Django will recognize this and then it will be validate assignment equals true I think I haven't put this in my notes um so we're going to see if that's right it is there we go so we can now see that our value uh quantity is being validated when we assign it and we can now see it give us o. quality or quantity equal ne1 it will actually tell us where exactly the originated and it will give us exactly the same error as it would have done if you passed it into the Constructor so now we've talked a little bit about how pantic works and I showed you the basics I want to show you a more real world example using an API so the work that I've done with pedantic specifically is mainly with apis they're really good with them um because you can do so much there's so many options that make make it much easier to deal with uh so if we pip install requests uh there we go or you can use your your preferred thing of choice it really doesn't bother me uh and then we import date time uh as DT we then import requests oopsy oopsy does whenever I turn this the screen recording on I just lose the ability to type apparently and then we're going to do from pantic import base model and we also going to import Fields because we're going to be using that and the API we're going to be using is the UK government not that one the UK government's bank holidays API so we're using this one because it's a very simple API and two because it's really funny uh so if we go over and we actually load it uh we can see we get this Json back which is all very nice uh we get our divisions we get our events we get the title we get the date we get the notes and we get whether or not bunting is appropriate which is perhaps the most British thing I've ever seen in my life and I I had to use it I just had to um but it does also allow us to show a number of things because it is a a bit of a non-standard API and we can use uh pedantics Advanced features to compensate for that so first we want to actually fetch data uh so we're going to do oh God uh data equals requests.get URL and then Json so this will get exactly what we're seeing here in a dictionary and then we want to start defining our models and I say models because we're actually going to be doing a few so you see everything is nested in here where you can actually Nest pedantic models and pedantic would just be able to work out what to do so by that I mean we can do a class event base model and then this will handle everything in these events so the title date notes and Bunting will all be handled in there so we can do title equals string uh date is a date time date notes is a string and EMB bunting is a bull we then if we go one level up we then have our divisions so if I actually close these we can see that we have three separate divisions and inside a division we have the division name and the events so we can do class division base model and then we could do name string uh oh actually we'll do division string and we'll come back to that and we'll do events uh list of event so you're now seeing we're already using another pedantic model and then we have the actual bank holidays themselves uh so I'm just going to call this bank holidays and then we will have England and Wales uh division like that we have Scotland as a division and Northern Ireland as a division and that's our entire API pretty much now this won't work at the moment and we'll talk about why that is in a second but if we then do holidays uh equals bank holidays and then we do just pass the data in like that we will will see that uh oops we get a good number of Errors uh so England and Wales it's complaining because um the field is required now this is one of the not so useful errors that panti provides but the problem is that the input value uses a kebab case and in our pythonic class name structure we use snake case now this is quite easy to do so there are two ways of doing this I will show the second way later uh because the division has a problem with this too but to actually change it for all um all of your attributes within the model uh we can do class config again and we can set Alias generator and then this will be a callable so for us we want to make sure that um our aliases and this logic is it feels kind of backwards so what we're doing is we want to define the logic to convert this into this not this into this so we're going to be replacing our underscores with dashes and that will convert our pedantic model name or attribute into the actual name that's provided in the API and if we do that then we'll actually get a successful thing back and then we can print holidays and this is going to be just a disaster because there's just so much stuff uh if we do holidays say do division uh oops dot uh events what is going on events zero and then we'll just do that uh we get an error why is that oh because it's not division it's the name of the division we want we can see that the first event is New Year's Day 2018 that bunting is appropriate for New Year's Day remember that that's very important um and yeah that all works perfectly fine now what I was talking about before when I said that there was something wrong up here is that we have this Division and then division is inside it which is how it appears in the API sure but it's not the the best name for it really it's not the clearest it would be clearer if it was called name because we then have the division and then we have the name of the division under name but of course if we do that it's going to complain because you know England and wales. name it doesn't exist you know it it's called division in the API and this is the second way of creating an alias where you can set this value as a field and you can provide the Alias name is division now again it's a little bit well I suppose it's it's not a little bit backwards when you think of it like this it is the conversion from this into to how it appears in the API but now we've set Alias to be Division and now pedantic knows and when you get division back it should be passed to name and now it works just fine so if I were to get uh print uh holidays. Scotland uh. name we can see that we get Scotland back uh if we print division the name division we don't because even the API has set it to division and even though it's an alias it we haven't set it as such we've set it as name so in the pedantic model it is name even though in the the API is division so whether or not you would want to have that is entirely up to you it really depends on the API it really depends on how you want to wrap these things um but in in this case it can be used to make things a little bit clearer that the name of the division is division on name right rather than division. division so the final thing that I wanted to show you before we go is that uh using these pantic models you can actually create properties on these so if I create a property on event and if we set um def is is past self ball and this will be true if the event is in the past so if the event has already happened then this will be true if the event is in the future this will be false obviously the API doesn't provide that but using um using the fact that this is a model we can provide a property that compares it for us so we can then set is this in the past true because it was New Year's Day 2018 if we look at the last one and I'm actually going to print the last one out I don't know how far it goes um see that boxing day or Boxing Day 2026 is the furthest goes my goodness uh oh this one actually does have notes that's interesting and Bunting is appropriate for boxing day keep that in mind we can see that it's in the future it has not passed so our property fails um or it doesn't fail it returns false but the property is not returned in the repper or or the string representation it still just Returns what the API returns in this bit and then we also have our property that we've done you could also put methods on it if you want you can well it's it's an object treat it as you want you can do anything let me know in the comments if there's anything really cool about pantic you know that I haven't talked about in this video I have barely scraped the surface of what pedantic can do it can do so much there's so much to it you could probably make an entire course about it if you really wanted to so let me know what your favorite things about pantic are if you've used it before if you never used it before let me know what you're planning to use it for now because it'd be really cool to find out if you want to know all sorts of tips and tricks about python elsewhere and all sorts of different different things make sure to watch the python is awesome series it'll be on the end card uh and I'll see you in the next one for whatever we do next
Info
Channel: Carberra
Views: 19,991
Rating: undefined out of 5
Keywords: pyfhon, pytho, pytbon, pytjon, ptyhon, pytyon, ptthon, pyyhon, pythn, pythoh, pythpn, ython, pytgon, pyhon, pytohn, phthon, oython, pthon, pyghon, pythoj, pythno, pythkn, ypthon, pytuon, lython, pyrhon, pythom, pythob, puthon, pgthon, python, pyhton, pythln, pythin, pytnon, pyton
Id: t6Cme1WTFCI
Channel Id: undefined
Length: 18min 9sec (1089 seconds)
Published: Mon Jul 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.