Comments (4)
@donpatrice The only way to do it is some monkey-patching.
For python 3.8 it would be:
from __future__ import annotations
from dataclasses import dataclass
from typing import List
from nvelope import Obj, string_conv, ListConversion, CompoundConv
@dataclass
class Section(Obj):
_conversion = {"title": string_conv}
title: str
sections: MaybeMissing[List[Section]]
Section._conversion["sections"] = ListConversion(CompoundConv(Section))
You may want to use the latest version 0.3.1 with better support for string type annotations (namely, MaybeMissing
logic).
from nvelope.
Thank you so much for the quick reply and the nice work. There is one little question left. If I now have an JSON with an array of sections I would do something like:
class Sections(Arr):
conversion = Compound(Section)
sections = Sections.from_json(json_file)
right?
And then I can do:
foo = Section.from_json(
{
"title": "bar",
"sections": Sections.from_json(json_file).as_json()
}
)
Is that the way how I do it properly? Or can I somehow "directly" assign the list of sections to foo
?
Thanks a lot!
from nvelope.
@donpatrice if I get you right, you might wanna do something like this:
foo = Section.from_json({"title": "bar"})
foo.sections = Jst(list(sections)) # use Jst wrapper since we defined sections as MaybeMissing attribute
Sections
you defined here is neither an equivalent of List[Sections]
nor is it an equivalent of MaybeMissing[List[Sections]]
, so your second block of code is not the way to go.
However, it seems to me that you don't need to define the Sections
class to do what you are describing here. It can be done much simpler this way:
foo = Section.from_json(
{
"title": "bar",
"sections": json_file
}
)
or
foo = Section(
title="bar",
sections=Jst([Section.from_json(record) for record in json_file])
)
Note: in the first code block you used wrong class. It must be CompoundConv
instead of Compound
from nvelope.
Thanks a lot @monomonedula, both for your help and the great work!
Note: in the first code block you used wrong class. It must be CompoundConv instead of Compound
Yes you are right, that was a typo.
from nvelope.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nvelope.