Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

support decimal and fraction #297

Open
ltworf opened this issue Apr 11, 2022 · 4 comments
Open

support decimal and fraction #297

ltworf opened this issue Apr 11, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@ltworf
Copy link
Owner

ltworf commented Apr 11, 2022

No description provided.

@ltworf ltworf added the enhancement New feature or request label Apr 11, 2022
@uyar
Copy link

uyar commented Nov 2, 2023

This would be very nice. I'm in the process of redesigning the https://github.com/cinemagoer/cinemagoer (former imdbpy) code base (in https://github.com/cinemagoer/cinemagoerng) and I'm using typedload for deserialization. Great library, thank you very much! Anyway, movie ratings are given as floats but I believe decimal would be the proper type for that.

@ltworf
Copy link
Owner Author

ltworf commented Nov 2, 2023

It's really really easy to do, but I haven't done it because it would mean importing them automatically even when they aren't used (most of the times, I guess).

Unless I think of a way of doing a conditional import, I don't think it's a good idea to just blindly import the module.

I think float is fine, I don't think you will need excessive precision for ratings.

Anyway you can pass strconstructed={decimal.Decimal} to support it.

This will override the default strconstructed set, so if you want to use also the types that are in there by default you must create the loader/dumper object and do like l.strconstructed.add(decimal.Decimal).

@uyar
Copy link

uyar commented Nov 3, 2023

OK, I've made a few tries and made it all work as you have described and as expected according to the documentation for Python's decimal module. I wish generating decimals from floats had a nicer syntax in Python without having to use strings or rounding, but I can see why things are the way they are. Thanks!

@ltworf
Copy link
Owner Author

ltworf commented Nov 4, 2023

Unfortunately non-integer numbers and computers don't get so well together. I still think for your case you don't need that much precision and a float would be fine.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants