Liked Pydantic? You'll LOVE Msgspec
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
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.
Using it for quite a while already. Great piece of software, highly recommended.
Used it in two projects, it's great
Great video. Thanks
Just to clarify the writing modes are binary and text(default), where binary mode returns byte object.
According to my experience from a project it gets additional speedups if you use the provided Decoder or encoder classes.
@Carberra
19 күн бұрын
Which provided classes sorry?
@oliver3880
19 күн бұрын
@@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
19 күн бұрын
Oooh I see. That's really neat actually!
Thanks.
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
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.
Esmerald python web framework has this built in with msgspec and is a very fun to work with😊😊
Used it for some time, but the hinting in Struct classes was just not working for me in Pycharm
Using with open to write bytes instead of pathlib's Path...you will be in big trouble😬 jk hahaha Great video! Great tool!
@Carberra
19 күн бұрын
Tbh I typically prefer Path's methods haha. Glad you liked the video!
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
16 күн бұрын
You'd probably need to use aliases.
so was one of the points that pydantic validator is safer?
@Carberra
19 күн бұрын
If you instantiate the class yeah, though if you encode/decode directly, or use a post_init hook, it works similarly.
@RollingcoleW
19 күн бұрын
@@Carberra thanks ok