Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csppfetch
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cspp_geo
csppfetch
Commits
cbc4ffa6
Commit
cbc4ffa6
authored
3 years ago
by
Alan De Smet
Browse files
Options
Downloads
Patches
Plain Diff
Better byte count formatting
parent
bb5b5645
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
csppfetch/__init__.py
+30
-6
30 additions, 6 deletions
csppfetch/__init__.py
with
30 additions
and
6 deletions
csppfetch/__init__.py
+
30
−
6
View file @
cbc4ffa6
...
...
@@ -96,21 +96,45 @@ class FileSet:
def
human_bytes
(
total_bytes
):
"""
return nicely formatted string for a count of bytes
The result is rounded to the displayed precision,
so (1024**2)-512 is
"
1.00 MiB
"
but (1024**2)-513 is
"
1023 KiB
"
>>>
human_bytes
(
0
)
'
0 B
'
>>>
human_bytes
(
3836163
)
'
3.66 MiB
'
>>>
human_bytes
((
1024
**
2
)
-
512
)
'
1.00 MiB
'
>>>
human_bytes
((
1024
**
2
)
-
513
)
'
1023 KiB
'
>>>
human_bytes
(
7763153400
)
'
7.23 GiB
'
"""
step_size
=
1024
# Yes, it's "Ki", not "ki".
steps
=
"
Ki Mi Gi Ti Pi Ei
"
.
split
()
# If you prefer the non-binary SI prefixes
# step_size = 1000
#
steps =
[""]+list(
"kMGTPE"
)
# steps = "kMGTPE"
if
total_bytes
<
step_size
:
return
f
"
{
total_bytes
}
B
"
value
=
total_bytes
step
=
-
1
while
value
>=
step_size
:
while
round
(
value
,
0
)
>=
step_size
:
step
+=
1
value
/=
step_size
value_str
=
f
"
{
value
:
f
}
"
[:
4
]
if
value_str
[
-
1
]
==
"
.
"
:
value_str
=
value_str
[:
-
1
]
if
round
(
value
,
2
)
<
10
:
value
=
round
(
value
,
2
)
value_str
=
f
"
{
value
:
.
2
f
}
"
elif
round
(
value
,
1
)
<
100
:
value
=
round
(
value
,
1
)
value_str
=
f
"
{
value
:
.
1
f
}
"
else
:
value
=
round
(
value
,
0
)
value_str
=
f
"
{
value
:
.
0
f
}
"
return
f
"
{
value_str
}
{
steps
[
step
]
}
B
"
...
...
@@ -145,7 +169,7 @@ class DownloadStatistics:
self
.
finish
()
speed
=
self
.
downloaded_size
/
(
self
.
total_time
().
total_seconds
())
if
speed
<
100
:
speed_str
=
"
{speed} B
"
speed_str
=
f
"
{
speed
}
B
"
else
:
speed_str
=
human_bytes
(
speed
)
ret
=
[
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment