bitmath


Namebitmath JSON
Version 1.3.3.1 PyPI version JSON
download
home_pagehttps://github.com/tbielawa/bitmath
SummaryPythonic module for representing and manipulating file sizes with different prefix notations (file size unit conversion)
upload_time2018-08-23 16:23:53
maintainer
docs_urlNone
authorTim Bielawa
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements python-coveralls coverage mock nose pyflakes pycodestyle progressbar231 click
Travis-CI
coveralls test coverage
            .. image:: https://api.travis-ci.org/tbielawa/bitmath.png
   :target: https://travis-ci.org/tbielawa/bitmath/
   :align: right
   :height: 19
   :width: 77

.. image:: https://coveralls.io/repos/tbielawa/bitmath/badge.png?branch=master
   :target: https://coveralls.io/r/tbielawa/bitmath?branch=master
   :align: right
   :height: 19
   :width: 77

.. image:: https://readthedocs.org/projects/bitmath/badge/?version=latest
   :target: http://bitmath.rtfd.org/
   :align: right
   :height: 19
   :width: 77


bitmath
=======

`bitmath <http://bitmath.readthedocs.org/en/latest/>`_ simplifies many
facets of interacting with file sizes in various units. Originally
focusing on file size unit conversion, functionality now includes:

* Converting between **SI** and **NIST** prefix units (``kB`` to ``GiB``)
* Converting between units of the same type (SI to SI, or NIST to NIST)
* Automatic human-readable prefix selection (like in `hurry.filesize <https://pypi.python.org/pypi/hurry.filesize>`_)
* Basic arithmetic operations (subtracting 42KiB from 50GiB)
* Rich comparison operations (``1024 Bytes == 1KiB``)
* bitwise operations (``<<``, ``>>``, ``&``, ``|``, ``^``)
* Reading a device's storage capacity (Linux/OS X support only)
* `argparse <https://docs.python.org/2/library/argparse.html>`_
  integration as a custom type
* `progressbar <https://code.google.com/p/python-progressbar/>`_
  integration as a better file transfer speed widget
* String parsing
* Sorting


In addition to the conversion and math operations, `bitmath` provides
human readable representations of values which are suitable for use in
interactive shells as well as larger scripts and applications. The
format produced for these representations is customizable via the
functionality included in stdlibs `string.format
<https://docs.python.org/2/library/string.html>`_.

In discussion we will refer to the NIST units primarily. I.e., instead
of "megabyte" we will refer to "mebibyte". The former is ``10^3 =
1,000,000`` bytes, whereas the second is ``2^20 = 1,048,576``
bytes. When you see file sizes or transfer rates in your web browser,
most of the time what you're really seeing are the base-2 sizes/rates.

**Don't Forget!** The source for bitmath `is available on GitHub
<https://github.com/tbielawa/bitmath>`_.

And did we mention there's almost 200 unittests? `Check them out for
yourself <https://github.com/tbielawa/bitmath/tree/master/tests>`_.


Installation
============

The easiest way to install bitmath is via ``dnf`` (or ``yum``) if
you're on a Fedora/RHEL based distribution. bitmath is available in
the main Fedora repositories, as well as the `EPEL6
<http://download.fedoraproject.org/pub/epel/6/i386/repoview/epel-release.html>`_
and `EPEL7
<http://download.fedoraproject.org/pub/epel/7/x86_64/repoview/epel-release.html>`_
repositories. There are now dual python2.x and python3.x releases
available.


**Python 2.x**:

.. code-block:: bash

   $ sudo dnf install python2-bitmath

**Python 3.x**:

.. code-block:: bash

   $ sudo dnf install python3-bitmath


.. note::

   **Upgrading**: If you have the old *python-bitmath* package
   installed presently, you could also run ``sudo dnf update
   python-bitmath`` instead


**PyPi**:

You could also install bitmath from `PyPi
<https://pypi.python.org/pypi/bitmath>`_ if you like:

.. code-block:: bash

   $ sudo pip install bitmath

.. note::

   **pip** installs need pip >= 1.1. To workaround this, `download
   bitmath <https://pypi.python.org/pypi/bitmath/#downloads>`_, from
   PyPi and then ``pip install bitmath-x.y.z.tar.gz``. See `issue #57
   <https://github.com/tbielawa/bitmath/issues/57#issuecomment-227018168>`_
   for more information.


