Archive for the ‘Programming’ Category

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.

How I became a Python programmer

August 22, 2013

Read all about it on the Josetteorama blog.

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.

Quote of the day #3

January 5, 2009

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots.

So far, the Universe is winning.”

Richard Cook

From an email signature.

Test results for Boost 1.37.0 and CodeGear compilers…

November 5, 2008

… may be found here!

I ran the tests both before and after applying Update 1 and the results are exactly the same.

Boost 1.37 is out

November 4, 2008

As you probably know Boost 1.37 was released yesterday. I expect it to be well received by CodeGear users, as it includes many specific patches for both C++Builder 2009 and C++Builder 2007. The good news are that many of these patches actually disable workarounds that are not needed any more and none is specific to the latest compiler version, so apparently there are no new regressions.

Among the involved libraries there are some of the fundamental ones, as Config, CallTraits, Exception, Iterator, MPL and TypeTraits. Asio, Foreach, Filesystem, Function, Functional and Serialization also received patches. Unfortunately there were quite a few patches that I didn’t manage to submit in time; these will have to wait for 1.38, I’m afraid.

I hope to be able to post test results for both CB2009 and CB2007 on the bcbboost site by tomorrow evening. Originally I didn’t plan to issue a new bcbboost release, but CodeGear released Update 1 for CB2009 too late for explicit support to be included in 1.37. In the next few days I’ll check what needs to be done.

C++ Builder 2009, Boost & bcbboost

September 22, 2008

The number of visits to this blog has increased in the last few days. I obviously don’t know for sure, but C++ Builder 2009 was recently released and one possible reason for the increased traffic is that people are looking for information on Boost support for the new compiler.

As you probably know CB 2009 includes a custom version of Boost 1.35 . This includes a very high number of specific changes which, combined with the standard compliance improvements in the compiler, increase dramatically the level of support compared with previous releases. I would consider this a very convenient solution for those that do not need the latest libraries.

I’m currently in the process of helping the CodeGear people merge their changes into the official Boost repository and I hope to finish in time for the next Boost release, 1.37 . Should I not make it I plan to check with CodeGear whether they have any objection on issuing their patches through bcbboost first.

I currently have no plans for a 1.36 backport, but if you direly need it, let me know.

Welcome to .Nick’s!

September 4, 2008

I just setup a small SourceForge project to hold the C# code I wrote during the last few months. It should be fairly general purpose, with a slant towards system tool programming, which is what I’ve been spending a fair portion of my time on since the beginning of this year. You can check it out here.

You will find the source code and a little bit of documentation. The next thing to do is to issue an initial release. Hopefully during the weekend, but don’t hold your breath.

Searching for a raskal

August 5, 2008

A while ago, while trying to find my way around the intricacies of .NET’s approach to application configuration, I stumbled upon this article. As I found the approach it described as well as the code it provides very convenient, I built around it my own configuration support classes.

Now I would like to publish my current C# toolbox as Open Source and I’d like to include stuff that’s directly derived from the article I just mentioned. Thus I’m trying to get in touch with raskal, who originally posted the article on the Nezdeboeuf blog on the RioterDeckers’ Headquarters site, to ask his or her permission.

Hopefully sooner or later someone who knows how to reach her or him will google for one of these names and chance upon this post…

insert_separator

June 25, 2008

I recently realized that there is a problem that has kept cropping up regularly in my code for the last few months: transform a sequence of words in a comma separated list. Not a big issue in itself, but I find it interesting for two reasons: first, it is almost impossible to specify a solution that matches the regularity and simplicity of the expected outcome; second, it’s a good test case for checking how well different languages support writing a generic solution.

I have to confess that I wouldn’t have given it much thought if this wasn’t a kind of problem that really irks my sense of aesthetics; what you want is something as simple as:

string1, string2, string3

A very plain sequence, without any exception or special case. Yet when you try to code it you can’t avoid making a special case of either the first or the last string, as in this Python function:

def insert_separator1(l):
    ret = l[0]
    for s in l[1:]:
        ret += ", "
        ret += s
    return ret

Otherwise you have to insert a conditional inside the loop, which conceptually you should only need to compute once, as in this alternative Python implementation:

def insert_separator2(l):
    ret = ""
    first = True
    for s in l:
        if first:
            first = False
        else:
            ret += ", "
        ret += s
    return ret

By the way, I’m well aware that Python’s standard library provides a ready-made solution:

print ", ".join([ "string1", "string2", "string3" ])

While Python is very convenient to expose programming concepts, the code where this little problem keeps coming up is actually written in C#. This language also has a library solution, which is very similar to Python’s:

using System;

class InsertSeparator1
{
    static void Main(string[] args)
    {
        string[] l = { "string1", "string2",
                "string3" };
        System.Console.Out.WriteLine(
                string.Join( ", ", l));
    }
}

These library solutions are undoubtedly convenient, but they are limited to the scenario where you want to concatenate your list elements into a single string. Sometimes, however, you would rather output your strings to a file, or you might want to interleave other kinds of things, rather than just strings.

Let’s see then how different languages support a generic solution. Thanks to generators and duck typing Python makes it very easy:

import sys

def insert_separator4(elements, sep):
    first = True
    for e in elements:
        if first:
            first = False
        else:
            yield sep
        yield e

if __name__ == "__main__":
    for e in insert_separator4([ "string1",
            "string2", "string3" ], ", "):
        sys.stdout.write(e)
    sys.stdout.write( '\n' )

Slightly less so C#, which is constrained by static typing and the subordination of generics to inheritance as a means of generalization:

class InsertSeparator2
{
    static IEnumerable<ValueType>
            InsertSeparator<ValueType>(
                    IEnumerable<ValueType> elements,
                    ValueType sep)
    {
        bool first = true;
        foreach (ValueType e in elements)
        {
            if (first)
                first = false;
            else
                yield return sep;
            yield return e;
        }
    }

    static void Main(string[] args)
    {
        string[] l = { "string1", "string2",
                "string3" };
        foreach ( string e in
                InsertSeparator<string>(l, ", " ) )
        {
            System.Console.Out.Write(e);
        }
        System.Console.Out.WriteLine();
    }
}

Note that you need to implement the IEnumerable<T> interface in order to exploit this function.

C++ is also statically typed, but thanks to a form of compile time duck typing supported by templates it doesn’t require an abstract base class:

#include <iterator>
#include <iostream>
#include <string>

template <typename InputIter, typename OutputIter, 
        typename ValueType>
void insert_separator(InputIter first, 
        InputIter last, 
        OutputIter out, 
        ValueType sep)
{
    bool initial = true;
    while ( first != last )
    {
        if ( initial )
            initial = false;
        else
            *out++ = sep;
        *out++ = *first++;
    }
}

int main()
{
    std::string l[] = { "string1", "string2",
            "string3" };
    insert_separator(l, l + 3, 
            std::ostream_iterator<std::string>(
                    std::cout), 
            ", ");
}

At first sight the C++ solution isn’t much better than the C# one. However generalization is not just a matter of language constructs; it is also a matter of conceptual framework. With little effort from our part the function above integrates perfectly with the standard library’s iterators and containers, as well as with any user defined ones, provided they adhere to the standard library constraints.

Finally, there is a solution to this problem which satisfies my quest for elegance, but it’s currently late at night and this post is already long enough as it is 😉