astropy/astropy — pull request #1268 by mdboom (2013-07-18 19:34:59+00:00)
Title: Allow sets of units to be enabled

This is a follow on to #1073.

This adds methods `add_enabled_units` and `set_enabled_units` to allow for adding sets or individual units after the fact.

I played with various approaches to this for a while. I'm not crazy about the fact that this relies on global state (the present implementation does, too, although less flexibly). The alternative is to require passing in sets of units to functions that do searching (e.g. find_equivalent_units). Ulteimately, I also thought that the convenience of "setting and forgetting" new units is more important, and the lack of thread-safety is not terribly problematic in this case, as the 99% use case is adding units as part of initialization, not adding units "on-the-fly".

There is no function to remove units -- that's tricky to get right giving all of the aliasing and duplication checking that goes on. Instead, I added context managers to allow for temporary enabling of units that restore to the previous state after exiting the `with` block.

With this change, Imperial units are no longer available by default, and they aren't injected into the `astropy.units` namespace ever. Most of the changes to the tests are for this.

Also, a note about backward compatibility. The old `register` and `deregister` methods now just raise an exception. It isn't really feasible to deprecate them and remove later, since that would require two fundamentally different ways of handling the unit registry at the bottom level at the same time. I hope that registering custom units is enough of a corner case (outside of astropy itself) that this won't be a big issue. We did warn that the API is not final on this stuff in 0.2 after all anyway.