monomonedula / nvelope Goto Github PK
View Code? Open in Web Editor NEWDefine your JSON schema as Python dataclasses
License: MIT License
Define your JSON schema as Python dataclasses
License: MIT License
From readme I see
@dataclass
class User(Obj):
_conversion = {
"id": int_conv,
"language_code": string_conv,
"username": string_conv,
}
id: int
language_code: str
username: str
Which makes me curious on why to define an argument named _conversion
, is this a design decision for some reason?
What about implementing that using the descriptor
protocol?
@dataclass
class User(Obj):
id: int = IntField()
language_code: str = StringField()
username: str = StringField()
And having all *Field
to be implementation of descriptors such as:
class IntField:
def __set_name__(self, owner, name):
self.public_name = name
self.private_name = '_' + name
def __get__(self, obj, objtype=None):
value = getattr(obj, self.private_name)
logging.info('Accessing %r giving %r', self.public_name, value)
return int(value) # conversion here on reading
def __set__(self, obj, value):
logging.info('Updating %r to %r', self.public_name, value)
setattr(obj, self.private_name, int(value)) # conversion here on writing
Is there a way for recursive definitions? So for example let's say we want to consider sections in an article. A section have a title and maybe some subsections. The corresponding json could look like:
{
"title": "This is a really nice title",
"sections": [
{
"title": "Oh this title is even nicer"
"sections: [
{
"title: "Not so nice title, no subsections"
}
]
},
{
"title": "Section without subsection"
}
]
So we could start with:
@dataclass
class Section(Obj):
_conversion = {"title": string_conv}
title: str
But obviously the maybe subsections are missing. Is there a way to model that? Thanks.
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.