**PPA**:

Ubuntu Xenial, Wily, Vivid, Trusty, and Precise users can install
bitmath from the `launchpad PPA
<https://launchpad.net/~tbielawa/+archive/ubuntu/bitmath>`_:

.. code-block:: bash

   $ sudo add-apt-repository ppa:tbielawa/bitmath
   $ sudo apt-get update
   $ sudo apt-get install python-bitmath


**Source**:

Or, if you want to install from source:

.. code-block:: bash

   $ sudo python ./setup.py install

If you want the bitmath manpage installed as well:

.. code-block:: bash

   $ sudo make install


Documentation
=============

The main documentation lives at
`http://bitmath.readthedocs.org/en/latest/
<http://bitmath.readthedocs.org/en/latest/>`_.

Topics include:

* The ``bitmath`` Module

  * Utility Functions
  * Context Managers
  * Module Variables
  * ``argparse`` integration
  * ``progressbar`` integration

* The ``bitmath`` command-line Tool

* Classes

  * Initializing
  * Available Classes
  * Class Methods

* Instances

  * Instance Attributes
  * Instance Methods
  * Instance Properties
  * The Formatting Mini-Language

* Getting Started

  * Tables of Supported Operations
  * Basic Math
  * Unit Conversion
  * Rich Comparison
  * Sorting

* Real Life Examples

  * Download Speeds
  * Calculating how many files fit on a device
  * Printing Human-Readable File Sizes in Python
  * Calculating Linux BDP and TCP Window Scaling

* Contributing to bitmath
* Appendices

  * Rules for Math
  * On Units
  * Who uses Bitmath
  * Related Projects

* NEWS

* Copyright


Examples
========


Arithmetic
----------

.. code-block:: python

   >>> import bitmath
   >>> log_size = bitmath.kB(137.4)
   >>> log_zipped_size = bitmath.Byte(987)
   >>> print "Compression saved %s space" % (log_size - log_zipped_size)
   Compression saved 136.413kB space
   >>> thumb_drive = bitmath.GiB(12)
   >>> song_size = bitmath.MiB(5)
   >>> songs_per_drive = thumb_drive / song_size
   >>> print songs_per_drive
   2457.6


Convert Units
-------------

File size unit conversion:

.. code-block:: python

   >>> from bitmath import *
   >>> dvd_size = GiB(4.7)
   >>> print "DVD Size in MiB: %s" % dvd_size.to_MiB()
   DVD Size in MiB: 4812.8 MiB


Select a human-readable unit
----------------------------

.. code-block:: python

   >>> small_number = kB(100)
   >>> ugly_number = small_number.to_TiB()

   >>> print ugly_number
   9.09494701773e-08 TiB
   >>> print ugly_number.best_prefix()
   97.65625 KiB


Rich Comparison
---------------

.. code-block:: python

   >>> cd_size = MiB(700)
   >>> cd_size > dvd_size
   False
   >>> cd_size < dvd_size
   True
   >>> MiB(1) == KiB(1024)
   True
   >>> MiB(1) <= KiB(1024)
   True

Sorting
-------

.. code-block:: python

   >>> sizes = [KiB(7337.0), KiB(1441.0), KiB(2126.0), KiB(2178.0),
                     KiB(2326.0), KiB(4003.0), KiB(48.0), KiB(1770.0),
                     KiB(7892.0), KiB(4190.0)]

   >>> print sorted(sizes)
   [KiB(48.0), KiB(1441.0), KiB(1770.0), KiB(2126.0), KiB(2178.0),
   KiB(2326.0), KiB(4003.0), KiB(4190.0), KiB(7337.0), KiB(7892.0)]


Custom Formatting
-----------------

* Use of the custom formatting system
* All of the available instance properties

Example:

