Python model object validation

2024/9/16 23:10:19

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.

Answer

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.

https://en.xdnf.cn/q/72608.html

Related Q&A

Cassandra 1.2 inserting/updating a blob column type using Python and the cql library

IntroI have a blob column on a Cassandra 1.2 column family, the table is defined as follows:CREATE TABLE objects (id text,obj blob,PRIMARY KEY (id) );The problem:The problem is that when I…

Using python with subprocess Popen

I am struggling to use subprocesses with python. Here is my task:Start an api via the command line (this should be no different than running any argument on the command line) Verify my API has come …

ipdb, multiple threads and autoreloading programs causing ProgrammingError

I am using ipdb debugger to debug multithreaded web applications locally (Django, Plone). Often ipdb seems to get confused because of the autoreload which happens when I am on the debug prompt. The res…

How to turn off logging buffer to get logs in real time with python command line tool?

I have a command line tool which produces plenty of logs. I want these logs to be sent to stdout as soon as theyre made. Right now, the program finishes everything (which can take several minutes), and…

How to tell if process is responding in Python on Windows

I am writing a python script to keep a buggy program open and I need to figure out if the program is not respoding and close it on windows. I cant quite figure out how to do this.

How to load and use a pretained PyTorch InceptionV3 model to classify an image

I have the same problem as How can I load and use a PyTorch (.pth.tar) model which does not have an accepted answer or one I can figure out how to follow the advice given.Im new to PyTorch. I am trying…

Append list to pandas DataFrame as new row with index

Despite of the numerous stack overflow questions on appending data to a dataframe I could not really find an answer to the following. I am looking for a straight forward solution to append a list as la…

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that Ive captured wit…

Saving matplotlib subplot figure to image file

Im fairly new to matplotlib and am limping along. That said, I havent found an obvious answer to this question.I have a scatter plot I wanted colored by groups, and it looked like plotting via a loop w…

Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars

I have a 3 dimensional data set that I am trying to manipulate in the following way. data.shape = (643, 2890, 10) vector.shape = (643,)I would like numpy to see data as a 643 length 1-D array of 2890x1…