bitmath
bitmath copied to clipboard
RFE when using best prefix keep as bits?
Using the latest version of bitmath, im trying to convert a bit value into the best bit prefix. as seen below the output is always converted to bytes, I would like to keep the value in bits so best for this would be mebibits not mebibytes
>>> b = 130130.75
>>> m = Kib(b)
>>> m
Kib(130130.75)
>>> m.best_prefix()
MiB(15.885101318359375)
edited by tbielawa to use correct prefix units in the written form, replaced 'mega' with 'mebi' at end of description
@ajwillo Thanks for posting your question. You gave me something interesting to think about.
I have to admit, when I first saw this issue roll in I wasn't quite sure what to make of it, that's not your fault, that's mine. I thought to myself, "well, best_prefix
is technically working as advertised".
But then I thought about this issue again today and I realized that you've identified a very legitimate use-case that absolutely has value. I completely missed this in the initial design of best_prefix
. Thank you for filing this RFE.
To maintain current expected functionality I can't change the default behavior of best_prefix
, but what I think I can do instead is add an optional keyword argument to the method and function such that the user can specify that they would like to preserve the original base unit. That way you could ensure you get back a bit or byte based unit back, depending on the input.
Proof of concept, must implement following behavior:
>>> a = bitmath.Kib(130130.75)
>>> print(a)
130130.75 Kib
>>> b = a.best_prefix(preserve_base=True)
>>> print(b)
1127.080810547 Mib