.. code-block:: python

   >>> longer_format = """Formatting attributes for %s
      ...: This instances prefix unit is {unit}, which is a {system} type unit
      ...: The unit value is {value}
      ...: This value can be truncated to just 1 digit of precision: {value:.1f}
      ...: In binary this looks like: {binary}
      ...: The prefix unit is derived from a base of {base}
      ...: Which is raised to the power {power}
      ...: There are {bytes} bytes in this instance
      ...: The instance is {bits} bits large
      ...: bytes/bits without trailing decimals: {bytes:.0f}/{bits:.0f}""" % str(ugly_number)

   >>> print ugly_number.format(longer_format)
   Formatting attributes for 5.96046447754 MiB
   This instances prefix unit is MiB, which is a NIST type unit
   The unit value is 5.96046447754
   This value can be truncated to just 1 digit of precision: 6.0
   In binary this looks like: 0b10111110101111000010000000
   The prefix unit is derived from a base of 2
   Which is raised to the power 20
   There are 6250000.0 bytes in this instance
   The instance is 50000000.0 bits large
   bytes/bits without trailing decimals: 6250000/50000000

Utility Functions
-----------------

**bitmath.getsize()**

.. code-block:: python

   >>> print bitmath.getsize('python-bitmath.spec')
   3.7060546875 KiB

**bitmath.parse_string()**

Parse a string with standard units:

.. code-block:: python

   >>> import bitmath
   >>> a_dvd = bitmath.parse_string("4.7 GiB")
   >>> print type(a_dvd)
   <class 'bitmath.GiB'>
   >>> print a_dvd
   4.7 GiB

**bitmath.parse_string_unsafe()**

Parse a string with ambiguous units:

.. code-block:: python

   >>> import bitmath
   >>> a_gig = bitmath.parse_string_unsafe("1gb")
   >>> print type(a_gig)
   <class 'bitmath.GB'>
   >>> a_gig == bitmath.GB(1)
   True
   >>> bitmath.parse_string_unsafe('1gb') == bitmath.parse_string_unsafe('1g')
   True


**bitmath.query_device_capacity()**

.. code-block:: python

   >>> import bitmath
   >>> with open('/dev/sda') as fp:
   ...     root_disk = bitmath.query_device_capacity(fp)
   ...     print root_disk.best_prefix()
   ...
   238.474937439 GiB

**bitmath.listdir()**

.. code-block:: python

   >>> for i in bitmath.listdir('./tests/', followlinks=True, relpath=True, bestprefix=True):
   ...     print i
   ...
   ('tests/test_file_size.py', KiB(9.2900390625))
   ('tests/test_basic_math.py', KiB(7.1767578125))
   ('tests/__init__.py', KiB(1.974609375))
   ('tests/test_bitwise_operations.py', KiB(2.6376953125))
   ('tests/test_context_manager.py', KiB(3.7744140625))
   ('tests/test_representation.py', KiB(5.2568359375))
   ('tests/test_properties.py', KiB(2.03125))
   ('tests/test_instantiating.py', KiB(3.4580078125))
   ('tests/test_future_math.py', KiB(2.2001953125))
   ('tests/test_best_prefix_BASE.py', KiB(2.1044921875))
   ('tests/test_rich_comparison.py', KiB(3.9423828125))
   ('tests/test_best_prefix_NIST.py', KiB(5.431640625))
   ('tests/test_unique_testcase_names.sh', Byte(311.0))
   ('tests/.coverage', KiB(3.1708984375))
   ('tests/test_best_prefix_SI.py', KiB(5.34375))
   ('tests/test_to_built_in_conversion.py', KiB(1.798828125))
   ('tests/test_to_Type_conversion.py', KiB(8.0185546875))
   ('tests/test_sorting.py', KiB(4.2197265625))
   ('tests/listdir_symlinks/10_byte_file_link', Byte(10.0))
   ('tests/listdir_symlinks/depth1/depth2/10_byte_file', Byte(10.0))
   ('tests/listdir_nosymlinks/depth1/depth2/10_byte_file', Byte(10.0))
   ('tests/listdir_nosymlinks/depth1/depth2/1024_byte_file', KiB(1.0))
   ('tests/file_sizes/kbytes.test', KiB(1.0))
   ('tests/file_sizes/bytes.test', Byte(38.0))
   ('tests/listdir/10_byte_file', Byte(10.0))


Formatting
----------

