marrow / schema Goto Github PK
View Code? Open in Web Editor NEWA generic declarative schema system.
License: MIT License
A generic declarative schema system.
License: MIT License
As per this gist, with notable use on forms involving password entry and confirmation.
Overridden attributes currently inherit the most-recent attribute's __sequence__
; this may not always be desirable, for example, when redefining a default value in a subclass.
For inclusion in the project package as marrow.schema.struct
.
Use a combination of the built-in validators and typecasters to show off class-based composition of fields.
Trivial example:
from marrow.mongo import Document
from marrow.mongo.field import String
class Sample(Document):
name:str = String()
This should stand out, glaringly, as a DRY failure. We are assigning a data descriptor object explicitly to handle strings, why must we declare, then, that instances contain strings within this attribute? The trivial example hides the scope of the majesty of this problem. A better example:
from bson import ObjectId as OID
from datetime import datetime, timedelta
from collections.abc import MutableMapping
from marrow.mongo import Document
from marrow.mongo.field import ObjectId
class Sample(Document):
name:Union[OID,datetime,timedelta,MutableMapping,str,bytes] = ObjectId()
Ruh-roh! Requiring developers using Marrow Mongo to declare such a Union on every ObjectId field (even if abbreviated, e.g. through assignment of that Union to a variable) is a very bad idea. The descriptor itself knows what values it can handle, and what it will hand out. (Additionally, until lazy evaluation of annotations is more widespread, it'd be nice to not force those extra imports on people.)
Introduced in c2a4554 and patched in 98a0f5d to permit preservation of explicit annotations, remaining are tests and documentation.
The Attribute
subclasses implementing fields may then declare an annotation
attribute, which will be hoisted up into the containing class' __annotations__
.
To assist with the generic implementation of ORM (or widget) style data access attributes some form of automatic to/from typecasting will be needed.
To reduce code duplication these casters should be external to the attribute classes themselves and merely referenced as a class attribute on the same.
Basically transfer that bit from marrow.tags
and expand to cover similar transformations as MongoEngine.
I.e. allow child Attribute instances to be notified that they have been included in a Container, and allow the Container to perform some additional work after class construction.
From #python:
<ssbr_at_work>
IDK. These days when I look at schemas, the core question I want to have answered is, "how do I keep forward and backward compatibility?"
<ssbr_at_work>
File formats need to answer that question, universally, don't they? Why don't the schema formats help?
<ssbr_at_work>
GothAlice: IMO you should borrow extensively from cap'n proto / protocol buffers
<ssbr_at_work>
GothAlice: So you write a serialization protocol that has a lot of freedom for forward/backward compatibility reasons, and then for a specific version you write a validator that ignores things it doesn't know about.
<ssbr_at_work>
GothAlice: so IMO the only thing marrow should remember from the lessons learned by protobuf/cap'n proto is not to reject unknown values
<sproingie>
ssbr_at_work: i think it's that protocol compatibility is much more complicated than locking it down to a particular version number or required fields, so it punts and leaves it up to the application layer to decide unless you avail yourself of explicitly marking things required
<ssbr_at_work>
sproingie: marking things as required is an antipattern in protocol buffer land, to the extent that it's forbidden in cap'n proto
<ssbr_at_work>
GothAlice: protocol buffers have a few ways they deal with unknown values that might be interesting to you as alternate options (or defaults)
<ssbr_at_work>
GothAlice: in particular, unknown fields are totally ignored, unknown values for known fields are read as a "default" known value
Example problem case:
class Foo(Container):
type = Attribute(default=str)
class Bar(Foo):
type = unicode
Bar.__attributes__ == Foo.__attributes__
a = Bar(int)
a.type == int
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.