GithubHelp home page GithubHelp logo

Confused about undictify HOT 2 CLOSED

dobiasd avatar dobiasd commented on August 27, 2024
Confused

from undictify.

Comments (2)

shawalli avatar shawalli commented on August 27, 2024 2

Thanks for the information! I'll give that a try.

from undictify.

Dobiasd avatar Dobiasd commented on August 27, 2024

Hi shawalli,

thanks for the feedback and sorry for the inconvenience.

In version 0.1.x it was:

my_thing = undictify.unpack_json(Thing, json_str)`

Now with version 0.2.x it is

my_thing = undictify.type_checked_apply(Thing, **json.loads(json_str))

I realized that unpacking JSON is just one specific use-case of a more general problem this library can solve, and I tried to reflect that.

Luckily the change needed on client side (see above) is quite small. And since the library has not yet reached version 1.x, I was hoping such a change would be acceptable. 🙂

If you still want a function named unpack_json, that you can use like my_thing = unpack_json(Thing, json_str) (and that has the optional convert_types flag), you can create it like this:

TypeT = TypeVar('TypeT')


def unpack_json(target_func: Callable[..., TypeT],
                object_repr: str, convert_types: bool = False) -> TypeT:
    if convert_types:
        return type_checked_apply_skip_convert(target_func,
                                               **json.loads(object_repr))
    return type_checked_apply_skip(target_func,
                                   **json.loads(object_repr))

I also just added it to examples/readme_examples.py.

However I'd like to encourage you to also try the new version, which is adding the @type_checked_call_skip decorator to your target class and unpacking you JSON like this:

my_thing = Thing(**json.loads(json_str))

from undictify.

Related Issues (14)

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.