Skip to content

Instantly share code, notes, and snippets.

@tbielawa
Created September 29, 2014 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbielawa/f949de30012a5657000f to your computer and use it in GitHub Desktop.
Save tbielawa/f949de30012a5657000f to your computer and use it in GitHub Desktop.
bitmath-1.0.8-1: Examples
>>> import bitmath
>>> a_big_kib = bitmath.KiB(102400)
>>> print "Default:", a_big_kib, "| Best-prefix:", a_big_kib.best_prefix()
Default: 102400.0 KiB | Best-prefix: 100.0 MiB
>>> print (a_big_kib/3.0).format("{value:.3f}/{unit}")
34133.333/KiB
>>> print a_big_kib.bits, a_big_kib.system
838860800.0 NIST
>>> with bitmath.format(fmt_str="{value:.3f}/{unit}"):
... print bitmath.kB(12345.67890)
...
12345.679/kB
>>> with bitmath.format(plural=True):
... print bitmath.kB(12345.67890)
...
12345.6789 kBs
>>> print bitmath.KiB(1) == bitmath.KiB(1).to_Byte() == bitmath.Byte(1024).KiB
True
>>> print bitmath.getsize('.git/config')
672.0 Byte
>>> with bitmath.format(plural=True):
... for path, bm in bitmath.listdir('tests', relpath=True, filter='test_b*.py'):
... print "{path} is {value} {unit}".format(path=path, value=bm.value, unit=bm.unit)
...
tests/test_basic_math.py is 7349.0 Bytes
tests/test_bitwise_operations.py is 2701.0 Bytes
tests/test_best_prefix_BASE.py is 2155.0 Bytes
tests/test_best_prefix_NIST.py is 5562.0 Bytes
tests/test_best_prefix_SI.py is 5472.0 Bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment