Liked Pydantic? You'll LOVE Msgspec

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ค. 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

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

  • @Mekuso8
    @Mekuso8 17 วันที่ผ่านมา +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.

  • @hansdietrich1496
    @hansdietrich1496 19 วันที่ผ่านมา +1

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

  • @Chiny_w_Pigulce
    @Chiny_w_Pigulce 19 วันที่ผ่านมา +2

    Used it in two projects, it's great

  • @ChrisHalden007
    @ChrisHalden007 18 วันที่ผ่านมา

    Great video. Thanks

  • @YogenderSinghBhabhoria
    @YogenderSinghBhabhoria 15 วันที่ผ่านมา

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

  • @veritas-odium-parit
    @veritas-odium-parit 19 วันที่ผ่านมา +5

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

    • @Carberra
      @Carberra  19 วันที่ผ่านมา

      Which provided classes sorry?

    • @oliver3880
      @oliver3880 19 วันที่ผ่านมา +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  19 วันที่ผ่านมา +2

      Oooh I see. That's really neat actually!

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 19 วันที่ผ่านมา

    Thanks.

  • @raccoonteachesyou
    @raccoonteachesyou 19 วันที่ผ่านมา

    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  19 วันที่ผ่านมา

      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.

  • @thekokoserver
    @thekokoserver 19 วันที่ผ่านมา

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

  • @_rozto
    @_rozto 17 วันที่ผ่านมา

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

  • @machadinhos
    @machadinhos 19 วันที่ผ่านมา

    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  19 วันที่ผ่านมา +1

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

  • @PetrSzturc
    @PetrSzturc 17 วันที่ผ่านมา

    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  16 วันที่ผ่านมา +1

      You'd probably need to use aliases.

  • @RollingcoleW
    @RollingcoleW 19 วันที่ผ่านมา

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

    • @Carberra
      @Carberra  19 วันที่ผ่านมา +2

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

    • @RollingcoleW
      @RollingcoleW 19 วันที่ผ่านมา

      @@Carberra thanks ok