New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix units in fs.filesize
#85
Conversation
I was using https://en.wikipedia.org/wiki/File_size as a guide, which appears to contradict with that. It's all very confusing, tbh. Think you're right re ValueError -> TypeError. |
I tried to read https://en.wikipedia.org/wiki/Binary_prefix and it did not make more sense: If you check the IEC, it enforces the use of But it seems like |
The original implementation matches this django filter. I'm thinking that perhaps filesize.py doesn't belong here. I use it in Are you using it anywhere? Tempted to just remove if from the lib to sidestep this thorny issue. |
I wouldn't remove it, since this is the kind of code you keep on rewriting and rewriting again everytime you need it, it's good that it's available in the library somewhere IMO. But still, even the Django doc states that they are not using the right prefixes, so... |
Thing is I'd rather not be descriptive regarding these units. There will always be developers who will want to do it the 'wrong' way to match some pre-existing convention perhaps. Maybe the best approach would be to provide a way of using any unit system you like, but default to the more technically correct way. BTW do you still favour the units in the PR or are you confused as I am? Django likes power of two for file sizes, OSX likes powers of 10. |
Then let's name the functions after what they do, and not what standard they try to conform to: we rename >>> fs.filesize.binary(4096)
4.0 KiB
>>> fs.filesize.binary(4096, si=False):
4.0 KB
>>> fs.filesize.decimal(4096)
4.1 kB Here: |
That sounds like a plan. How would you feel about leaving in |
|
fs/filesize.py
Outdated
:rtype str: | ||
|
||
""" | ||
prefixes = ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB') \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this as an alternative to line continuation...
prefixes = (
('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
if si else
('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
)
Looks great. Just needs "binary" in all. Happy to merge if you are? |
I realize, if we are to keep traditionnal, then maybe we don't need the
(this looks like an endless debate 😄) |
The great thing about standards is they are so many to chose from... Think you're right about the |
I squashed everything into a single commit, merge shoud be clear now. |
Great! I hope never to have to think about file size units again :) |
According to Wikipedia, the binary filesize units should be
KiB
,MiB
, ..., and the decimal units should bekB
,MB
, ...Previously,
traditional
would use the SI prefixes, anddecimal
would display bits instead of bytes.Also, I'm not sure about this, but once again functions should probably be raising
TypeError
when given an argument that cannot be coerced to a numerical value.