Intro to Pydantic V1

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2024
  • A highly useful library for data modeling, parsing and validation (used in FastAPI and useful for many other aspects of Python software development)
    For Pydantic V2 see here: • Intro to Pydantic V1
    00:00 - Introduction
    02:16 - Basics
    14:41 - Field Aliases
    22:48 - Extra Fields Behavior
    27:12 - Using Config for an Alias Generator
    34:29 - Field Validations
    35:02 - Field Validations - Constrained Numerics
    36:14 - Field Validations - Constrained Strings
    40:16 - Field Validations - Custom Validators
    43:50 - Field Validations - Auto Correcting Field Values using Custom Validators
    46:26 - Field Validations - Multi-Field Custom Validators
    56:19 - Nested Models using Model Composition
    You'll need to understand Python's type hinting syntax - if you're not familiar with that, I have a video on type hinting here: • Python Type Hinting
    #mathbyteacademy #python #pydantic #pythonlibraries
    Code for this Video
    ================
    Available in GitHub code repo: github.com/fbaptiste/python-blog
    Direct link: tinyurl.com/2mhxrv3u
    My Python Courses
    =================
    - Pydantic V2: Essentials
    www.udemy.com/course/pydantic...
    - Python 3 Fundamentals (introduction to Python)
    www.udemy.com/course/python3-...
    - Python 3 Deep Dive (Part 1 - Functional)
    www.udemy.com/course/python-3...
    - Python 3 Deep Dive (Part 2 - Iteration, Generators)
    www.udemy.com/course/python-3...
    - Python 3 Deep Dive (Part 3 - Hash Maps)
    www.udemy.com/course/python-3...
    - Python 3 Deep Dive (Part 4 - OOP)
    www.udemy.com/course/python-3...

