What are the best methods for combining marshmallow and OO schema definitions in Python?

Suppose a simple scheme defined in marshmallows

class AddressSchema(Schema): street=fields.String(required=True) city=fields.String(required=True) country=fields.String(default='USA') class PersonSchema(Schema): name=fields.String(required=True) address=fields.Nested(AddressSchema()) 

In this case, applications that work with objects in memory and serialization / deserialization in JSON are used, i.e. SQL database.

Using the standard json library, I can parse JSON objects that match this schema and access objects in the same way as person1['address']['city'] , but using string patterns in the detailed syntax is somewhat unsatisfactory.

Handmade OO Model

I could define a parallel OO model and annotate my schema with @post_load decorators, for example:

 class Address(object): def __init__(self, street, city, country='USA'): self.street=street self.city=city self.country=country class Person(object): def __init__(self, street, city=None): self.street=street self.city=city 

But the repetition is not very pleasant (and I did not even include the description in the diagram).

No OO Model

Perhaps the explicit OO model doesn't buy much - these are basic data access devices, no behavior. I could get syntactic sugar using jsobject so that I can write for example person1.address.city . But this is also not entirely correct. As a developer, I don't have an explicit python class API to consult to determine which fields to use, I can reference the marshmallow scheme, but this is very indirect.

Code generation

It would be quite easy to generate the OO code above from the definitions of a marshmallow scheme. I am surprised that there seems to be no such library. Perhaps code generation is considered very unwritten? Of course, this would be appropriate only for the definitions of data access style classes; adding non-general behavior will be strictly no-no.

For users of the code, they did not need to know that the approach with code genesis was used - everything would be there with an explicit API, with documents visible along with the rest of the code in readthedocs, etc.

Dynamic classes

Another approach would be dynamic classes derived from marshmallow definitions. Again, as far as I can tell that there is no such library (although the range of approaches to creating dynamic classes in python is impressive, maybe I missed some). It probably won’t buy you so much compared to jsobjects, but there may be some advantages - you could twist it with some explicit code with a certain behavior. The disadvantage of a dynamic approach is that explicit preference is given to implicit in the Python world.

What is most pythonic?

The absence of libraries here means that I either do not find something or do not look at it in a suitable pythonic manner. I'm glad to contribute something to pypi, but before adding another meta-OO library, I wanted to be sure that I did the due diligence here.

+5
source share

Source: https://habr.com/ru/post/1271076/


All Articles