.. code-block:: python

   >>> with bitmath.format(fmt_str="[{value:.3f}@{unit}]"):
   ...     for i in bitmath.listdir('./tests/', followlinks=True, relpath=True, bestprefix=True):
   ...         print i[1]
   ...
   [9.290@KiB]
   [7.177@KiB]
   [1.975@KiB]
   [2.638@KiB]
   [3.774@KiB]
   [5.257@KiB]
   [2.031@KiB]
   [3.458@KiB]
   [2.200@KiB]
   [2.104@KiB]
   [3.942@KiB]
   [5.432@KiB]
   [311.000@Byte]
   [3.171@KiB]
   [5.344@KiB]
   [1.799@KiB]
   [8.019@KiB]
   [4.220@KiB]
   [10.000@Byte]
   [10.000@Byte]
   [10.000@Byte]
   [1.000@KiB]
   [1.000@KiB]
   [38.000@Byte]
   [10.000@Byte]

``argparse`` Integration
------------------------

Example script using ``bitmath.integrations.BitmathType`` as an
argparser argument type:

.. code-block:: python

   import argparse
   import bitmath
   parser = argparse.ArgumentParser(
       description="Arg parser with a bitmath type argument")
   parser.add_argument('--block-size',
                       type=bitmath.integrations.BitmathType,
                       required=True)

   results = parser.parse_args()
   print "Parsed in: {PARSED}; Which looks like {TOKIB} as a Kibibit".format(
       PARSED=results.block_size,
       TOKIB=results.block_size.Kib)

If ran as a script the results would be similar to this:

.. code-block:: bash

   $ python ./bmargparse.py --block-size 100MiB
   Parsed in: 100.0 MiB; Which looks like 819200.0 Kib as a Kibibit


``progressbar`` Integration
---------------------------

Use ``bitmath.integrations.BitmathFileTransferSpeed`` as a
``progressbar`` file transfer speed widget to monitor download speeds:

.. code-block:: python

   import requests
   import progressbar
   import bitmath
   import bitmath.integrations

   FETCH = 'https://www.kernel.org/pub/linux/kernel/v3.0/patch-3.16.gz'
   widgets = ['Bitmath Progress Bar Demo: ', ' ',
              progressbar.Bar(marker=progressbar.RotatingMarker()), ' ',
              bitmath.integrations.BitmathFileTransferSpeed()]

   r = requests.get(FETCH, stream=True)
   size = bitmath.Byte(int(r.headers['Content-Length']))
   pbar = progressbar.ProgressBar(widgets=widgets, maxval=int(size),
                                  term_width=80).start()
   chunk_size = 2048
   with open('/dev/null', 'wb') as fd:
       for chunk in r.iter_content(chunk_size):
           fd.write(chunk)
           if (pbar.currval + chunk_size) < pbar.maxval:
               pbar.update(pbar.currval + chunk_size)
   pbar.finish()


If ran as a script the results would be similar to this:

