Skip to main content

Pythonic module for representing and manipulating file sizes with different prefix notations.

Project description

Only a snippet of the full README is shown on PyPi

NOTE: I strongly urge you to read this on GitHub instead. Over there the Markdown formatting is rendered much more pleasantly.

[![Build Status](https://api.travis-ci.org/tbielawa/bitmath.png)](https://travis-ci.org/tbielawa/bitmath/) [![Coverage Status](https://img.shields.io/coveralls/tbielawa/bitmath.svg)](https://coveralls.io/r/tbielawa/bitmath?branch=master)

bitmath

bitmath simplifies many facets of interacting with file sizes in various units. Examples include: converting between SI and NIST prefix units (GiB to kB), converting between units of the same type (SI to SI, or NIST to NIST), basic arithmetic operations (subtracting 42KiB from 50GiB), rich comparison operations (1024 Bytes == 1KiB), bitwise operations (<<, >>, &, |, ^), and 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.

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

  • Classes * [Class Initializer Signature](#class-initializer-signature) * [Class Methods](#class-methods)
  • Instances * [Available Classes](#available-classes) * [Instance Methods](#instance-methods) * [Instance Attributes](#instance-attributes)
  • [Usage](#usage)
  • [Basic Examples](#examples)
  • [Real Life Examples](#real-life-examples) * [Download Speeds](#example-1) * [Calculating how many files fit on a device](#example-2) * [Printing Human-Readable File Sizes in Python](#example-3) * [Calculating Linux BDP and TCP Window Scaling](#example-4)
  • [On Units](#on-units)

Basics

### Class Initializer Signature

BitMathType([value=0, [bytes=None, [bits=None]]])

A bitmath type may be initialized in four different ways:

  • Set no initial value

The default size is 0

zero_kib = KiB()
  • Set the value in current prefix units

That is to say, if you want to encapsulate 1KiB, initialize the bitmath type with 1:

one_kib = KiB(1)

one_kib = KiB(value=1)
  • Set the number of bytes

Use the bytes keyword

one_kib = KiB(bytes=1024)
  • Set the number of bits

Use the bits keyword

one_kib = KiB(bits=8192)

Project details


Release history Release notifications

History Node

1.3.1.2

History Node

1.3.1.1

History Node

1.3.0.2

History Node

1.3.0.1

History Node

1.2.4.1

History Node

1.2.3-4

History Node

1.2.3-1

History Node

1.2.0-1

History Node

1.1.0-1

History Node

1.0.8-1

History Node

1.0.7-1

This version
History Node

1.0.6-1

History Node

1.0.5-1

History Node

1.0.4-1

History Node

1.0.2-3

History Node

1.0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
bitmath-1.0.6-1.tar.gz (39.5 kB) Copy SHA256 hash SHA256 Source None Jul 14, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page