jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

populate - arg throw_on_unknown_keyword #125

Open user706 opened 5 years ago

user706 commented 5 years ago

Hi,

I would like the __init__ and populate methods ref, to have the following signature:

def __init__(self,  throw_on_unknown_keyword = False, **kwargs):
    # ...

def populate(self, throw_on_unknown_keyword = False, **values):
    # ...
            if structure_name in values:
                field.__set__(self, values.pop(structure_name))
            elif throw_on_unknown_keyword and structure_name != 'throw_on_unknown_keyword' :
                throw # ...
        for name, _, field in fields:
            if name in values:
                field.__set__(self, values.pop(name))
            elif throw_on_unknown_keyword and name != 'throw_on_unknown_keyword':
                throw # ...

# warning: I have not checked the details!

Reason: I wrap the Model inside a class:


class A:
        class ModelA(models.Base):
        a = fields.DateTimeField(required=True)
        b = fields.FloatField(required=True)
        c = fields.FloatField(required=True)

    def __init__(self, **kwargs):
        self.model = self.ModelA(throw_on_unknown_keyword = True, **kwargs)

    # #Instead of
    #def __init__(self, a, b, c):
    #   self.model = self.ModelA(a = a, b = b, c = c)

With throw_on_unknown_keyword = True, I keep the constructor of A and the model synchronized, and get an exception if they are not synchronized.

Is that feasible?