Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
) 9 7026
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
Life is odd. I just came to the group with the specific purpose of
asking this exact question. Who are you? :)
Thanks!
~Sean
samuraisam <sa*********@gmail.comwrote:
> Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb'] [int(log)]
)
I have a couple of picky comments. The abbreviation for "bytes" should be
"B"; small "b" is bits by convention. All of the prefixes above "k" should
be capitalized: kB, MB and GB, not mb and gb.
The truly anal retentive would probably argue that you should be using kiB,
MiB, and GiB, since you are basing your scale on 1024 instead of 1000.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
Wait a sec...what if you send it a large amount of bytes? Say...
bigger than 2147483647. You'll get an OverflowError. I thought you
had my solution...
~Sean
On Jun 13, 12:48 am, half.ital...@gmail.com wrote:
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
Wait a sec...what if you send it a large amount of bytes? Say...
bigger than 2147483647. You'll get an OverflowError. I thought you
had my solution...
~Sean
On Jun 13, 12:48 am, half.ital...@gmail.com wrote:
On Jun 12, 8:47 pm, samuraisam <samuraib...@gmail.comwrote:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
Wait a sec...what if you send it a large amount of bytes? Say...
bigger than 2147483647. You'll get an OverflowError. I thought you
had my solution...
~Sean
I take it back.
samuraisam <sa*********@gmail.comwrites:
Quick file size formatting for all those seekers out there...
import math
def filesizeformat(bytes, precision=2):
"""Returns a humanized string for a given amount of bytes"""
bytes = int(bytes)
if bytes is 0:
return '0bytes'
log = math.floor(math.log(bytes, 1024))
return "%.*f%s" % (
precision,
bytes / math.pow(1024, log),
['bytes', 'kb', 'mb', 'gb', 'tb','pb', 'eb', 'zb', 'yb']
[int(log)]
)
The output doesn't match the calculation.
The symbol for "bit" is 'b'. The symbol for "byte" is 'B'. 'kb' is
'kilobit', i.e. 1000 bits. 'mb' is a "metre-bit", a combination of two
units. And so on. The SI units have definitions that are only muddied
by misusing them this way.
Especially since we now have units that actually do mean what we want
them to. The units that match the calculation you're using are 'KiB',
'MiB', 'GiB' and so on.
<URL:http://en.wikipedia.org/wiki/Binary_prefix>
Dividing by 1024 doesn't give 'kb', it gives 'KiB'. Likewise for the
rest of the units. So the list of units should be::
['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
--
\ Contentsofsignaturemaysettleduringshipping. |
`\ |
_o__) |
Ben Finney
Haha, you guys. Use it however you want. But trust me, if you put MiB
and GiB instead of the more-common mb and gb [MB and GB] in your
applications, your users will probably have a harder time
understanding what you mean.
Ben Finney wrote:
The symbol for "bit" is 'b'. The symbol for "byte" is 'B'. 'kb' is
'kilobit', i.e. 1000 bits. 'mb' is a "metre-bit", a combination of two
units. And so on. The SI units have definitions that are only muddied
by misusing them this way.
I have to disagree: 'mb' should stand for "milli-bit" :)
which could be considered as the probability of a bit
.... this might be useful for quantum computing.
Avell Diroll <av*********@yahoo.frwrites:
I have to disagree: 'mb' should stand for "milli-bit" :)
Yes, you're right. My "metre-bit" was wrong.
--
\ "Whenever you read a good book, it's like the author is right |
`\ there, in the room talking to you, which is why I don't like to |
_o__) read good books." -- Jack Handey |
Ben Finney This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mego |
last post by:
Sto usando vb6 per creare un file .cub a a partire da una query:
LOCATION= d:\Test.cub;
SOURCE_DSN = "Provider=MSOLAP.2;Data Source=data-center;Initial
Catalog=FoodMart 2000";
CREATECUBE=CREATE...
|
by: Kristian Nybo |
last post by:
Hi,
I'm writing a simple image file exporter as part of a school project. To
implement my image format of choice I need to work with big-endian
bytes, where 'byte' of course means '8 bits', not...
|
by: Alexander Hunziker |
last post by:
Hello group,
I have a program that reads data from a binary file. I know that at some
known position in the file there are 12 4 bytes long floating point
numbers. Here's how I read them now:
...
|
by: Dave |
last post by:
I am trying to call VerQueryValue from a C# program. VerQueryValue takes as
one of its parameters a pointer to a pointer to an array of bytes, which it
uses to return a pointer to the required...
|
by: PiotrKolodziej |
last post by:
Hi
I have long variable containing number of stored bytes.
I want to display Bytes , KB, MB, GB depending of the size of this variable
as a string.
Does framework provide any class for such a...
|
by: Wes |
last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++.
The idea of the program is to open one file as input, read bytes from
it, do some bitwise operations on the bytes, and then...
|
by: cyberco |
last post by:
I'm using web.py to send an image to the client. This works
(shortened):
print open(path, "rb").read()
but this doesn't:
img = Image.open(path)
img.thumbnail((10,10))
print img.getdata()
|
by: levimc |
last post by:
I know that that topic may be old to you but I looked at other more-
than-two-year-old topics
related to mine. However, I didn't find them working for my project
at all because its errors return...
|
by: RYKLOU |
last post by:
I am kinda new to php, but i do know what i am doing kinda, but i came across this error when i am trying to upload a file to my website.
Fatal error: Allowed memory size of 8388608 bytes...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |