Posts Tagged ‘Programming’

Nxpy 0.3.0 released

November 30, 2014

I’m happy to announce the availability of release 0.3.0 of my Nxpy library, which is available from PyPI and from SourceForge. Updated documentation may be found here.

The main purpose of this release is Python 3 compatibility: all tests now pass with 3.4, 3.3 and 3.2, as well as 2.7 and 2.6. Parts of the library still work with 2.5. With the exception of the ccase package all tests were run with all Python versions on Windows 7 and Fedora 20, and with all but 2.5 on OS X 10.10. The ccase non-destructive tests were run only on Windows 7 with Python 3.4 and 2.7; the destructive tests weren’t run at all, as I don’t have a test ClearCase installation available anymore.

Given the focus on Python 3 there are almost no new features; a few methods received an additional encoding argument to better support the differences in string handling between 2 and 3.

The only thing that is completely new is the CurrentDirectory class in the path module. This is a context manager that lets you change directory temporarily, a little like the pushd/popd commands.

Advertisements

A little thing I didn’t know about Java enums

June 21, 2013

I like being amazed by programming languages, especially when it’s a case of the obvious just working. One could argue that that’s how it should always be and that there should be nothing amazing about it, but that’s another story.

I happened to have an enum like this:

public enum Dbms { DB2_WIN, DB2_AS, DB2_LINUX, ORACLE; }

and I wanted to provide a way to check if an instance was one of the different DB2 variants. I decided that the simplest, most convenient approach would have been to add a boolean method such as:

public boolean isDB2() {
  // Check if this is one of the DB2 constants
}

However I had never needed to give an enum a non static method before and I realized that I didn’t know how to refer to the current instance’s value from such a method. While googleing for inspiration a thought dawned on me: an enum instance doesn’t have a value, it is a value. So I wrote my method as follows:

public boolean isDB2() {
  switch(this) {
  case DB2_AS:
  case DB2_WIN:
  case DB2_LINUX:
    return true;
  default:
    return false;
  }
}

and it just worked. As simple as that.