python-humanfriendly icon indicating copy to clipboard operation
python-humanfriendly copied to clipboard

Force conversion unit in format_size

Open sebastian-luna-valero opened this issue 5 years ago • 5 comments

Hi,

We are interested in adding an optional flag to format_size() so you can force the conversion unit.

Here is the desired behaviour:

python3 -c "import humanfriendly; print(humanfriendly.format_size(1000 ** 3 * 4))"
4 GB

python3 -c "import humanfriendly; print(humanfriendly.format_size(1000 ** 3 * 4, force_unit='bytes'))"
4000000000 bytes

python3 -c "import humanfriendly; print(humanfriendly.format_size(1000 ** 3 * 4, force_unit='KB'))"
4000000 KB

python3 -c "import humanfriendly; print(humanfriendly.format_size(1000 ** 3 * 4, force_unit='MB'))"
4000 MB

python3 -c "import humanfriendly; print(humanfriendly.format_size(1000 ** 3 * 4, force_unit='GB'))"
4 GB

python3 -c "import humanfriendly; print(humanfriendly.format_size(1000 ** 3 * 4, force_unit='TB'))"
0 TB

Best regards, Sebastian

sebastian-luna-valero avatar Sep 03 '20 08:09 sebastian-luna-valero

Coverage Status

Coverage increased (+0.003%) to 93.417% when pulling 57ba6f73f172ef3087a680c4dea6bf518bdb6053 on sebastian-luna-valero:force-units into 05d02d4f6ef317edf97aca679411ec6514685243 on xolox:master.

coveralls avatar Sep 03 '20 09:09 coveralls

Hi @xolox

All tests are passing except for coveralls which I don't know how to fix.

Please have a look and let me know your thoughts.

Many thanks, Sebastian

sebastian-luna-valero avatar Sep 03 '20 09:09 sebastian-luna-valero

Hi again,

Alright so adding tests for the new code solves the coveralls issue.

Please have a look and let me know your thoughts.

Best regards, Sebastian

sebastian-luna-valero avatar Sep 03 '20 11:09 sebastian-luna-valero

any news on this? its exactly what im looking for

yodog avatar May 11 '21 17:05 yodog

Hi,

Probably my changes are a bit too much for this package and that's why it hasn't been merge.

I have just taken the relevant code out and copied it to my own script. @yodog I recommend you to do the same.

If you think it is interesting, I might upload it to my own git repo and share it with you.

Best regards, Sebastian

sebastian-luna-valero avatar May 14 '21 10:05 sebastian-luna-valero