ความคิดเห็น • 54

  • @leoiceman10
    @leoiceman10 หลายเดือนก่อน

    really cool to see you have youtube channel now Fred !!!, really there is no one better than you, when it comes to explaining python concepts, btw i also wrote to you linkedin about it, that i got a job in Amazon, using your material on python to prepare for interviews.

    • @mathbyteacademy
      @mathbyteacademy  หลายเดือนก่อน

      Thanks! that's great about getting your job, glad you found my Python material useful for that!

  • @amidfallen
    @amidfallen 2 ปีที่แล้ว +6

    Pydantic is such great library! I'm looking forward to a content about FastAPI.

  • @santoshkaladagi
    @santoshkaladagi 2 ปีที่แล้ว +18

    I bought all your courses in udemy. They are amazing and have helped me a lot. Thanks for uploading these videos on youtube. I hope at some point you make a video on asyncio too. Take care

    • @mathbyteacademy
      @mathbyteacademy  2 ปีที่แล้ว +2

      Glad you like them!

    • @rje4242
      @rje4242 ปีที่แล้ว +3

      same here. Dr Baptiste's Udemy course is excellent and I was happy to hear his voice in a youtube channel.

    • @VivekKumarSinha25
      @VivekKumarSinha25 8 หลายเดือนก่อน

      @@mathbyteacademy Could you please tell which version of pydantic have you used for demonstration in this video?

  • @machinimaaquinix3178
    @machinimaaquinix3178 ปีที่แล้ว +5

    This is a fantastic intro to pydantic. Most other YT videos are too short and narrow. This covers the material in just the right amount with a great real world example at the end. Very well done, you got a new sub and potential udemy course buyer. (when I get to those topics :D)

  • @difegam3
    @difegam3 ปีที่แล้ว +2

    Thanks! You’re an amazing educator. I am in the part 2 of your Udemy course, and I have already bought the other two. Thanks a lot for sharing your knowledge in this incredible simple way.

  • @moose304
    @moose304 ปีที่แล้ว

    Really fantastic walk through/tutorial. One of the best I've seen. Thank you!

  • @kevon217
    @kevon217 ปีที่แล้ว

    Great, thorough walkthrough. Really appreciate your expertise. Excited to start implementing pydantic.

  • @awaismirza7908
    @awaismirza7908 ปีที่แล้ว

    Great work! A suggestion would be to divide the video and GitHub notebook into different sections. This makes following them easy.

  • @arturkabitcher
    @arturkabitcher ปีที่แล้ว

    thanks for the overview, Fred!

  • @pradeepgb986
    @pradeepgb986 ปีที่แล้ว

    Very nice and detailed explanation on using the validators in Pydantic. Thank you so much. Can you please upload a video on validating the data (using pydantic) by reading from a csv file and then inserting the data into a PostGres (Or any database) table?

  • @Daniii3004
    @Daniii3004 ปีที่แล้ว

    thank you very much, great overview!

  • @proMehediBD
    @proMehediBD ปีที่แล้ว

    This is a great tutotial! Thanks a lot for the video!! But one thing is missing from here that is -
    Relation with other table with id.
    For example
    books: {
    title: ""
    author: "objectId"
    }

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว

      Thanks Mehedi - glad you liked it! I'm not following you, can you explain a bit more?

  • @iaaslabs9141
    @iaaslabs9141 2 ปีที่แล้ว

    thank you sir

  •  2 ปีที่แล้ว

    great thank you

  • @CyrilLevyMarchal
    @CyrilLevyMarchal 2 ปีที่แล้ว +1

    As good as your awesome udemy courses!

  • @mathiaz943
    @mathiaz943 ปีที่แล้ว

    Thanks Fred for the awesome tutorial. I have one suggestion: please make sure you are not tying at the bottom of your screen, when I pause the video I see the YT's video control UIs over what you've just typed which makes it impossible to read...

  • @PythonicDevs
    @PythonicDevs ปีที่แล้ว +1

    Best

  • @souravbiswas4489
    @souravbiswas4489 ปีที่แล้ว

    Fred, I am a big fan of your teaching from udemy and this channel. Requesting you to make a video about dbapi, sqlalchemy and alembic. There is not any good tutorial available, at least in the detailed way you teach..

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว

      Thanks, glad you like it!
      However, SQLAlchemy (or other ORMs) is not a topic I will cover - I avoid it as much as possible, since I prefer actually writing SQL by hand.

  • @warrenmarkham8891
    @warrenmarkham8891 ปีที่แล้ว +1

    Anyone know whether Pydantic can be integrated into PySpark? I tried mapping parsed data strings to a BaseModel object instead of a namedtuple object for each record and I had an error related to pickling.
    Background:
    I am creating a PySpark ETL on AWS for an internship.
    I'm new to all three (AWS, PySpark, ETL concepts) so my mental models may be incoherent, but I'm wondering if:
    Use Cases:
    - Pydantic can be used to validate records on read/update
    - Pydantic can be used to validate and generate test data
    - other things?
    No expectation of an answer. I understand that people have their own lives and things. Just thought I would ask.
    P.S: I love everything Fred does. At the end of a hard day learning and working, I relax by learning from Fred's content. They are just so well put together and delivered from such a nice place of calm and thoughtfulness that it is a pleasure rather than a battle to learn from him.

    • @warrenmarkham8891
      @warrenmarkham8891 ปีที่แล้ว +1

      Just saw a link to the Faker library. Maybe I should look into that for the 'validate and generate test data' use case.

  • @SyedMuhammadHussain1
    @SyedMuhammadHussain1 8 หลายเดือนก่อน

    1:10:07 why is the second person's display name None? I think it should be "Oskar M". And btw thanks for such a useful and detailed video on pydantic. I couldn't find something like this anywhere.

    • @mathbyteacademy
      @mathbyteacademy  8 หลายเดือนก่อน

      Yeah, it should be - not sure what happened when I was recording . If you check the notebook in GitHub you'll see the display name correctly populated. Glad you liked the video!

  • @user-yj5wl9xy5t
    @user-yj5wl9xy5t ปีที่แล้ว

    👍🏻

  • @dankprole7884
    @dankprole7884 ปีที่แล้ว

    Great video! 1 question I have is where you added an empty list as the default value for an argument. If we do this in standard Python classes then all instances will share the same list, leading to some potentially unwanted side effects. Does pydantic take care of this for us?

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +1

      It does - you can just try it out for yourself and test by making two instances of the same Pydantic model, and test identity of the attribute of the two different instances (using `is`) - you'll see that the result is `False`

    • @dankprole7884
      @dankprole7884 ปีที่แล้ว

      @@mathbyteacademy thank you - looking forward to using it!

  • @syedabdul8509
    @syedabdul8509 8 หลายเดือนก่อน

    When you print ex.json() @6:18, how are you getting the output in a nice formatted manner?
    For me, the print of a list is a single line with the scroll bar

    • @mathbyteacademy
      @mathbyteacademy  8 หลายเดือนก่อน

      I wonder if something changed in later versions of V1. Certainly things changed in Pydantic V2.x.
      You can use this to print nicely indented JSON: `print(ex.json(indent=2))`

    • @syedabdul8509
      @syedabdul8509 8 หลายเดือนก่อน

      @@mathbyteacademy Yeah, giving indent worked. Also when can we expect the new Pydantic V2 complete tutorial?

    • @mathbyteacademy
      @mathbyteacademy  8 หลายเดือนก่อน

      ​@@syedabdul8509 Glad it worked! I am finalizing a full Pydantic V2 course that will be released on Udemy in early January.

  • @elsavelaz
    @elsavelaz 11 หลายเดือนก่อน

    Can someone please help me demistify “validation checking,” because pydantic seems like typechecking on steroids. Reason is that data in the wild might have some random letters that would pass pydantic but it’s not REALLY data validation. This may be because the way “data validation “ is used varies depending on which part of a workflow or tech stack, but I feel nontechnical audiences can’t wrap head around this nuance, and end up with still not necessarily “good data,” just better data.
    Please correct me if I’m wrong

    • @elsavelaz
      @elsavelaz 11 หลายเดือนก่อน

      Of course a solution for example with names, is using something like the python Human Name Parser in conjunction and add some simple checks and exceptions list, but what about in numeric data that can fall in ranges for multiple IoT streams or such? I once worked somewhere that didn’t understand the many to one faultiness of type checking so their data was lots of sensor data that was scrambled and of course their models sucked … so am checking here to see if pydantic would solve something like that, which I don’t think it can in isolation but maybe I’m wrong

    • @mathbyteacademy
      @mathbyteacademy  11 หลายเดือนก่อน +1

      You can write custom validators in Pydantic. I cover the basics of that in this video. You can also create validators that can reference other fields in the structure (in case you need validation using combinations of fields) - also covered in this video.

    • @elsavelaz
      @elsavelaz 11 หลายเดือนก่อน

      @@mathbyteacademy can you give me an example of a custom validator that makes sure something is a name? Or are you saying, attach something like human name parser to a validator then that is the custom validator ? I’m probably just missing something here… I will also ask chatGPT for a similar example

    • @elsavelaz
      @elsavelaz 11 หลายเดือนก่อน

      Thanks for the response by the way!!

    • @mathbyteacademy
      @mathbyteacademy  11 หลายเดือนก่อน +1

      Yes, you need to write your own Python function (as a pydantic custom validator) that examines the bit of data and determines if it is valid - the implementation of that validation function is entirely up to you. Pydantic has some built-in validators for common things (like integers constrained to some range, text from a pre-defined collection of possible values, etc). But Pydantic isn't going to provide you with more specific validations such as checking if a string is a name - that's something you have to write your own code for.
      As far as how to verify that a string is a name, I have no idea how you would do that - if I gave you a string like "drmla" or "ngoc" how would you determine (not in code, just manually inspecting) if that is a valid name?