Last active
September 23, 2019 21:30
-
-
Save carlos-jenkins/1f1cea0e156678758958391b47458e02 to your computer and use it in GitHub Desktop.
Auto-converter of arbitrary values to International System of Units (SI) human readable prefix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Auto-converter of arbitrary values to International System of Units (SI) | |
* human readable prefix. | |
* | |
* Typical usage: | |
* | |
* const txpackets = 2450 | |
* const txbytes = txpackets * 256 | |
* const txtime = 2.0 | |
* | |
* const unit1 = biPrefix(txbytes, 'B') | |
* const unit2 = siPrefix((txbytes * 8) / txtime, 'bps') | |
* | |
* console.log( | |
* `${txpackets} packets for a total ` + | |
* `${unit1.value.toFixed(2)}${unit1.prefix}${unit1.unit} bytes transmitted in ` + | |
* `${txtime} seconds.\nThroughput of ` + | |
* `${unit2.value.toFixed(2)}${unit2.prefix}${unit2.unit}.` | |
* ) | |
* | |
* 2450 packets for a total 612.50KiB bytes transmitted in 2 seconds. | |
* Throughput of 2.51Mbps. | |
*/ | |
function unitPrefix(value, unit, multiplier) { | |
const prefixes = [ | |
'', | |
'K', // kilo / kibi | |
'M', // mega / mebi | |
'G', // giga / gibi | |
'T', // tera / tebi | |
'P', // peta / pebi | |
'E', // exa / exbi | |
'Z', // zetta / zebi | |
'Y', // yotta / yobi | |
] | |
const options = prefixes.length - 1 | |
let n = 0 | |
while (value >= multiplier && n < options) { | |
value /= multiplier | |
n += 1 | |
} | |
const prefix = prefixes[n] | |
return { | |
value, unit, prefix | |
} | |
} | |
function siPrefix(value, unit) { | |
return unitPrefix(value, unit, 10 ** 3) | |
} | |
function biPrefix(value, unit) { | |
const u = unitPrefix(value, unit, 2 ** 10) | |
return { ...u, prefix: `${u.prefix}i` } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Transform an arbitrary value to its closest next multiple. | |
Both the International System of Units Prefixes (multiples of 1000) and | |
Binary Prefixes (multiples of 1024) are supported. | |
Typical usage: | |
:: | |
>>> txpackets = 2450 | |
>>> txbytes = txpackets * 256 | |
>>> txtime = 2.0 | |
>>> print( | |
... '{0} packets for a total ' | |
... '{1.value:.2f}{1.prefix}{1.unit} bytes transmitted in ' | |
... '{2:.2f} seconds.\\nThroughput of ' | |
... '{3.value:.2f}{3.prefix}{3.unit}.'.format( | |
... txpackets, | |
... bi_prefix(txbytes, 'B'), | |
... txtime, | |
... si_prefix((txbytes * 8) / txtime, 'bps'), | |
... ) | |
... ) | |
2450 packets for a total 612.50KiB bytes transmitted in 2.00 seconds. | |
Throughput of 2.51Mbps. | |
""" | |
from collections import namedtuple | |
UnitPrefix = namedtuple('UnitPrefix', 'value,unit,prefix') | |
def unit_prefix(value, unit, multiplier): | |
""" | |
Transform a value to its closest next multiple. | |
Both the International System of Units Prefixes (multiples of 1000) and | |
Binary Prefixes (multiples of 1024) are supported. | |
:param float value: Value to transform. | |
:param str unit: Name of the unit. | |
:param int multiplier: Unit multiplier (1000, 1024) | |
:return: Named tuple with transformed value, unit and prefix. | |
:rtype: namedtuple | |
""" | |
prefixes = [ | |
'', | |
'K', # kilo / kibi | |
'M', # mega / mebi | |
'G', # giga / gibi | |
'T', # tera / tebi | |
'P', # peta / pebi | |
'E', # exa / exbi | |
'Z', # zetta / zebi | |
'Y', # yotta / yobi | |
] | |
options = len(prefixes) - 1 | |
n = 0 | |
while value >= multiplier and n < options: | |
value /= multiplier | |
n += 1 | |
prefix = prefixes[n] | |
if multiplier == 2 ** 10: | |
prefix = '{}i'.format(prefix) | |
return UnitPrefix( | |
value=value, | |
unit=unit, | |
prefix=prefix, | |
) | |
def si_prefix(value, unit): | |
""" | |
Transform a value to its closest next International System of Units | |
multiple (multiples of 1000). | |
:param float value: Value to transform. | |
:param str unit: Name of the unit. | |
:return: Named tuple with transformed value, unit and prefix. | |
:rtype: namedtuple | |
""" | |
return unit_prefix(value, unit, 10 ** 3) | |
def bi_prefix(value, unit): | |
""" | |
Transform a value to its closest next Binary Prefix multiple | |
(multiples of 1024). | |
:param float value: Value to transform. | |
:param str unit: Name of the unit. | |
:return: Named tuple with transformed value, unit and prefix. | |
:rtype: namedtuple | |
""" | |
return unit_prefix(value, unit, 2 ** 10) | |
__all__ = [ | |
'si_prefix', | |
'bi_prefix', | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment