Liked Pydantic? You'll LOVE Msgspec

Sdílet
Vložit
  • čas přidán 28. 07. 2024
  • If you've ever needed to work with JSON, TOML, YAML, MessagePack, or even structured data, you'll know how many tools are out there. But what if I told you there was one tool that could do all of those things, complete with a consistent API and with speed as a focus? Well, have I got the treat for you!
    00:00 - Intro
    00:55 - Working with JSON, TOML, YAML, and MessagePack data [1]
    05:38 - Serialisation and deserialisation benchmarks [2-3]
    08:56 - Using Pydantic-like structs [4]
    13:53 - Struct benchmarks [2-3]
    15:16 - Outro
    [1] jcristharif.com/msgspec/usage...
    [2] jcristharif.com/msgspec/bench...
    [3] github.com/Carberra/python-is...
    [4] jcristharif.com/msgspec/struc...
    -
    If you want to see more of me, join my Discord server!
    / discord
    I get a lot of people asking, so here's my setup!
    • Visual Studio Code: • My Visual Studio Code ...
    • Terminal: • Make your terminal loo...
    • Desk/recording gear: kit.co/Carberra
    • PC build: uk.pcpartpicker.com/list/TmzGYN
    Affiliations (I may earn a commission):
    • Codecrafters -- 40% off any plan: app.codecrafters.io/join?via=...
    • Keeper Password Manager -- 30% off your first year: keeper.io/r/EX4FB1C
    -
    If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.
    #python #coding #howto

Komentáře • 23

  • @Mekuso8
    @Mekuso8 Před 19 dny +4

    The "strange" validation that msgspec does is completely on purpose and it fits very well with how I typically use it. Validations at runtime should, IMO, be a last resort as it slows everything down. It's only applied upon decoding, where it's really needed. Everything else is much better handled ahead of time by other libraries like mypy.

  • @YogenderSinghBhabhoria

    Just to clarify the writing modes are binary and text(default), where binary mode returns byte object.

  • @hansdietrich1496
    @hansdietrich1496 Před 21 dnem +1

    Using it for quite a while already. Great piece of software, highly recommended.

  • @Chiny_w_Pigulce
    @Chiny_w_Pigulce Před 21 dnem +2

    Used it in two projects, it's great

  • @ChrisHalden007
    @ChrisHalden007 Před 20 dny

    Great video. Thanks

  • @veritas-odium-parit
    @veritas-odium-parit Před 21 dnem +5

    According to my experience from a project it gets additional speedups if you use the provided Decoder or encoder classes.

    • @Carberra
      @Carberra  Před 21 dnem

      Which provided classes sorry?

    • @oliver3880
      @oliver3880 Před 21 dnem +5

      @@Carberra you can init a decoder/encoder instance and re-use it. Moreover you can set a decoder/encoder to a specific struct increasing performance even more.
      dec = msgspec.json.Decoder()
      dec.decode(msg)
      say you have some defined msgspec.Struct called ResponseMsg
      dec = msgspec.json.Decoder(type=ResponseMsg, strict=True)
      this will decode the json into the ResponseMsg struct and raises an error if the json doesnt comply with your defined struct.

    • @Carberra
      @Carberra  Před 21 dnem +2

      Oooh I see. That's really neat actually!

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 Před 21 dnem

    Thanks.

  • @thekokoserver
    @thekokoserver Před 21 dnem

    Esmerald python web framework has this built in with msgspec and is a very fun to work with😊😊

  • @raccoonteachesyou
    @raccoonteachesyou Před 21 dnem

    Hi ! Great video, very concise and interesting !
    I'm kind of curious about the validator section tho, you can instantiate Structs with bad values but can't dump them.
    The only workaround I've seen for this is to implement a __post__init method and writing custom logic, but that's not ideal.
    Do you know if there's other ways to do this ?
    Thanks !

    • @Carberra
      @Carberra  Před 21 dnem

      As far as I can tell it's the only way, short of contributing something. It does strike me as somewhat of an intentional decision though, which is a bit annoying.

  • @_rozto
    @_rozto Před 19 dny

    Used it for some time, but the hinting in Struct classes was just not working for me in Pycharm

  • @machadinhos
    @machadinhos Před 21 dnem

    Using with open to write bytes instead of pathlib's Path...you will be in big trouble😬
    jk hahaha
    Great video! Great tool!

    • @Carberra
      @Carberra  Před 21 dnem +1

      Tbh I typically prefer Path's methods haha. Glad you liked the video!

  • @PetrSzturc
    @PetrSzturc Před 19 dny

    Do you know how to setup loading json into object if the json has nodes names same as keywords? Eg "class". How would you define that struct?

    • @Carberra
      @Carberra  Před 18 dny +1

      You'd probably need to use aliases.

  • @RollingcoleW
    @RollingcoleW Před 21 dnem

    so was one of the points that pydantic validator is safer?

    • @Carberra
      @Carberra  Před 21 dnem +2

      If you instantiate the class yeah, though if you encode/decode directly, or use a post_init hook, it works similarly.

    • @RollingcoleW
      @RollingcoleW Před 21 dnem

      @@Carberra thanks ok