This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients FlipperPA, Jason Stelzer, eric.smith, martin.panter, miserlou2, mivade, serhiy.storchaka
Date 2017-10-29.11:23:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1509276216.99.0.213398074469.issue31749@psf.upfronthosting.co.za>
In-reply-to
Content
Ken Kundert started a related discussion a while back on Python-ideas: <https://www.mail-archive.com/search?l=mid&q=20160830203427.GE2363@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²".
History
Date User Action Args
2017-10-29 11:23:37martin.pantersetrecipients: + martin.panter, eric.smith, serhiy.storchaka, mivade, miserlou2, FlipperPA, Jason Stelzer
2017-10-29 11:23:36martin.pantersetmessageid: <1509276216.99.0.213398074469.issue31749@psf.upfronthosting.co.za>
2017-10-29 11:23:36martin.panterlinkissue31749 messages
2017-10-29 11:23:35martin.pantercreate