Update: 2014-03-27 – python-bitmath is now in Fedora!
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.
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.
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!
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.
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.
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.
My first blog post (co-authored wtih Steve Milner) on the Red Hat Developer Blog (@RHELDevelop) has just been published!
[..] this post describes our journey from initially trying to implement a simple solution to improve the day-to-day lives of developers, through the technical limitations we experienced along the way, and finally arrives at the empathy for our developers we’ve gained from that experience. We’ll wrap up with a note on how Red Hat Software Collections (announced as GA in September) would’ve simplified our development process.
Enjoy!
See also:
You’re using Ruby on Rails and the Twitter Bootstrap framework. You are using the link_to ActionView helper method. The generated anchor will be visually represented in a navbar as a Bootstrap button component (via the ‘btn‘ and ‘btn-small‘ class attributes). Finally, you want to use one of those sweet Boostrap icons instead of text. It should look something like this:
I had difficulty understanding the correct way to call the link_to method to get the results I desired. Part of my failure to grok is because I’m a complete newbie to RoR, and the other part of it is because the link_to method has 4 distinct signatures that you can call it by.
After some searching around I found a couple of resources on Stack Overflow which looked promising:
However, none of those results were quite exactly what I was looking for. They did provide some useful insight into solving the problem though.
Here’s the code:
If my understanding is correct, then this approach implements the third link_to method signature: link_to(options = {}, html_options = {}) do
The options parameter we give here is a hash which is passed to the url_for method, and finally to the Route Module. It ends up returning a URL string in the form of http://yoursite/pages/new
The html_options parameter describes the attributes (other than href) which we desire present in the generated anchor. In this example we are describing an anchor with two classes: btn and btn-small. You could add additional symbols to the hash just as easily: {:class => ‘btn btn-small’, :id => ‘new-page-button’, :title => ‘Create A New Page’}
Finally, the way we’re calling link_to requires that we pass it a block to use as the generated link body (or in our case, icon). So we escape from the ERB sequence for a moment, enter the HTML which Boostrap turns into an icon, and then close the block.
Here’s a shell function which unifies the act of creating a directory and then subsequently changing into it:
Example usage:
In the future I hope to expand on this list. In general though, strive to follow the basics of the Unix Philosophy.
You should only know what your close friends tell you – http://en.wikipedia.org/wiki/Law_of_Demeter
Boring behavior is the new exciting – http://en.wikipedia.org/wiki/Principle_of_least_astonishment
(Doing) less is more – http://www.faqs.org/docs/artu/ch01s06.html
Rule of Repair: Repair what you can — but when you must fail, fail noisily and as soon as possible http://www.catb.org/~esr/writings/taoup/html/ch01s06.html#id2878538