author | Tero Marttila <terom@fixme.fi> |
Sun, 14 Jun 2009 16:27:03 +0300 | |
changeset 110 | a5b30b423f79 |
parent 81 | 2b0b76cd8ba0 |
permissions | -rw-r--r-- |
56
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
""" |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
Miscellaneous formatting functions |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
""" |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
import datetime |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
def filesize (bytes) : |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
""" |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
Returns a human-readable string describing the given filesize |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
The returned string is either "x.xx MiB", "x.xx KiB", or "zzz Bytes", whereby x.xx is at least 0.1 |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
""" |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
kb, bytes = divmod(bytes, 1024) |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
mb, kb = divmod(kb, 1024) |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
if mb or kb > 1024 / 10.0 : |
81
2b0b76cd8ba0
misc. format.py formatting bugs
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
18 |
return "%.2f MiB" % (mb + kb / 1024 + bytes / 1024**2) |
56
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
elif kb or bytes > 1024 / 10.0 : |
81
2b0b76cd8ba0
misc. format.py formatting bugs
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
21 |
return "%.2f KiB" % (kb + bytes / 1024) |
56
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
else : |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
return "%d Bytes" % bytes |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
|
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
def filetime (ts) : |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
""" |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
Returns a human-readable string describing the given file timestamp |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
""" |
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
|
77
2a53c5ade434
misc. fixes, it runs now, but HTML output is corrupt (no flattening)
Tero Marttila <terom@fixme.fi>
parents:
56
diff
changeset
|
31 |
return datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S") |
56
80658a2eebf6
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |