12.05.2024 Pydantic is the Python Package I Wish I'd Learned Earlier

� � 
LIVE � �  � � 


Whenever we are dealing with data that. has come from somewhere outside of our. program maybe from our end user or from. api or something like that we really. want to be able to check it validate it. and put it into a pro on into a neat. format so that we can send it through. our application without it causing a. massive issue and burning the whole. thing down so in front of me i have some. json data which i have put together and. if we have a quick look we can see that. for example some of the prices are. strings and some of them are integers. the rest is fairly selfexplanatory but. what we're going to do is we're going to. use pydantic to actually set up models. for this data we're going to import it. and i'm going to show you some of the. cool things that i can do so we're going. to do let's make this a bit bigger from.

Pydantic. we're going to import base model so this. is going to be how we are going to. define our data including the. types that they're going to be so let's. have the first one we'll have class. product so this is going to be the main. product class that we're creating which. is going to inherit from the pedantic. base model so if we look at our data we. can see that we have. each one of these is a product and we. have an id and a title and then each one. has a list of variants as well that has. their own specific pieces of information. so the first thing we're going to do is. we're going to say we're going to have. an id and it's going to be an integer. and then we have our title. which is a string like this. string. these are the first two pieces now what. we're doing you can see is that we're. saying this is the field and this is the.

Type that it's going to be. so the last one is our variance. let's move this in here our variance. which is going to be equal to a new. model now we're going to be saying that. inside this product model here we have. this variance so we're going to say. we're going to have our variant here but. this is going to be a list always going. to be a list. so we'll do list of variants. now we need to import this in from. typing there we go and we'll need to. create this as well so we're going to. create this class here. and this is going to inherit again from. the base model we don't need pass thank. you and we just need to now grab. what information we have for each of the. variants name sku available and price. we've got the name which is a string. we'll have the sku which is also a. string. and we have the. available. available which is going to be available.

Well there we go a boolean true or false. and then we have a price which i'm going. to say is going to be a floating point. number in this instance. so save that so now we have this variant. based model this variant class that we. can call in here now we might want to. think that maybe each product not all of. them are going to have variants so i'm. going to go ahead and put optional in. here and wrap the whole list to make it. optional and we're going to import this. as well from typing up there see so now. we've got our data defined and we're. saying that this is what it's going to. look like it's going to have these data. types and these fields so we can really. start to look at how pedantic will help. us now that we've actually defined this. over something other over something like. just using a plain dictionary so let's.

Create an item using our product class. and we'll say product is equal to item. is equal to product and we're going to. need to have our id. here and this should be equal to. a integer so let's just do one two three. one two three. and let's move this down and then we're. gonna have the what did i call it title. which is going to be a string so let's. just have this as. a cool shirt. and then we have our variance so we'll. say variance variance is going to be. equal to and this is a list so we've. said that it has to be a list here so if. i was to try and make this something. else we would get an error it wouldn't. work i'm going to say i have a list. and inside this list we're going to have. each individual variant like this so we. can see how it's dying to stack together. nicely so the variant had a name name. was equal to i'll just say that this one.

Was small. and then it had a skew so we'll say our. skew is equal to i'll just do. abc123 in this case. and then the available. available. is equal to. and we'll make this boolean because it. needs to be a boolean true and our price. which is going to be. 24.99. so there we go so we've now constructed. how our products gonna look now what. this does is this means we can have. access to. printing out or access to things like. the item dot and then our code editor is. just going to give us the options here. that we have available to us so for. example if you just wanted to access one. of the variants you could do. items or variants we'll access the first. one in the list and now we have. everything automatically here nice and. available to us because we know exactly. what it should be and how it should look. so this is a much better way of working.

With the data so let's just add a few. more variants in. for the sake of it for the moment. that will move itself in just a second. let's put medium we'll change that what. we'll do is we will remove. the 0.99 from here. and we'll turn this into a string so. let's just fill that out there and let's. go ahead and print out the whole of item. and you'll see that what actually. happens. is that because we've defined. the um. available as a boolean. it's changed it from a string of false. to an actual boolean false and the same. with the price we've said that the price. is actually going to be a floating point. number so it has changed it from an. integer to a float and if this was a. string. it would do exactly the same thing it's. changed it here so this is a really neat. little thing within pedantic which means. that.

When we're importing data in and i'll. show you that in a minute when we pull. this in from our data.json file that we. can. have it change the type for us if it's. obvious what it's supposed to be which. is really cool i'm going to move on to. validation just for the moment but i can. hear you already saying but what if i. don't want to have to type this out all. the time but i'll show you when we. actually make getting the data from our. json file i'll show you how easy it is. to create your item classes from that. without having to type it all out every. time so we'll get to that but what we. can do is we can actually have a. validator so i'm going to import the. validator in here and underneath the. variant class i'm going to create my. validator so i'm going to say this is a. decorator and we need to give it the.

Name of the field that we're going to be. validating again so we'll do skew no. excuse so we're going to check that. we're going to validate against this. field here so i'm going to say have a. new function and we'll call this one. skew. length so we're going to validate the. length of the skew for each time. we need to pass in not self we need to. pass in cls this is a class method and. then the value and now we can just check. the length of the values we can say if. the. length. of our value. is not equal to and let's say seven. we're going to give it a error so we're. going to raise a value error and we're. going to write in here. skew must be seven characters 78 no. seven characters. so what we can do is we can start to. construct our actual validation for the. data that we've got and now in this case. i'm just raising an error which is going.

To stop the program but you could do. whatever it is in here that you wanted. to. so if we check out my skus and my. variants they are only six so when we. run this. i'm going to get this skew must be seven. characters in my value error that i put. into my validator here so let's make. these seven. there we go and we'll run it now and. it's past the validation so we have our. item all neat here with the correct data. types or validated based on our sku. length validator. so that's all very well and good let's. just remove this but what if we wanted. to import this in. import data in from an external source. which is what this is all really for so. i have my data.json file which has some. results in it. we have two different uh separate. products with different. uh. different with their own different. variants so let's go ahead and say with.

Open and we'll have our data dot json. i don't know what pycharm's doing there. lovely thank you i don't need all of. that that's really annoying as f. i'll do json.load because we want to. load this in. load f and we'll save this into our data. here and we need to import the json. in from python at the top there we go. now because this is now adjacent object. what we can do is we can say let's have. our items list is going to be equal to. and we're going to use list. comprehension and we'll say that each. one of these is going to be. using our product. base model. we do star star item and we just do for. item in data results now the reason why. we're doing data results is because i've. actually put a results key in at the. first part of my json file so just by. doing this line here we're now going to. import all of the json file all of the.

Items in to this items list. with just this one line validating. against both of our classes here and our. skew length validator so when i run this. now and we get all of this data back. although we'll notice here that in my. validator. we've got sku is equal to none and. that's because i forgot to return. the value if it is indeed correct so now. we've done that we have the information. here all how we wanted it so there's our. product id. title and the variance list there and. again we could access this the first. item here's the title. or we could go the first item. the variance and then the. the second variant in the list dot name. just like this and you have access to. the data using the format like this. which is just so much better and so. useful now you will find that. things like this and type hinting is.

very useful. getting all your data in order and it's. obviously seen a lot in orms and i did a. video actually about the sql model orm. which uses a combination of sql alchemy. and. pedantic to create. your models making your database nice. and easy to put all your information in. in the correct format with the correct. types that video is right here i think. you'll enjoy it thank you very much for. watching

All Devices iOS Android Chromecast