Git Product home page Git Product logo

nvelope's Issues

Descriptors

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

Recursive definition

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.

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.