.. code-block:: bash

   $ python ./smalldl.py
   Bitmath Progress Bar Demo:  ||||||||||||||||||||||||||||||||||||||||| 1.58 MiB/s

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tbielawa/bitmath",
    "name": "bitmath",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Tim Bielawa",
    "author_email": "timbielawa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ec/ec/808245570e00df2e1fe8252903da309f18eb58768f44fecc0215dafbc386/bitmath-1.3.3.1.tar.gz",
    "platform": "",
    "description": ".. image:: https://api.travis-ci.org/tbielawa/bitmath.png\n   :target: https://travis-ci.org/tbielawa/bitmath/\n   :align: right\n   :height: 19\n   :width: 77\n\n.. image:: https://coveralls.io/repos/tbielawa/bitmath/badge.png?branch=master\n   :target: https://coveralls.io/r/tbielawa/bitmath?branch=master\n   :align: right\n   :height: 19\n   :width: 77\n\n.. image:: https://readthedocs.org/projects/bitmath/badge/?version=latest\n   :target: http://bitmath.rtfd.org/\n   :align: right\n   :height: 19\n   :width: 77\n\n\nbitmath\n=======\n\n`bitmath <http://bitmath.readthedocs.org/en/latest/>`_ simplifies many\nfacets of interacting with file sizes in various units. Originally\nfocusing on file size unit conversion, functionality now includes:\n\n* Converting between **SI** and **NIST** prefix units (``kB`` to ``GiB``)\n* Converting between units of the same type (SI to SI, or NIST to NIST)\n* Automatic human-readable prefix selection (like in `hurry.filesize <https://pypi.python.org/pypi/hurry.filesize>`_)\n* Basic arithmetic operations (subtracting 42KiB from 50GiB)\n* Rich comparison operations (``1024 Bytes == 1KiB``)\n* bitwise operations (``<<``, ``>>``, ``&``, ``|``, ``^``)\n* Reading a device's storage capacity (Linux/OS X support only)\n* `argparse <https://docs.python.org/2/library/argparse.html>`_\n  integration as a custom type\n* `progressbar <https://code.google.com/p/python-progressbar/>`_\n  integration as a better file transfer speed widget\n* String parsing\n* Sorting\n\n\nIn addition to the conversion and math operations, `bitmath` provides\nhuman readable representations of values which are suitable for use in\ninteractive shells as well as larger scripts and applications. The\nformat produced for these representations is customizable via the\nfunctionality included in stdlibs `string.format\n<https://docs.python.org/2/library/string.html>`_.\n\nIn discussion we will refer to the NIST units primarily. I.e., instead\nof \"megabyte\" we will refer to \"mebibyte\". The former is ``10^3 =\n1,000,000`` bytes, whereas the second is ``2^20 = 1,048,576``\nbytes. When you see file sizes or transfer rates in your web browser,\nmost of the time what you're really seeing are the base-2 sizes/rates.\n\n**Don't Forget!** The source for bitmath `is available on GitHub\n<https://github.com/tbielawa/bitmath>`_.\n\nAnd did we mention there's almost 200 unittests? `Check them out for\nyourself <https://github.com/tbielawa/bitmath/tree/master/tests>`_.\n\n\nInstallation\n============\n\nThe easiest way to install bitmath is via ``dnf`` (or ``yum``) if\nyou're on a Fedora/RHEL based distribution. bitmath is available in\nthe main Fedora repositories, as well as the `EPEL6\n<http://download.fedoraproject.org/pub/epel/6/i386/repoview/epel-release.html>`_\nand `EPEL7\n<http://download.fedoraproject.org/pub/epel/7/x86_64/repoview/epel-release.html>`_\nrepositories. There are now dual python2.x and python3.x releases\navailable.\n\n\n**Python 2.x**:\n\n.. code-block:: bash\n\n   $ sudo dnf install python2-bitmath\n\n**Python 3.x**:\n\n.. code-block:: bash\n\n   $ sudo dnf install python3-bitmath\n\n\n.. note::\n\n   **Upgrading**: If you have the old *python-bitmath* package\n   installed presently, you could also run ``sudo dnf update\n   python-bitmath`` instead\n\n\n**PyPi**:\n\nYou could also install bitmath from `PyPi\n<https://pypi.python.org/pypi/bitmath>`_ if you like:\n\n.. code-block:: bash\n\n   $ sudo pip install bitmath\n\n.. note::\n\n   **pip** installs need pip >= 1.1. To workaround this, `download\n   bitmath <https://pypi.python.org/pypi/bitmath/#downloads>`_, from\n   PyPi and then ``pip install bitmath-x.y.z.tar.gz``. See `issue #57\n   <https://github.com/tbielawa/bitmath/issues/57#issuecomment-227018168>`_\n   for more information.\n\n\n**PPA**:\n\nUbuntu Xenial, Wily, Vivid, Trusty, and Precise users can install\nbitmath from the `launchpad PPA\n<https://launchpad.net/~tbielawa/+archive/ubuntu/bitmath>`_:\n\n.. code-block:: bash\n\n   $ sudo add-apt-repository ppa:tbielawa/bitmath\n   $ sudo apt-get update\n   $ sudo apt-get install python-bitmath\n\n\n**Source**:\n\nOr, if you want to install from source:\n\n.. code-block:: bash\n\n   $ sudo python ./setup.py install\n\nIf you want the bitmath manpage installed as well:\n\n.. code-block:: bash\n\n   $ sudo make install\n\n\nDocumentation\n=============\n\nThe main documentation lives at\n`http://bitmath.readthedocs.org/en/latest/\n<http://bitmath.readthedocs.org/en/latest/>`_.\n\nTopics include:\n\n* The ``bitmath`` Module\n\n  * Utility Functions\n  * Context Managers\n  * Module Variables\n  * ``argparse`` integration\n  * ``progressbar`` integration\n\n* The ``bitmath`` command-line Tool\n\n* Classes\n\n  * Initializing\n  * Available Classes\n  * Class Methods\n\n* Instances\n\n  * Instance Attributes\n  * Instance Methods\n  * Instance Properties\n  * The Formatting Mini-Language\n\n* Getting Started\n\n  * Tables of Supported Operations\n  * Basic Math\n  * Unit Conversion\n  * Rich Comparison\n  * Sorting\n\n* Real Life Examples\n\n  * Download Speeds\n  * Calculating how many files fit on a device\n  * Printing Human-Readable File Sizes in Python\n  * Calculating Linux BDP and TCP Window Scaling\n\n* Contributing to bitmath\n* Appendices\n\n  * Rules for Math\n  * On Units\n  * Who uses Bitmath\n  * Related Projects\n\n* NEWS\n\n* Copyright\n\n\nExamples\n========\n\n\nArithmetic\n----------\n\n.. code-block:: python\n\n   >>> import bitmath\n   >>> log_size = bitmath.kB(137.4)\n   >>> log_zipped_size = bitmath.Byte(987)\n   >>> print \"Compression saved %s space\" % (log_size - log_zipped_size)\n   Compression saved 136.413kB space\n   >>> thumb_drive = bitmath.GiB(12)\n   >>> song_size = bitmath.MiB(5)\n   >>> songs_per_drive = thumb_drive / song_size\n   >>> print songs_per_drive\n   2457.6\n\n\nConvert Units\n-------------\n\nFile size unit conversion:\n\n.. code-block:: python\n\n   >>> from bitmath import *\n   >>> dvd_size = GiB(4.7)\n   >>> print \"DVD Size in MiB: %s\" % dvd_size.to_MiB()\n   DVD Size in MiB: 4812.8 MiB\n\n\nSelect a human-readable unit\n----------------------------\n\n.. code-block:: python\n\n   >>> small_number = kB(100)\n   >>> ugly_number = small_number.to_TiB()\n\n   >>> print ugly_number\n   9.09494701773e-08 TiB\n   >>> print ugly_number.best_prefix()\n   97.65625 KiB\n\n\nRich Comparison\n---------------\n\n.. code-block:: python\n\n   >>> cd_size = MiB(700)\n   >>> cd_size > dvd_size\n   False\n   >>> cd_size < dvd_size\n   True\n   >>> MiB(1) == KiB(1024)\n   True\n   >>> MiB(1) <= KiB(1024)\n   True\n\nSorting\n-------\n\n.. code-block:: python\n\n   >>> sizes = [KiB(7337.0), KiB(1441.0), KiB(2126.0), KiB(2178.0),\n                     KiB(2326.0), KiB(4003.0), KiB(48.0), KiB(1770.0),\n                     KiB(7892.0), KiB(4190.0)]\n\n   >>> print sorted(sizes)\n   [KiB(48.0), KiB(1441.0), KiB(1770.0), KiB(2126.0), KiB(2178.0),\n   KiB(2326.0), KiB(4003.0), KiB(4190.0), KiB(7337.0), KiB(7892.0)]\n\n\nCustom Formatting\n-----------------\n\n* Use of the custom formatting system\n* All of the available instance properties\n\nExample:\n\n.. code-block:: python\n\n   >>> longer_format = \"\"\"Formatting attributes for %s\n      ...: This instances prefix unit is {unit}, which is a {system} type unit\n      ...: The unit value is {value}\n      ...: This value can be truncated to just 1 digit of precision: {value:.1f}\n      ...: In binary this looks like: {binary}\n      ...: The prefix unit is derived from a base of {base}\n      ...: Which is raised to the power {power}\n      ...: There are {bytes} bytes in this instance\n      ...: The instance is {bits} bits large\n      ...: bytes/bits without trailing decimals: {bytes:.0f}/{bits:.0f}\"\"\" % str(ugly_number)\n\n   >>> print ugly_number.format(longer_format)\n   Formatting attributes for 5.96046447754 MiB\n   This instances prefix unit is MiB, which is a NIST type unit\n   The unit value is 5.96046447754\n   This value can be truncated to just 1 digit of precision: 6.0\n   In binary this looks like: 0b10111110101111000010000000\n   The prefix unit is derived from a base of 2\n   Which is raised to the power 20\n   There are 6250000.0 bytes in this instance\n   The instance is 50000000.0 bits large\n   bytes/bits without trailing decimals: 6250000/50000000\n\nUtility Functions\n-----------------\n\n**bitmath.getsize()**\n\n.. code-block:: python\n\n   >>> print bitmath.getsize('python-bitmath.spec')\n   3.7060546875 KiB\n\n**bitmath.parse_string()**\n\nParse a string with standard units:\n\n.. code-block:: python\n\n   >>> import bitmath\n   >>> a_dvd = bitmath.parse_string(\"4.7 GiB\")\n   >>> print type(a_dvd)\n   <class 'bitmath.GiB'>\n   >>> print a_dvd\n   4.7 GiB\n\n**bitmath.parse_string_unsafe()**\n\nParse a string with ambiguous units:\n\n.. code-block:: python\n\n   >>> import bitmath\n   >>> a_gig = bitmath.parse_string_unsafe(\"1gb\")\n   >>> print type(a_gig)\n   <class 'bitmath.GB'>\n   >>> a_gig == bitmath.GB(1)\n   True\n   >>> bitmath.parse_string_unsafe('1gb') == bitmath.parse_string_unsafe('1g')\n   True\n\n\n**bitmath.query_device_capacity()**\n\n.. code-block:: python\n\n   >>> import bitmath\n   >>> with open('/dev/sda') as fp:\n   ...     root_disk = bitmath.query_device_capacity(fp)\n   ...     print root_disk.best_prefix()\n   ...\n   238.474937439 GiB\n\n**bitmath.listdir()**\n\n.. code-block:: python\n\n   >>> for i in bitmath.listdir('./tests/', followlinks=True, relpath=True, bestprefix=True):\n   ...     print i\n   ...\n   ('tests/test_file_size.py', KiB(9.2900390625))\n   ('tests/test_basic_math.py', KiB(7.1767578125))\n   ('tests/__init__.py', KiB(1.974609375))\n   ('tests/test_bitwise_operations.py', KiB(2.6376953125))\n   ('tests/test_context_manager.py', KiB(3.7744140625))\n   ('tests/test_representation.py', KiB(5.2568359375))\n   ('tests/test_properties.py', KiB(2.03125))\n   ('tests/test_instantiating.py', KiB(3.4580078125))\n   ('tests/test_future_math.py', KiB(2.2001953125))\n   ('tests/test_best_prefix_BASE.py', KiB(2.1044921875))\n   ('tests/test_rich_comparison.py', KiB(3.9423828125))\n   ('tests/test_best_prefix_NIST.py', KiB(5.431640625))\n   ('tests/test_unique_testcase_names.sh', Byte(311.0))\n   ('tests/.coverage', KiB(3.1708984375))\n   ('tests/test_best_prefix_SI.py', KiB(5.34375))\n   ('tests/test_to_built_in_conversion.py', KiB(1.798828125))\n   ('tests/test_to_Type_conversion.py', KiB(8.0185546875))\n   ('tests/test_sorting.py', KiB(4.2197265625))\n   ('tests/listdir_symlinks/10_byte_file_link', Byte(10.0))\n   ('tests/listdir_symlinks/depth1/depth2/10_byte_file', Byte(10.0))\n   ('tests/listdir_nosymlinks/depth1/depth2/10_byte_file', Byte(10.0))\n   ('tests/listdir_nosymlinks/depth1/depth2/1024_byte_file', KiB(1.0))\n   ('tests/file_sizes/kbytes.test', KiB(1.0))\n   ('tests/file_sizes/bytes.test', Byte(38.0))\n   ('tests/listdir/10_byte_file', Byte(10.0))\n\n\nFormatting\n----------\n\n.. code-block:: python\n\n   >>> with bitmath.format(fmt_str=\"[{value:.3f}@{unit}]\"):\n   ...     for i in bitmath.listdir('./tests/', followlinks=True, relpath=True, bestprefix=True):\n   ...         print i[1]\n   ...\n   [9.290@KiB]\n   [7.177@KiB]\n   [1.975@KiB]\n   [2.638@KiB]\n   [3.774@KiB]\n   [5.257@KiB]\n   [2.031@KiB]\n   [3.458@KiB]\n   [2.200@KiB]\n   [2.104@KiB]\n   [3.942@KiB]\n   [5.432@KiB]\n   [311.000@Byte]\n   [3.171@KiB]\n   [5.344@KiB]\n   [1.799@KiB]\n   [8.019@KiB]\n   [4.220@KiB]\n   [10.000@Byte]\n   [10.000@Byte]\n   [10.000@Byte]\n   [1.000@KiB]\n   [1.000@KiB]\n   [38.000@Byte]\n   [10.000@Byte]\n\n``argparse`` Integration\n------------------------\n\nExample script using ``bitmath.integrations.BitmathType`` as an\nargparser argument type:\n\n.. code-block:: python\n\n   import argparse\n   import bitmath\n   parser = argparse.ArgumentParser(\n       description=\"Arg parser with a bitmath type argument\")\n   parser.add_argument('--block-size',\n                       type=bitmath.integrations.BitmathType,\n                       required=True)\n\n   results = parser.parse_args()\n   print \"Parsed in: {PARSED}; Which looks like {TOKIB} as a Kibibit\".format(\n       PARSED=results.block_size,\n       TOKIB=results.block_size.Kib)\n\nIf ran as a script the results would be similar to this:\n\n.. code-block:: bash\n\n   $ python ./bmargparse.py --block-size 100MiB\n   Parsed in: 100.0 MiB; Which looks like 819200.0 Kib as a Kibibit\n\n\n``progressbar`` Integration\n---------------------------\n\nUse ``bitmath.integrations.BitmathFileTransferSpeed`` as a\n``progressbar`` file transfer speed widget to monitor download speeds:\n\n.. code-block:: python\n\n   import requests\n   import progressbar\n   import bitmath\n   import bitmath.integrations\n\n   FETCH = 'https://www.kernel.org/pub/linux/kernel/v3.0/patch-3.16.gz'\n   widgets = ['Bitmath Progress Bar Demo: ', ' ',\n              progressbar.Bar(marker=progressbar.RotatingMarker()), ' ',\n              bitmath.integrations.BitmathFileTransferSpeed()]\n\n   r = requests.get(FETCH, stream=True)\n   size = bitmath.Byte(int(r.headers['Content-Length']))\n   pbar = progressbar.ProgressBar(widgets=widgets, maxval=int(size),\n                                  term_width=80).start()\n   chunk_size = 2048\n   with open('/dev/null', 'wb') as fd:\n       for chunk in r.iter_content(chunk_size):\n           fd.write(chunk)\n           if (pbar.currval + chunk_size) < pbar.maxval:\n               pbar.update(pbar.currval + chunk_size)\n   pbar.finish()\n\n\nIf ran as a script the results would be similar to this:\n\n.. code-block:: bash\n\n   $ python ./smalldl.py\n   Bitmath Progress Bar Demo:  ||||||||||||||||||||||||||||||||||||||||| 1.58 MiB/s\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pythonic module for representing and manipulating file sizes with different prefix notations (file size unit conversion)",
    "version": "1.3.3.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "041fb78aefe713f77dc1f9c08ea4e52b",
                "sha256": "293325f01e65defe966853111df11d39215eb705a967cb115851da8c4cfa3eb8"
            },
            "downloads": -1,
            "filename": "bitmath-1.3.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "041fb78aefe713f77dc1f9c08ea4e52b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 88519,
            "upload_time": "2018-08-23T16:23:53",
            "upload_time_iso_8601": "2018-08-23T16:23:53.031981Z",
            "url": "https://files.pythonhosted.org/packages/ec/ec/808245570e00df2e1fe8252903da309f18eb58768f44fecc0215dafbc386/bitmath-1.3.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2018-08-23 16:23:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "tbielawa",
    "github_project": "bitmath",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": false,
    "requirements": [
        {
            "name": "python-coveralls",
            "specs": []
        },
        {
            "name": "coverage",
            "specs": [
                [
                    "==",
                    "4.0.3"
                ]
            ]
        },
        {
            "name": "mock",
            "specs": []
        },
        {
            "name": "nose",
            "specs": []
        },
        {
            "name": "pyflakes",
            "specs": []
        },
        {
            "name": "pycodestyle",
            "specs": []
        },
        {
            "name": "progressbar231",
            "specs": []
        },
        {
            "name": "click",
            "specs": []
        }
    ],
    "lcname": "bitmath"
}
        
Elapsed time: 0.03004s