Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQUEST] Control float precision for nested objects in pretty print? #2370

Closed
NekoApocalypse opened this issue Jun 29, 2022 · 3 comments
Closed

Comments

@NekoApocalypse
Copy link

NekoApocalypse commented Jun 29, 2022

I have a program that collects some statistics from a dataset. It has two outputs:

  • A human readable preview printed to the terminal. I wish all float to be printed with precision 4. i.e. 3.141592653 -> 3.1416
  • A json save file for downstream applications. The numbers should be in their original value.

Currently I am generating a "preview" version of the result with rounded floats just for pretty print, along side a "high-precision" version for json storage.

I wonder is it possible that we control the precision of floats with a parameter?

Example:

data = {
  "player": "John",
  "games_played": 255,
  "win_rate": 166 / 255, # should be 0.65098039215
}
orignal = json.dumps(data) # output with full precision
rich.pprint(data) # pretty print with full precision
rich.pprint(data, presicion=2) # pretty print with precision = 2

Should print:

{ "player": "John", "games_played": 255, "win_rate": 0.65098039215 }
{ "player": "John", "games_played": 255, "win_rate": 0.6510 }
@royatt
Copy link

royatt commented Mar 17, 2023

Adding a parameter that is specific to ints (or any parameter honestly) in the pprint function seems rather difficult.
Perhaps you can go in the route of enquiring a specific console object, IMO adding a precision parameter to the console constructor isn't that bad, but could also use a custom formatter or what not.

@willmcgugan
Copy link
Collaborator

I think that is beyond the remit of Rich. Not something that should go in the core lib.

Copy link

github-actions bot commented Jul 2, 2024

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants