Giter VIP home page Giter VIP logo

binary's Introduction

binary

Latest PyPI version GitHub Actions Codecov Supported Python versions License

binary provides a bug-free and easy way to convert between and within binary (IEC) and decimal (SI) units.

Installation

binary is distributed on PyPI as a universal wheel and is available on Linux/macOS and Windows and supports Python 2.7/3.5+ and PyPy.

$ pip install binary

Examples

Let's import what we need:

>>> from binary import BinaryUnits, DecimalUnits, convert_units

Basic conversion:

>>> convert_units(1536, BinaryUnits.KB, BinaryUnits.MB)
(1.5, 'MiB')

How much actual storage your new hard drive has:

>>> convert_units(4, DecimalUnits.TB, BinaryUnits.TB)
(3.637978807091713, 'TiB')

Human readable:

>>> amount, unit = convert_units(kubernetes_ingest_bytes_per_second)
>>> 'Incoming traffic: {:.2f} {}/s'.format(amount, unit)
'Incoming traffic: 24.77 GiB/s'

Usage

Just a single function!

convert_units(n, unit=BYTE, to=None, si=False, exact=False)

Converts between and within binary and decimal units. If no unit is specified, n is assumed to already be in bytes. If no to is specified, n will be converted to the highest unit possible. If no unit nor to is specified, the output will be binary units unless si is True. If exact is True. the calculations will use decimal.Decimal.

  • Parameters
    • n (int or float) - The number of units.
    • unit - The unit n represents. See types.
    • to - The unit to convert to. See types.
    • si (bool) - Assume SI units when no unit nor to is specified.
    • exact (bool) - Use decimal.Decimal for calculations.

Types

Although the string representations for binary units end in iB, the attributes do not for ease of use.

Type Short Long
BinaryUnits B BYTE
BinaryUnits KB KIBIBYTE
BinaryUnits MB MEBIBYTE
BinaryUnits GB GIBIBYTE
BinaryUnits TB TEBIBYTE
BinaryUnits PB PEBIBYTE
BinaryUnits EB EXBIBYTE
BinaryUnits ZB ZEBIBYTE
BinaryUnits YB YOBIBYTE
DecimalUnits B BYTE
DecimalUnits KB KILOBYTE
DecimalUnits MB MEGABYTE
DecimalUnits GB GIGABYTE
DecimalUnits TB TERABYTE
DecimalUnits PB PETABYTE
DecimalUnits EB EXABYTE
DecimalUnits ZB ZETTABYTE
DecimalUnits YB YOTTABYTE

License

binary is distributed under the terms of both

at your option.

Changelog

Important changes are emphasized.

1.0.0

  • Initial release

binary's People

Contributors

jdufresne avatar ofek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

binary's Issues

Doesn't handle negative numbers

I like this library but it doesn't seem to work well for negative numbers

>>> convert_units(Decimal('-149250113536'))
(Decimal('-149250113536'), 'B')
>>> convert_units(Decimal('149250113536'))
(Decimal('139'), 'GiB')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.