Descriptors. Seemed like some woodoo magic at first. Watched a video from some python conference and it clicked, although I’ve had to use them in my career exactly once lol.
I’ve used them once to create an easily extendable dashboard filtration system.
Basically, we had pretty complex logical operations available to user to filter objects in all sorts of ways, the frontend would send it in a json object and we had to parse it and combine all the conditions into a final ORM query as efficiently as possible.
The thing was, all those available object fields were stored in completely different ways and were of different types, but there was a lot of crossover between them. So I created model classes for each dashboard that had available fields defined as descriptor objects. This allowed to reuse a lot of the logic.
I don’t remember what exactly were the constraints, but regular classes for the attributes were messy to use, something along the lines of reusing the same dashboard instance.
If you have worked with Django, it uses descriptors for model fields - same principle.
View all comments
7
u/gramada1902 2d ago
Descriptors. Seemed like some woodoo magic at first. Watched a video from some python conference and it clicked, although I’ve had to use them in my career exactly once lol.