Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python model object validation

Tags:

python

I'm writing an interface to be used by two applications. This interface should use some DoSomethingRequest and DoSomethingResponse classes to do the communication.

Is there any library that does some model validation, for example like Django's Model?

I basically want to be able to say something like:
Object A must have a "text" property of type str(), a "number" property of type int(), an "items" property of type list(). In a DRY way.

I'm looking for something like the following, or better:

class MyEmbeddedModelClass(EmbeddedModel):
    text = TextField(required = True)

class MyModel(Model):
    text = TextField(required = True)
    number = IntField(default = 0)
    items = ListField(EmbeddedModel)


a = MyModel()
a.text = "aaaa"
a.number = 1
a.items = [
    MyEmbeddedModelClass("bbbb"),
    MyEmbeddedModelClass("cccc"),
    MyEmbeddedModelClass("dddd")
]
a.validate()

I know I can write my own, but I'd rather use a library if available, I'm a bit new to this.

like image 560
Prody Avatar asked Oct 13 '11 13:10

Prody


1 Answers

If you want to enforce interfaces, or use design-by-contract, then you probably want the zope.interface library. Despite the name, which reflects its origins in Zope, it's not actually tied to that framework at all and is quite usable outside.

like image 185
Daniel Roseman Avatar answered Oct 10 '22 13:10

Daniel Roseman