Uncertainty extension for IPython
Wouldn’t it be nice if we had uncertainty with a nice notation in IPython? The current method would be to use raw Python,
from uncertainties import ufloat
print(ufloat(12.34, 0.01))
12.340+/-0.010
Let’s use the infix library to make the notation easier. We’ll define |pm|
to
mean +/-
.
Plumbum color
I’ve been working on a color addition to Plumbum for a little while, and I’d
like to share the basics of using it with you now. This library was originally
built around a special str
subclass, but now is built on the new Styles
representation and is far more powerful than the first implementation. It safely
does nothing if you do not have a color-compatible systems (posix + tty
currently), but can be forced if need be. It is included with Plumbum, so you
don’t have to add a requirement for your scripts that is non-essential (as color
often is). It is integrated with plumbum.cli
, too. Also, I’ve managed to
accelerate the color selection algorithms about 8x, allowing near game-like
speeds. (see the fullcolor.py
example).
Plumbum scripting
Scripting in Bash is a pain. Bash can do almost anything, and is unbeatable for small scripts, but it struggles when scaling up to doing anything close to a real world scripting problem. Python is a natural choice, especially for the scientist who already is using it for analysis. But, it’s much harder to do basic tasks in Python. So you are left with scripts starting out as Bash scripts, and then becoming a mess, then being (usually poorly) ported to Python, or even worse, being run by a Python script. I’ve seen countless Python scripts that run Bash scripts that run real programs. I’ve even written one or two. It’s not pretty.
I recently came (back) across a really powerful library for doing efficient command line scripts in Python. It contains a set of tools that makes the four (five with color) main tasks of command line scripts simple and powerful. I will also go over the one main drawback of the library (and the possible enhancement!).
[Read More]Simple Overloading in Python
This is intended as an example to demonstrate the use of overloading in object
oriented programming. This was written as a Jupyter notebook (aka IPython) in
Python 3. To run in Python 2, simply rename the variables that have unicode
names, and replace truediv
with div
.
While there are several nice Python libraries that support uncertainty (for example, the powerful uncertainties package and the related units and uncertainties package pint), they usually use standard error combination rules. For a beginning physics class, often ‘maximum error’ combination is used. Here, instead of using a standard deviation based error and using combination rules based on uncorrelated statistical distributions, we assume a simple maximum error and simply add errors.
To implement this, let’s build a Python class and use overloading to implement algebraic operations.
[Read More]