Martin Panter <vadmium...@gmail.com> added the comment:

Ken Kundert started a related discussion a while back on Python-ideas: 
<https://www.mail-archive.com/search?l=mid&q=20160830203427.ge2...@kundert.designers-guide.com>.
 This was about SI-prefixed units in general; not restricted to bytes. Also, 
the “timeit” module already does auto-scaling across nsec, usec, msec, and sec.

I think supporting decimal SI prefixes (for µs, mL, km, MW, etc) is more 
important than the binary versions (KiB, MiB, GiB). And units of 1,024,000 are 
definitely too niche.

I think a new format type “:h” may be the way forward. Perhaps it would add an 
SI prefix, and then the user could append their unit:

>>> f"{123901842:h}B"  # Six significant digits by default (like “:g”)
"123.902 MB"
>>> f"{123901842:.5h}B"  # Drop trailing zeros
"123.9 MB"
>>> f"{12:+6h}m"  # Sign and width options may be useful
"  +12 m"
>>> f"{12e99:h}m"  # Exponential notation for extreme values
"1.2e100 m"
>>> f"{12e99:H}m"  # Capitalize E, INF, etc (but not k for kilo-, etc)
"1.2E100 m"
>>> f"{123901:#.5h}m"  # Alternative form keeps trailing zeros
"123.90 km"
>>> f"{123:.2h}m"  # Precision < 3 may not be respected
"123 m"
>>> f"{123:#.2h}m"  # Maybe alternative form could respect the precision
"0.12 km"
>>> f"{123901842:.4h}B".replace(" ", "")  # Avoid the space
"123.9MB"
>>> f"{123901842:.4h}B".replace(" ", "&nbsp;")  # Alternative space
"123.9&nbsp;MB"
>>> f"{123901842:.4h}B".replace(".", ",")  # Alternative to decimal point
"123,9 MB"
>>> f"{12e-6:h}sec"  # Non-ASCII by default
"12 µsec"
>>> f"{12e-6:h}sec".replace("\N{MICRO SIGN}", "u")  # ASCII compatibility
"12 usec"

Squares and cubes may be a minor stumbling block: 0.001 m² is one thousand 
square millimetres, but f"{0.001:.3h}m²" would return "1 mm²".

----------
nosy: +martin.panter

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31749>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to