Git Product home page Git Product logo

Comments (4)

monomonedula avatar monomonedula commented on May 30, 2024

@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.

donpatrice avatar donpatrice commented on May 30, 2024

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.

monomonedula avatar monomonedula commented on May 30, 2024

@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.

donpatrice avatar donpatrice commented on May 30, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.