Python declarative

Burak Arslan burak.arslan at arskom.com.tr
Fri Jan 24 08:40:07 EST 2014


On 01/24/14 11:21, Frank Millman wrote:
> I store database metadata in the database itself. I have a table that 
> defines each table in the database, and I have a table that defines each 
> column. Column definitions include information such as data type, allow 
> null, allow amend, maximum length, etc. Some columns require that the value 
> is constrained to a subset of allowable values (e.g. 'title' must be one of 
> 'Mr', 'Mrs', etc.). I know that this can be handled by a 'check' constraint, 
> but I have added some additional features which can't be handled by that. So 
> I have a column in my column-definition table called 'choices', which 
> contains a JSON'd list of allowable choices. It is actually more complex 
> than that, but this will suffice for a simple example.


I wonder whether your use cases can be fully handled by Xml Schema
standard. It's quite robust and easy to use. You are already doing
validation at the app level so I don't think it'd be much of a problem
for you to adopt it.

e.g.

>>> from spyne.model import *
>>> class C(ComplexModel):
...     title = Unicode(values=['Mr', 'Mrs', 'Ms'])
...
>>> from lxml import etree
>>> from spyne.util.xml import get_validation_schema
>>> schema = get_validation_schema([C], 'some_ns')
>>> doc1 = etree.fromstring('<C xmlns="some_ns"><title>Mr</title></C>')
>>> print schema.validate(doc1)
True
>>> doc2 = etree.fromstring('<C xmlns="some_ns"><title>xyz</title></C>')
>>> print schema.validate(doc2)
False
>>> print schema.error_log
<string>:1:0:ERROR:SCHEMASV:SCHEMAV_CVC_ENUMERATION_VALID: Element
'{some_ns}title': [facet 'enumeration'] The value 'xyz' is not an
element of the set {'Mr', 'Mrs', 'Ms'}.
<string>:1:0:ERROR:SCHEMASV:SCHEMAV_CVC_DATATYPE_VALID_1_2_1: Element
'{some_ns}title': 'xyz' is not a valid value of the atomic type
'{some_ns}C_titleType'.
>>>


Also, if you need conversion between various serialization formats and
Python object hierarchies, I put together an example for you:
https://gist.github.com/plq/8596519

I hope these help.

Best,
Burak




More information about the Python-list mailing list