rich
rich copied to clipboard
[REQUEST] Control float precision for nested objects in pretty print?
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 }