Commit 05b3c47c authored by Luke Macken's avatar Luke Macken

Humanize bytes

parent 5769d6a7
...@@ -33,7 +33,7 @@ from meliae import loader ...@@ -33,7 +33,7 @@ from meliae import loader
from gi.repository import GLib, GObject, Pango, Gtk, WebKit from gi.repository import GLib, GObject, Pango, Gtk, WebKit
import pyrasite import pyrasite
from pyrasite.utils import setup_logger, run from pyrasite.utils import setup_logger, run, humanize_bytes
log = logging.getLogger('pyrasite') log = logging.getLogger('pyrasite')
...@@ -306,12 +306,12 @@ class PyrasiteWindow(Gtk.Window): ...@@ -306,12 +306,12 @@ class PyrasiteWindow(Gtk.Window):
cpu_user = cputimes.user, cpu_user = cputimes.user,
cpu_sys = cputimes.system, cpu_sys = cputimes.system,
mem = p.get_memory_percent(), mem = p.get_memory_percent(),
mem_rss = meminfo.rss, mem_rss = humanize_bytes(meminfo.rss),
mem_vms = meminfo.vms, mem_vms = humanize_bytes(meminfo.vms),
read_count = io.read_count, read_count = io.read_count,
read_bytes = io.read_bytes, read_bytes = humanize_bytes(io.read_bytes),
write_count = io.write_count, write_count = io.write_count,
write_bytes = io.write_bytes, write_bytes = humanize_bytes(io.write_bytes),
) )
open_files = p.get_open_files() open_files = p.get_open_files()
......
# http://code.activestate.com/recipes/577081-humanized-representation-of-a-number-of-bytes/
from __future__ import division
def humanize_bytes(bytes, precision=1):
"""Return a humanized string representation of a number of bytes."""
abbrevs = (
(1<<50L, 'PB'),
(1<<40L, 'TB'),
(1<<30L, 'GB'),
(1<<20L, 'MB'),
(1<<10L, 'kB'),
(1, 'bytes')
)
if bytes == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytes >= factor:
break
return '%.*f %s' % (precision, bytes / factor, suffix)
# Some useful functions based on code from Will Maier's 'ideal Python script' # Some useful functions based on code from Will Maier's 'ideal Python script'
# https://github.com/wcmaier/python-script # https://github.com/wcmaier/python-script
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment