Technitribe

interesting problems (and a few solutions, too)

Technitribe
  • About the Authors
  • Log In
  • Log Out
  • Lost Password
  • Register
  • Reset Password
    • 16 Mar 2014

      New side-project: bitmath – A Python module for representing file sizes with different prefix notations

      Written by Tim Bielawa

      Update: 2014-03-27 – python-bitmath is now in Fedora!

      Background

      I’ve written a new Python module which I’m calling bitmath.

      Given my day job as a System Administrator, and the content of my upcoming book on Linux virtual disks, I frequently find myself in situations requiring me to convert file sizes into various other formats. I recall far too many instances of having to do unit conversions mid-code in projects (does this look familiar to anyone else?):

      Thus, out of necessity, bitmath was born.

      bitmath

      I’m going to plagiarize myself here and rip the long description right from the project:

      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), and rich comparison operations (1024 Bytes == 1KiB).

      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.

      Anyone not deeply invested in the heartache that comes with converting base-2 and base-10 prefix units can stop reading now.

      Why Should You Care?

      bitmath is Pythonic

      bitmath finally provides a uniform way to manipulate these units in a way which is natural to Python programmers. There’s nothing special or unusual required to work with bitmath objects. You can add them, subtract them, multiply and divide them — just like you’re already used to with the int and float objects. bitmath objects support all of the standard Rich Comparison Operators as well!

      bitmath converts anything

      Converting bitmath objects is a first-class supported operation. Even more complex conversions, like converting from Mibibits to Kilobytes, is trivial. This functionality is supported in both directions: from the lowliest Bit() object through the grand EiB() object. That is to say, any bitmath object, whether it be base-2 (NIST ‘kibi’ and ‘gibi’ style) or base-10 (SI ‘kilo’ and ‘giga’ style), supports conversion (without loss of accuracy) to any other valid prefix unit.

      bitmath objects print out nicely

      As the previous two examples demonstrated, bitmath objects have useful console representations, as well as string representations. In a future release the format string for this may be configurable.

      Getting bitmath

      Fedora/RHEL6 users will need to wait a bit longer for a proper yum install command to be available, as the package is currently sitting in the package-review queue awaiting judgement. Update: 2014-03-27: python-bitmath is now in Fedora!

      However, there are several other methods available for installation: installing from PyPi, building your own RPM, and from setup/distutils.

       

      • Tags »

    One Comment on “New side-project: bitmath – A Python module for representing file sizes with different prefix notations”

    • python-bitmath – Now available in Fedora! | Technitribe

      27 Mar 2014 11:03 am

      […] Last week I wrote about bitmath, a Python module I made for working with (prefix) units commonly used to represent file sizes, e.g., kB, GiB, Byte, TB.  I can now happily say that python-bitmath has passed the review process and has been officially accepted into Fedora! […]

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    • The Authors
    • Virtual Disk Guide

      Interested in virtualization? Do QCOWs rule your filesystem? Are you a libvirt or KVM+QEMU wizard? I wrote a book about virtual disk management. Check out the The Linux Sysadmin's Guide to Virtual Disks online for free at ScribesGuides.com.


      Consider supporting the author by purchasing a hard copy of the first edition for just $10.00 on Lulu.com.

    • bitmath

      bitmath is a Python library for dealing with file size units (GiB's, kB's, etc) in a sane way. bitmath supports arithmetic, rich comparison, conversion, automatic best human-readable representation, and many other utility functions. Read some examples on the docs site or check out the source on GitHub.

    • latest posts

      • Querying block device sizes in Python on Linux and Mac OS X February 4, 2023
      • Using jq to filter an array of objects from JSON September 9, 2019
      • Two Year Break — And we’re back! November 16, 2018
    • tags

      bitmath blog conference css dblatex DNS DocBook eclipse Emacs Erlang Fedora fedora 22 filter GNU Screen Haiku Introduction java jboss LCSEE Linux locale locales fix slicehost ubuntu Macports module nist nXML-Mode opengl open source OS X package packaging pki prefix units presentation project pypi Python scholarship si summit Tutorial ubuntu xcode XML XMPP
    • h4ck teh world

      tbielawatbielawa
      • Issue Comment
        bitmath
        February 6, 2023 - 12:55 am UTC
      • Issue Comment
        bitmath
        February 6, 2023 - 12:54 am UTC
      • Push
        bitmath
        February 6, 2023 - 12:51 am UTC
      • Issue Comment
        bitmath
        February 6, 2023 - 12:36 am UTC
      • Push
        bitmath
        February 6, 2023 - 12:30 am UTC

Creative Commons License
Technitribe by Tim Bielawa is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.