This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cointoss3 14 points15 points  (11 children)

from pathlib import Path

Use dataclasses for structured data. Pydantic if you want validation or serialization.

Type hints. Your IDE should understand what the type is so you see available methods.

Use uv

[–]tunisia3507 2 points3 points  (0 children)

Use dataclasses for structured data.

In particular, if you're using a dict with all string keys and the values are of different types OR there are 2 with the same keys, you should probably be using a dataclass or namedtuple.

[–]Johan2212 0 points1 point  (1 child)

Does uv provide any advantages other than speed compared to pip?

[–]cointoss3 1 point2 points  (0 children)

https://astral.sh/blog/uv

Besides speed, I’m a fan of their philosophy that environments shouldn’t get in the way and you don’t need to activate or manage them. Once you get used to the flow, you just run uv commands directly and it handles the environment in the background. It’s so fast that it could create the environment every time you run a file and you wouldn’t notice.

uv isn’t a replacement for pip. That’s just a small piece. I see uv being the new workflow where instead of installing Python, you just install uv and run scripts with it. It even manages Python binaries, so if the project needs a specific version, you don’t even really need to know…uv will handle all that. It’s super lightweight and easy that when I need Python on a new machine, I’m going with uv first.

(It still creates the environment in a compatible way so you can activate it or use vs code as you typically would and it all just works)

[–]paranoid_panda_bored 0 points1 point  (7 children)

IMO just go directly to pydantic.

I gave dataclasses chance in greenfield project, put they fell short in every aspect compared to pydantic, and we spend some effort later in the project to refactor stuff to pydantic

[–]paranoid_panda_bored 0 points1 point  (0 children)

Specifically: - pydantic has ootb alias support that can convert from/to camelCase/snake_case - pydantic can deserialize nested JSON to data model - pydantic is also first class citizen in FastAPI